intel_edison_fab_c.c: Fix frequency setting on edison r2 images
authorBrendan Le Foll <brendan.le.foll@intel.com>
Sun, 1 Mar 2015 17:15:19 +0000 (17:15 +0000)
committerBrendan Le Foll <brendan.le.foll@intel.com>
Sun, 1 Mar 2015 17:17:53 +0000 (17:17 +0000)
This reworks the mraa_intel_edison_i2c_freq() function. i2c_dw_sysnode is a
folder not a file so we need to grab the mode file from that folder. We now
also support i2c-1 frequency changing

Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
src/x86/intel_edison_fab_c.c

index e745af7..204d698 100644 (file)
@@ -674,33 +674,46 @@ mraa_intel_edison_mmap_setup(mraa_gpio_context dev, mraa_boolean_t en)
 mraa_result_t
 mraa_intel_edison_i2c_freq(mraa_i2c_context dev, mraa_i2c_mode_t mode)
 {
-    if (dev->busnum == 6) {
-        int sysnode = open("/sys/devices/pci0000:00/0000:00:09.1/i2c_dw_sysnode", O_RDWR);
-        if (sysnode == -1) {
-            return MRAA_ERROR_INVALID_RESOURCE;
-        }
+    int sysnode = -1;
 
-        char bu[5];
-        int length;
-        switch (mode) {
-            case MRAA_I2C_STD:
-                length = snprintf(bu, sizeof(bu), "std");
-                break;
-            case MRAA_I2C_FAST:
-                length = snprintf(bu, sizeof(bu), "fast");
-                break;
-            case MRAA_I2C_HIGH:
-                length = snprintf(bu, sizeof(bu), "high");
-                break;
-        }
-        if (write(sysnode, bu, length*sizeof(char)) == -1) {
+    switch (dev->busnum) {
+        case 1:
+            sysnode = open("/sys/devices/pci0000:00/0000:00:08.0/i2c_dw_sysnode/mode", O_RDWR);
+            break;
+        case 6:
+            sysnode = open("/sys/devices/pci0000:00/0000:00:09.1/i2c_dw_sysnode/mode", O_RDWR);
+            break;
+        default:
+            syslog(LOG_NOTICE, "i2c bus selected does not support frequency changes");
+            return MRAA_ERROR_FEATURE_NOT_SUPPORTED;
+    }
+    if (sysnode == -1) {
+        return MRAA_ERROR_INVALID_RESOURCE;
+    }
+
+    char bu[5];
+    int length;
+    switch (mode) {
+        case MRAA_I2C_STD:
+            length = snprintf(bu, sizeof(bu), "std");
+            break;
+        case MRAA_I2C_FAST:
+            length = snprintf(bu, sizeof(bu), "fast");
+            break;
+        case MRAA_I2C_HIGH:
+            length = snprintf(bu, sizeof(bu), "high");
+            break;
+        default:
+            syslog(LOG_ERR, "Invalid i2c mode selected");
             close(sysnode);
-            return MRAA_ERROR_INVALID_RESOURCE;
-        }
+            return MRAA_ERROR_INVALID_PARAMETER;
+    }
+    if (write(sysnode, bu, length*sizeof(char)) == -1) {
         close(sysnode);
-        return MRAA_SUCCESS;
+        return MRAA_ERROR_INVALID_RESOURCE;
     }
-    return MRAA_ERROR_FEATURE_NOT_SUPPORTED;
+    close(sysnode);
+    return MRAA_SUCCESS;
 }
 
 mraa_result_t