diff options
| author | Toni Jones <toni.jones@ni.com> | 2019-02-12 15:57:11 -0600 | 
|---|---|---|
| committer | Aaron Rossetto <aaron.rossetto@ni.com> | 2021-03-08 16:17:48 -0600 | 
| commit | 909b434e1ee6eb3797f5c070deaadfef34f11853 (patch) | |
| tree | f9a8cb06c0ba1e70e426eeb160f18da17b7c45f4 /mpm/python | |
| parent | 5070256a0ac171759b83e188c3a2ba578e00d381 (diff) | |
| download | uhd-909b434e1ee6eb3797f5c070deaadfef34f11853.tar.gz uhd-909b434e1ee6eb3797f5c070deaadfef34f11853.tar.bz2 uhd-909b434e1ee6eb3797f5c070deaadfef34f11853.zip | |
mpm: Add i2c_dev lookup using sys_name
Add i2c_dev adapter device lookup which uses a the sys_name value
instead of OF_NAME to find the adapter. OF_NAME is not unique for some
i2c device nodes. The logic for finding the adapter from the parent
node was pulled into a helper function and is shared across both
lookup functions.
Co-authored-by: Michael Auchter <michael.auchter@ni.com>
Co-authored-by: Toni Jones <toni.jones@ni.com>
Diffstat (limited to 'mpm/python')
| -rw-r--r-- | mpm/python/usrp_mpm/sys_utils/i2c_dev.py | 58 | 
1 files changed, 51 insertions, 7 deletions
| diff --git a/mpm/python/usrp_mpm/sys_utils/i2c_dev.py b/mpm/python/usrp_mpm/sys_utils/i2c_dev.py index 7da5177f8..b84086e55 100644 --- a/mpm/python/usrp_mpm/sys_utils/i2c_dev.py +++ b/mpm/python/usrp_mpm/sys_utils/i2c_dev.py @@ -8,6 +8,21 @@ Utilities for i2c lookups  """  import pyudev +from usrp_mpm.sys_utils.udev import get_device_from_dt_symbol + +def _get_i2c_adapter_from_parent(parent, context): +    """ +    Helper to get the i2c adapter from a given parent device. +    This logic is common for different device lookup methods. +    """ +    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]  def of_get_i2c_adapter(of_name):      """ @@ -26,11 +41,40 @@ def of_get_i2c_adapter(of_name):      if len(parents) == 0:          return None      parent = parents[0] -    if not parent: +    return _get_i2c_adapter_from_parent(parent, context) + + +def dt_symbol_get_i2c_bus(symbol): +    """ +    Return i2c bus associated with the given device tree symbol + +    The return value is a string, e.g. '/dev/i2c-0'. If nothing is found, it'll +    return None. +    """ +    context = pyudev.Context() +    parent = get_device_from_dt_symbol(symbol, subsystem='i2c', context=context) +    return _get_i2c_adapter_from_parent(parent, context) + + +def sysname_get_i2c_adapter(sys_name): +    """ +    Return bus adapter device for given device tree sys_name. + +    The return value is a string, e.g. '/dev/i2c-0'. If nothing is found, it'll +    return None. +    """ +    parent = sysname_get_i2c_parent(sys_name) +    return _get_i2c_adapter_from_parent(parent, pyudev.Context()) + +def sysname_get_i2c_parent(sys_name): +    """ +    Returns the parent of an i2c adapter found by sys_name +    """ +    # If has i2c-dev, follow to grab i2c-%d node +    context = pyudev.Context() +    parents = list(context.list_devices(subsystem="i2c-adapter", sys_name=sys_name)) +    if len(parents) > 1: +        raise RuntimeError("Non-unique sys_name when getting i2c bus id") +    if len(parents) == 0:          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] +    return parents[0] | 
