Add pwm id as fan pwm target

The current code assumes fan and pwm has the same id.

This commit add the support to use pwmX as fan pwm target by
PWM_TARGET_ environment.

E.g.
PWM_TARGET_fan0 = "1"
PWM_TARGET_fan1 = "1"
PWM_TARGET_fan2 = "2"
PWM_TARGET_fan3 = "2"

The above config tells hwmon to set pwm1 for fan0/1's pwm target,
and set pwm2 for fan2/3's pwm target.

Change-Id: Ic1d850ca3d6d20274d905fa9004d7eba2718e9eb
Signed-off-by: Lei YU <mine260309@gmail.com>
diff --git a/targets.hpp b/targets.hpp
index b671467..de86170 100644
--- a/targets.hpp
+++ b/targets.hpp
@@ -65,6 +65,9 @@
 
     // Check if target sysfs file exists
     std::string sysfsFullPath;
+    std::string targetName = sensor.first;
+    std::string targetId = sensor.second;
+    std::string entry = hwmon::entry::target;
 
     using namespace std::literals;
     const std::string pwm = "pwm"s;
@@ -72,47 +75,32 @@
 
     if (InterfaceType::FAN_PWM == type)
     {
-        /* We're leveraging that the sensor ID matches for PWM.
-         * TODO(venture): There's a CL from intel that allows
-         * this to be specified via an environment variable.
-         */
-        sysfsFullPath = sysfs::make_sysfs_path(ioAccess.path(),
-                                               pwm,
-                                               sensor.second,
-                                               empty);
-    }
-    else
-    {
-        sysfsFullPath = sysfs::make_sysfs_path(ioAccess.path(),
-                                               sensor.first,
-                                               sensor.second,
-                                               hwmon::entry::target);
+        targetName = pwm;
+        // If PWM_TARGET is set, use the specified pwm id
+        auto id = getEnv("PWM_TARGET", sensor);
+        if (!id.empty())
+        {
+            targetId = id;
+        }
+        entry = empty;
     }
 
+    sysfsFullPath = sysfs::make_sysfs_path(ioAccess.path(),
+                                           targetName,
+                                           targetId,
+                                           entry);
     if (fs::exists(sysfsFullPath))
     {
         uint32_t targetSpeed = 0;
 
         try
         {
-            if (InterfaceType::FAN_PWM == type)
-            {
-                targetSpeed = ioAccess.read(
-                        pwm,
-                        sensor.second,
-                        empty,
-                        sysfs::hwmonio::retries,
-                        sysfs::hwmonio::delay);
-            }
-            else
-            {
-                targetSpeed = ioAccess.read(
-                        sensor.first,
-                        sensor.second,
-                        hwmon::entry::target,
-                        sysfs::hwmonio::retries,
-                        sysfs::hwmonio::delay);
-            }
+            targetSpeed = ioAccess.read(
+                targetName,
+                targetId,
+                entry,
+                sysfs::hwmonio::retries,
+                sysfs::hwmonio::delay);
         }
         catch (const std::system_error& e)
         {
@@ -133,12 +121,11 @@
 
         target = std::make_shared<T>(ioAccess.path(),
                                      devPath,
-                                     sensor.second,
+                                     targetId,
                                      bus,
                                      objPath.c_str(),
                                      deferSignals,
                                      targetSpeed);
-        auto type = Targets<T>::type;
         obj[type] = target;
     }