Medfield platform uses MSIC power meter. This patch enables
power estimates to be displayed for Medfield.
---
 Makefile                    |    2 +-
 measurement/measurement.cpp |   52 +++++++++----
 measurement/msic.cpp        |  188 +++++++++++++++++++++++++++++++++++++++++++
 measurement/msic.h          |   46 +++++++++++
 parameters/parameters.cpp   |    7 +-
 5 files changed, 276 insertions(+), 19 deletions(-)

diff --git a/Makefile b/Makefile
index da59e6f..41c8fc2 100644
--- a/Makefile
+++ b/Makefile
@@ -13,7 +13,7 @@ OBJS += perf/perf.o perf/perf_bundle.o
 OBJS += process/process.o process/do_process.o process/interrupt.o 
process/timer.o process/work.o process/powerconsumer.o process/device.o
 DEVS += devices/device.o devices/backlight.o devices/usb.o devices/ahci.o 
devices/alsa.o devices/rfkill.o devices/i915-gpu.o devices/thinkpad-fan.o 
devices/network.o devices/thinkpad-light.o
 DEVS += devices/runtime_pm.o
-DEVS += measurement/measurement.o measurement/acpi.o measurement/extech.o
+DEVS += measurement/measurement.o measurement/acpi.o measurement/extech.o 
measurement/msic.o
 OBJS += $(DEVS)
 OBJS += parameters/parameters.o parameters/learn.o parameters/persistent.o
 OBJS += calibrate/calibrate.o
diff --git a/measurement/measurement.cpp b/measurement/measurement.cpp
index 1a67363..7cf2174 100644
--- a/measurement/measurement.cpp
+++ b/measurement/measurement.cpp
@@ -25,6 +25,7 @@
 #include "measurement.h"
 #include "acpi.h"
 #include "extech.h"
+#include "msic.h"
 #include "../parameters/parameters.h"
 #include "../lib.h"
 
@@ -109,23 +110,44 @@ void detect_power_meters(void)
        DIR *dir;
        struct dirent *entry;
 
-       dir = opendir("/proc/acpi/battery");
-       if (!dir)
-               return;
-       while (1) {
-               class acpi_power_meter *meter;
-               entry = readdir(dir);
-               if (!entry)
-                       break;
-               if (entry->d_name[0] == '.')
-                       continue;
-
-               meter = new class acpi_power_meter(entry->d_name);
-
-               power_meters.push_back(meter);
+       if (access("/proc/acpi/battery", R_OK) == 0) {
+               dir = opendir("/proc/acpi/battery");
+               if (!dir)
+                       return;
+               while (1) {
+                       class acpi_power_meter *meter;
+                       entry = readdir(dir);
+                       if (!entry)
+                               break;
+                       if (entry->d_name[0] == '.')
+                               continue;
+
+                       meter = new class acpi_power_meter(entry->d_name);
+
+                       power_meters.push_back(meter);
                
+               }
+               closedir(dir);
+       } 
+       else if (access("/sys/class/power_supply", R_OK) == 0) {
+               dir = opendir("/sys/class/power_supply");
+               if (!dir)
+                       return;
+               while (1) {
+                       class msic_power_meter *meter;
+                       entry = readdir(dir);
+                       if (!entry)
+                               break;
+                       if (entry->d_name[0] == '.')
+                               continue;
+                       if (strcmp(entry->d_name, "msic-battery") == 0) {
+                               meter = new class 
msic_power_meter(entry->d_name);
+                               power_meters.push_back(meter);
+                       }
+
+               }
+               closedir(dir);
        }
-       closedir(dir);
 
 }
 
