power: charger-gauge-comm: API for voltage and capacity snapshot
Darbha Sriharsha [Mon, 23 Dec 2013 09:07:14 +0000 (14:07 +0530)]
Add API to record a snapshot of voltage and capacity readings at
any instant and store it in specific sysfs entries.

Bug 1291416

Change-Id: I7cd6c1cd364acf0c00074a193edf1bdd5b9ed158
Signed-off-by: Darbha Sriharsha <dsriharsha@nvidia.com>
Reviewed-on: http://git-master/r/349430
GVS: Gerrit_Virtual_Submit
Reviewed-by: Laxman Dewangan <ldewangan@nvidia.com>

drivers/power/battery-charger-gauge-comm.c
include/linux/power/battery-charger-gauge-comm.h

index 522db79..3df6f4e 100644 (file)
@@ -44,6 +44,8 @@
 #define JETI_TEMP_WARM         45
 #define JETI_TEMP_HOT          60
 
+#define MAX_STR_PRINT          50
+
 static DEFINE_MUTEX(charger_gauge_list_mutex);
 static LIST_HEAD(charger_list);
 static LIST_HEAD(gauge_list);
@@ -73,8 +75,14 @@ struct battery_gauge_dev {
        struct list_head                list;
        void                            *drv_data;
        struct thermal_zone_device      *battery_tz;
+       int                             battery_voltage;
+       int                             battery_capacity;
+       int                             battery_snapshot_voltage;
+       int                             battery_snapshot_capacity;
 };
 
+struct battery_gauge_dev *bg_temp;
+
 static void battery_charger_restart_charging_wq(struct work_struct *work)
 {
        struct battery_charger_dev *bc_dev;
@@ -228,6 +236,80 @@ int battery_charging_restart(struct battery_charger_dev *bc_dev, int after_sec)
 }
 EXPORT_SYMBOL_GPL(battery_charging_restart);
 
+static ssize_t battery_show_snapshot_voltage(struct device *dev,
+                               struct device_attribute *attr,
+                               char *buf)
+{
+       struct battery_gauge_dev *bg_dev = bg_temp;
+
+       return snprintf(buf, MAX_STR_PRINT, "%d\n",
+                               bg_dev->battery_snapshot_voltage);
+}
+
+static ssize_t battery_show_snapshot_capacity(struct device *dev,
+                               struct device_attribute *attr,
+                               char *buf)
+{
+       struct battery_gauge_dev *bg_dev = bg_temp;
+
+       return snprintf(buf, MAX_STR_PRINT, "%d\n",
+                               bg_dev->battery_snapshot_capacity);
+}
+
+static DEVICE_ATTR(battery_snapshot_voltage, S_IRUGO,
+               battery_show_snapshot_voltage, NULL);
+
+static DEVICE_ATTR(battery_snapshot_capacity, S_IRUGO,
+               battery_show_snapshot_capacity, NULL);
+
+static struct attribute *battery_snapshot_attributes[] = {
+       &dev_attr_battery_snapshot_voltage.attr,
+       &dev_attr_battery_snapshot_capacity.attr,
+       NULL
+};
+
+static const struct attribute_group battery_snapshot_attr_group = {
+       .attrs = battery_snapshot_attributes,
+};
+
+int battery_gauge_record_voltage_value(struct battery_gauge_dev *bg_dev,
+                                                       int voltage)
+{
+       if (!bg_dev)
+               return -EINVAL;
+
+       bg_dev->battery_voltage = voltage;
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(battery_gauge_record_voltage_value);
+
+int battery_gauge_record_capacity_value(struct battery_gauge_dev *bg_dev,
+                                                       int capacity)
+{
+       if (!bg_dev)
+               return -EINVAL;
+
+       bg_dev->battery_capacity = capacity;
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(battery_gauge_record_capacity_value);
+
+int battery_gauge_record_snapshot_values(struct battery_gauge_dev *bg_dev,
+                                               int interval)
+{
+       msleep(interval);
+       if (!bg_dev)
+               return -EINVAL;
+
+       bg_dev->battery_snapshot_voltage = bg_dev->battery_voltage;
+       bg_dev->battery_snapshot_capacity = bg_dev->battery_capacity;
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(battery_gauge_record_snapshot_values);
+
 void battery_charging_restart_cancel(struct battery_charger_dev *bc_dev)
 {
        if (!bc_dev->ops->restart_charging) {
@@ -408,6 +490,7 @@ struct battery_gauge_dev *battery_gauge_register(struct device *dev,
        struct battery_gauge_info *bgi, void *drv_data)
 {
        struct battery_gauge_dev *bg_dev;
+       int ret;
 
        dev_info(dev, "Registering battery gauge driver\n");
 
@@ -422,6 +505,10 @@ struct battery_gauge_dev *battery_gauge_register(struct device *dev,
                return ERR_PTR(-ENOMEM);
        }
 
+       ret = sysfs_create_group(&dev->kobj, &battery_snapshot_attr_group);
+       if (ret < 0)
+               dev_info(dev, "Could not create battery snapshot sysfs group\n");
+
        mutex_lock(&charger_gauge_list_mutex);
 
        INIT_LIST_HEAD(&bg_dev->list);
@@ -436,6 +523,8 @@ struct battery_gauge_dev *battery_gauge_register(struct device *dev,
                        bg_dev->tz_name);
        list_add(&bg_dev->list, &gauge_list);
        mutex_unlock(&charger_gauge_list_mutex);
+       bg_temp = bg_dev;
+
        return bg_dev;
 }
 EXPORT_SYMBOL_GPL(battery_gauge_register);
index f102ddb..3988f82 100644 (file)
@@ -91,5 +91,14 @@ void battery_charger_set_drvdata(struct battery_charger_dev *bc_dev,
                        void *data);
 void *battery_gauge_get_drvdata(struct battery_gauge_dev *bg_dev);
 void battery_gauge_set_drvdata(struct battery_gauge_dev *bg_dev, void *data);
+int battery_gauge_record_voltage_value(struct battery_gauge_dev *bg_dev,
+                                                               int voltage);
+int battery_gauge_record_capacity_value(struct battery_gauge_dev *bg_dev,
+                                                               int capacity);
+int battery_gauge_record_snapshot_values(struct battery_gauge_dev *bg_dev,
+                                                               int interval);
+
+
+
 
 #endif /* _LINUX_POWER_BATTERY_CHARGER_GAUGE_COMM_H */