Whale: add ADC driver

BUG=chrome-os-partner:28870
TEST=Verify on Larvae board
  Usage:
    dut-control whale_adc

Change-Id: Ib52f88d6d375d8ec588f0f37c9306b6cc74b0e11
Reviewed-on: https://chromium-review.googlesource.com/210461
Reviewed-by: Dean Liao <deanliao@chromium.org>
Reviewed-by: Todd Broch <tbroch@chromium.org>
Commit-Queue: Aaron Chuang <aaron.chuang@quantatw.com>
Tested-by: Aaron Chuang <aaron.chuang@quantatw.com>
diff --git a/servo/bbadc.py b/servo/bbadc.py
new file mode 100644
index 0000000..dd4cddc
--- /dev/null
+++ b/servo/bbadc.py
@@ -0,0 +1,50 @@
+# Copyright 2014 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+"""Allows creation of ADC interface for beaglebone devices."""
+import glob
+import os
+
+
+class BBadcError(Exception):
+  """Class for exceptions of BBadc."""
+
+  def __init__(self, msg, value=0):
+    """BBadc constructor.
+
+    Args:
+      msg: string, message describing error in detail
+      value: integer, value of error when non-zero status returned.  Default=0
+    """
+    super(BBAdcError, self).__init__(msg, value)
+    self.msg = msg
+    self.value = value
+
+
+class BBadc(object):
+  """Provides interface to ADC through beaglebone."""
+
+  ADC_ENABLE_COMMAND = 'echo cape-bone-iio > %s'
+  ADC_ENABLE_NODE = '/sys/devices/bone_capemgr.*/slots'
+  ADC_IN_NODE = '/sys/devices/ocp.*/helper.*/AIN*'
+
+  def __init__(self):
+    """Enables ADC drvier."""
+    adc_nodes = glob.glob(BBadc.ADC_ENABLE_NODE)
+    for adc_node in adc_nodes:
+      os.system(BBadc.ADC_ENABLE_COMMAND % adc_node)
+
+  def read(self):
+    """Reads ADC values.
+
+    Returns:
+      ADC inputs from AIN0 to AIN7.
+    """
+    buffer = []
+    adc_in_nodes = glob.glob(BBadc.ADC_IN_NODE)
+    for adc_in in adc_in_nodes:
+      with open(adc_in, 'r') as f:
+        buffer.append(int(f.read(), 10))
+
+    return buffer
diff --git a/servo/data/servo_whale_samus_overlay.xml b/servo/data/servo_whale_samus_overlay.xml
index 2da8bb9..585a448 100644
--- a/servo/data/servo_whale_samus_overlay.xml
+++ b/servo/data/servo_whale_samus_overlay.xml
@@ -461,4 +461,10 @@
     <params cmd="set" subtype="data" interface="4" drv="m24c02"
             slv="0x50" input_type="str"></params>
   </control>
+  <!-- ADC -->
+  <control>
+    <name>whale_adc</name>
+    <doc>Read ADC inputs</doc>
+    <params cmd="get" interface="5" drv="larvae_adc"></params>
+  </control>
 </root>
diff --git a/servo/drv/__init__.py b/servo/drv/__init__.py
index 3c07b64..4f5ac56 100644
--- a/servo/drv/__init__.py
+++ b/servo/drv/__init__.py
@@ -21,6 +21,7 @@
 import ina2xx
 import ina219
 import ina231
+import larvae_adc
 import lcm2004
 import ltc1663
 import link_power
diff --git a/servo/drv/larvae_adc.py b/servo/drv/larvae_adc.py
new file mode 100644
index 0000000..a0933d5
--- /dev/null
+++ b/servo/drv/larvae_adc.py
@@ -0,0 +1,28 @@
+# Copyright 2014 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+"""ADC driver.
+
+It returns Larvae's ADC sensors' value (AIN0 to AIN6).
+"""
+
+# servo libs
+import hw_driver
+
+
+class larvaeAdc(hw_driver.HwDriver):
+  """Reads ADC inputs."""
+
+  def __init__(self, interface, params):
+    """Constructor."""
+    super(larvaeAdc, self).__init__(interface, params)
+
+  def get(self):
+    """Reads ADC inputs.
+
+    Returns:
+      ADC values from AIN0 to AIN6.
+    """
+    buffer = self._interface.read()
+    return buffer[0:7]
diff --git a/servo/servo_interfaces.py b/servo/servo_interfaces.py
index 7818194..47f02ff 100644
--- a/servo/servo_interfaces.py
+++ b/servo/servo_interfaces.py
@@ -30,7 +30,7 @@
      {'name': 'bb_uart', 'uart_num': 5,
       'txd': ['lcd_data8', 0x4], 'rxd': ['lcd_data9', 0x4]},
      {'name': 'bb_i2c', 'bus_num': 2},
-     'dummy',
+     'bb_adc',
      'dummy',
      {'name': 'bb_uart', 'uart_num': 1},
      {'name': 'bb_uart', 'uart_num': 2}]
diff --git a/servo/servo_server.py b/servo/servo_server.py
index 4d489f2..70636ea 100644
--- a/servo/servo_server.py
+++ b/servo/servo_server.py
@@ -15,6 +15,7 @@
 
 # TODO(tbroch) deprecate use of relative imports
 from drv.hw_driver import HwDriverError
+import bbadc
 import bbi2c
 import bbgpio
 import bbuart
@@ -181,6 +182,10 @@
 
     return fobj
 
+  def _init_bb_adc(self, interface):
+    """Initalize beaglebone ADC interface."""
+    return bbadc.BBadc()
+
   def _init_bb_gpio(self, interface):
     """Initalize beaglebone gpio interface."""
     return bbgpio.BBgpio()