diff options
author | Alex Williams <alex.williams@ni.com> | 2018-10-15 12:03:07 -0700 |
---|---|---|
committer | Brent Stapleton <bstapleton@g.hmc.edu> | 2018-11-07 18:26:13 -0800 |
commit | 738b9ec102cdb307413cab23fdcf7fb2522f9895 (patch) | |
tree | 53a1181c6a38915b778d42cd02a3bd194e172675 | |
parent | e821861383663dec8d56782089f19a0b159d7c9d (diff) | |
download | uhd-738b9ec102cdb307413cab23fdcf7fb2522f9895.tar.gz uhd-738b9ec102cdb307413cab23fdcf7fb2522f9895.tar.bz2 uhd-738b9ec102cdb307413cab23fdcf7fb2522f9895.zip |
mpm: Add convenience function to pull i2c bus from device tree
This function grabs the i2c character device path from the OF_NAME
property. That property must be unique in the device tree!
-rw-r--r-- | mpm/python/usrp_mpm/sys_utils/CMakeLists.txt | 1 | ||||
-rw-r--r-- | mpm/python/usrp_mpm/sys_utils/i2c_dev.py | 36 |
2 files changed, 37 insertions, 0 deletions
diff --git a/mpm/python/usrp_mpm/sys_utils/CMakeLists.txt b/mpm/python/usrp_mpm/sys_utils/CMakeLists.txt index ee7c249df..e1d830c94 100644 --- a/mpm/python/usrp_mpm/sys_utils/CMakeLists.txt +++ b/mpm/python/usrp_mpm/sys_utils/CMakeLists.txt @@ -8,6 +8,7 @@ SET(USRP_MPM_FILES ${USRP_MPM_FILES}) SET(USRP_MPM_SYSUTILS_FILES ${CMAKE_CURRENT_SOURCE_DIR}/__init__.py ${CMAKE_CURRENT_SOURCE_DIR}/dtoverlay.py + ${CMAKE_CURRENT_SOURCE_DIR}/i2c_dev.py ${CMAKE_CURRENT_SOURCE_DIR}/net.py ${CMAKE_CURRENT_SOURCE_DIR}/sysfs_gpio.py ${CMAKE_CURRENT_SOURCE_DIR}/sysfs_thermal.py diff --git a/mpm/python/usrp_mpm/sys_utils/i2c_dev.py b/mpm/python/usrp_mpm/sys_utils/i2c_dev.py new file mode 100644 index 000000000..7da5177f8 --- /dev/null +++ b/mpm/python/usrp_mpm/sys_utils/i2c_dev.py @@ -0,0 +1,36 @@ +# +# Copyright 2018 Ettus Research, a National Instruments Company +# +# SPDX-License-Identifier: GPL-3.0-or-later +# +""" +Utilities for i2c lookups +""" + +import pyudev + +def of_get_i2c_adapter(of_name): + """ + Return bus adapter device for given device tree name. To use the OF_NAME + for matching (as this function does), it must be a unique property in the + device tree (and this must be a device-tree platform). + + The return value is a string, e.g. '/dev/i2c-0'. If nothing is found, it'll + return None. + """ + # If has i2c-dev, follow to grab i2c-%d node + context = pyudev.Context() + parents = list(context.list_devices(subsystem="i2c-adapter", OF_NAME=of_name)) + if len(parents) > 1: + raise RuntimeError("Non-unique OF_NAME when getting i2c bus id") + if len(parents) == 0: + return None + parent = parents[0] + if not parent: + return None + devices = list(context.list_devices(parent=parent, subsystem="i2c-dev")) + properties = [dict(d.properties) for d in devices] + # The i2c-adapter will have the lowest minor number + # FIXME: This likely isn't API--It just happens to work as of this writing + chosen = min([(int(p['MINOR']), p['DEVNAME']) for p in properties]) + return chosen[1] |