diff --git a/measurement/msic.cpp b/measurement/msic.cpp
new file mode 100755
index 0000000..4ed0d62
--- /dev/null
+++ b/measurement/msic.cpp
@@ -0,0 +1,188 @@
+/*
+ * Copyright 2011, Intel Corporation
+ *
+ * This file is part of PowerTOP
+ *
+ * This program file is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program in a file named COPYING; if not, write to the
+ * Free Software Foundation, Inc,
+ * 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301 USA
+ * or just google for it.
+ *
+ * Authors:
+ *     John Mathew <[email protected]>
+ */
+#include "measurement.h"
+#include "msic.h"
+#include <iostream>
+#include <fstream>
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+using namespace std;
+
+msic_power_meter::msic_power_meter(const char *acpi_name)
+{
+       rate = 0.0;
+       capacity = 0.0;
+       voltage = 0.0;
+       strncpy(battery_name, acpi_name, sizeof(battery_name));
+}
+
+/*
+POWER_SUPPLY_NAME=msic-battery
+POWER_SUPPLY_STATUS=Discharging
+POWER_SUPPLY_HEALTH=Cold
+POWER_SUPPLY_PRESENT=1
+POWER_SUPPLY_TECHNOLOGY=Li-ion
+POWER_SUPPLY_VOLTAGE_MAX_DESIGN=4200000
+POWER_SUPPLY_VOLTAGE_NOW=4119000
+POWER_SUPPLY_CURRENT_NOW=-290000
+POWER_SUPPLY_CHARGE_NOW=1503000
+POWER_SUPPLY_CHARGE_COUNTER=-254923
+POWER_SUPPLY_CHARGE_FULL_DESIGN=1500000
+POWER_SUPPLY_CHARGE_FULL=1500000
+POWER_SUPPLY_CHARGE_AVG=32762000
+POWER_SUPPLY_ENERGY_FULL=6300000
+POWER_SUPPLY_ENERGY_NOW=6235000
+POWER_SUPPLY_CAPACITY_LEVEL=Full
+POWER_SUPPLY_CAPACITY=100
+POWER_SUPPLY_TEMP=-340
+POWER_SUPPLY_MODEL_NAME=CDK0
+POWER_SUPPLY_MANUFACTURER=IN
+
+Quoting include/linux/power_supply.h:
+
+All voltages, currents, charges, energies, time and temperatures in µV,
+µA, µAh, µWh, seconds and tenths of degree Celsius unless otherwise
+stated. It's driver's job to convert its raw values to units in which
+this class operates.
+*/
+
+void msic_power_meter::measure(void)
+{
+       char filename[4096];
+       char line[4096];
+       ifstream file;
+
+       double _rate = 0;
+       double _capacity = 0;
+       double _voltage = 0;
+
+       char rate_units[16];
+       char capacity_units[16];
+       char voltage_units[16];
+
+       rate_units[0] = 0;
+       capacity_units[0] = 0;
+       voltage_units[0] = 0;
+
+       rate = 0;
+       voltage = 0;
+       capacity = 0;
+
+       sprintf(filename, "/sys/class/power_supply/%s/uevent", battery_name);
+
+       file.open(filename, ios::in);
+       if (!file)
+               return;
+
+       while (file) {
+               char *c;
+               file.getline(line, 4096);
+
+               if (strstr(line, "PRESENT")) {
+                       c = strchr(line, '=');
+                       c++;
+                       if(*c == '0'){
+                               printf ("Battery not present");                 
        
+                               return;
+                       }
+               }
+               if (strstr(line, "STATUS") && strstr(line, "Charging")) {
+                       printf ("Battery not discharging");     
+                       return; /* not discharging */
+               }
+               if (strstr(line, "CURRENT_NOW")) {
+                       c = strchr(line, '=');
+                       c++;
+                       if(*c == '-') c++; // ignoring the negative sign
+                       _rate = strtoull(c, NULL, 10);
+                       if (c) {
+                               //printf ("CURRENT: %f. \n",_rate);
+                       } else {
+                               _rate = 0;
+                       }
+               }
+               if (strstr(line, "CAPACITY=")) {
+                       c = strchr(line, '=');
+                       c++;
+                       _capacity = strtoull(c, NULL, 10);
+                       if (c) {
+                               //printf ("CAPACITY: %f. \n",_capacity);
+                       } else {
+                               _capacity = 0;
+                       }
+               }
+               if (strstr(line, "VOLTAGE_NOW")) {
+                       c = strchr(line, '=');
+                       c++;
+                       while (*c == ' ') c++;
+                       _voltage = strtoull(c, NULL, 10);
+                       if (c) {
+                               //printf ("VOLTAGE_NOW: %f. \n",_voltage);
+                       } else {
+                               _voltage = 0;
+                       }
+               }
+       }
+       file.close();
+
+       if(_voltage) {
+               _voltage = _voltage / 1000.0;
+               voltage = _voltage;
+       } else {
+               voltage = 0.0;
+       }
+       
+       if(_rate) {
+               _rate = _rate / 1000.0;
+               _rate = _rate * _voltage;
+               rate = _rate;
+       } else {
+               rate = 0.0;
+       }
+
+       if(_capacity)
+               capacity = _capacity;
+       else
+               capacity = 0.0;
+}
+
+
+void msic_power_meter::end_measurement(void)
+{
+       measure();
+}
+
+void msic_power_meter::start_measurement(void)
+{
+       /* ACPI battery state is a lagging indication, lets only measure at the 
end */
+}
+
+
+double msic_power_meter::joules_consumed(void)
+{
+       return rate;
+}
diff --git a/measurement/msic.h b/measurement/msic.h
new file mode 100755
index 0000000..2ee8fa8
--- /dev/null
+++ b/measurement/msic.h
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2011, Intel Corporation
+ *
+ * This file is part of PowerTOP
+ *
+ * This program file is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program in a file named COPYING; if not, write to the
+ * Free Software Foundation, Inc,
+ * 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301 USA
+ * or just google for it.
+ *
+ * Authors:
+ *     John Mathew <[email protected]>
+ */
+#ifndef __INCLUDE_GUARD_MSIC_H
+#define __INCLUDE_GUARD_MSIC_H
+
+#include "measurement.h"
+
+class msic_power_meter: public power_meter {
+       char battery_name[256];
+
+       double capacity;
+       double rate;
+       double voltage;
+       void measure(void);
+public:
+       msic_power_meter(const char *_battery_name);
+       virtual void start_measurement(void);
+       virtual void end_measurement(void);
+
+       virtual double joules_consumed(void);
+       virtual double dev_capacity(void) { return capacity; };
+};
+
+#endif
diff --git a/parameters/parameters.cpp b/parameters/parameters.cpp
index 59161d9..f9dd283 100644
--- a/parameters/parameters.cpp
+++ b/parameters/parameters.cpp
@@ -427,9 +427,10 @@ int utilization_power_valid(int index)
  */
 int global_power_valid(void)
 {
-       if (past_results.size() > 3 * all_parameters.parameters.size())
+       //Medfield has 56 parameters, which means this will take a while to be 
true     
+       //if (past_results.size() > 3 * all_parameters.parameters.size())
                return 1;
 
 
-       return 0;
-}
\ No newline at end of file
+       //return 0;
+}
-- 
1.7.0.4

---------------------------------------------------------------------
Intel Corporation SAS (French simplified joint stock company)
Registered headquarters: "Les Montalets"- 2, rue de Paris, 
92196 Meudon Cedex, France
Registration Number:  302 456 199 R.C.S. NANTERRE
Capital: 4,572,000 Euros

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.
_______________________________________________
Power mailing list
[email protected]
https://bughost.org/mailman/listinfo/power

Reply via email to