[PATCHv4 6/9] Thermal: Create Thermal map sysfs attributes for a zone

2013-10-01 Thread Durgadoss R
This patch creates a thermal map sysfs node under
/sys/class/thermal/zoneX/. The thermal map
shows the binding relationship between a sensor
and a cooling device within a particular zone.
This contains entries named mapY_trip_type,
mapY_sensor_name, mapY_cdev_name, mapY_trip_mask,
mapY_weightX.

Signed-off-by: Durgadoss R 
---
 drivers/thermal/thermal_core.c |  252 +++-
 include/linux/thermal.h|   26 +
 2 files changed, 277 insertions(+), 1 deletion(-)

diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c
index d6e29f6..e1289a4 100644
--- a/drivers/thermal/thermal_core.c
+++ b/drivers/thermal/thermal_core.c
@@ -548,6 +548,24 @@ static void __remove_trip_attr(struct thermal_zone *tz, 
int indx)
tz->trip_attr[indx] = NULL;
 }
 
+static void __remove_map_entry(struct thermal_zone *tz, int indx)
+{
+   int i;
+   struct thermal_map_attr *attr;
+
+   attr = tz->map_attr[indx];
+
+   for (i = 0; i < NUM_MAP_ATTRS; i++)
+   device_remove_file(>device, >attrs[i].attr);
+
+   for (i = 0; i < tz->map[indx]->num_weights; i++)
+   device_remove_file(>device, >weights_attr[i].attr);
+
+   kfree(tz->map_attr[indx]);
+   tz->map_attr[indx] = NULL;
+   tz->map[indx] = NULL;
+}
+
 static void remove_sensor_from_zone(struct thermal_zone *tz,
struct thermal_sensor *ts)
 {
@@ -572,6 +590,14 @@ static void remove_sensor_from_zone(struct thermal_zone 
*tz,
}
 
tz->sensor_indx--;
+
+   /* Remove all mappings associated with this sensor */
+   for (j = 0; j < MAX_MAPS_PER_ZONE; j++) {
+   if (tz->map[j] && !strnicmp(ts->name, tz->map[j]->sensor_name,
+   THERMAL_NAME_LENGTH)) {
+   __remove_map_entry(tz, j);
+   }
+   }
mutex_unlock(>lock);
 }
 
@@ -593,6 +619,14 @@ static void remove_cdev_from_zone(struct thermal_zone *tz,
tz->cdevs[j] = tz->cdevs[j + 1];
 
tz->cdev_indx--;
+
+   /* Remove all mappings associated with this sensor */
+   for (j = 0; j < MAX_MAPS_PER_ZONE; j++) {
+   if (tz->map[j] && !strnicmp(cdev->type, tz->map[j]->cdev_name,
+   THERMAL_NAME_LENGTH)) {
+   __remove_map_entry(tz, j);
+   }
+   }
mutex_unlock(>lock);
 }
 
@@ -1117,6 +1151,126 @@ critical_trip_show(struct device *dev,
return sprintf(buf, "%d\n", val);
 }
 
+static ssize_t
+map_ttype_show(struct device *dev, struct device_attribute *attr, char *buf)
+{
+   int indx, ret = -EINVAL;
+   struct thermal_zone *tz = to_zone(dev);
+
+   if (!sscanf(attr->attr.name, "map%d_trip_type", ))
+   return -EINVAL;
+
+   if (indx < 0 || indx >= MAX_MAPS_PER_ZONE)
+   return -EINVAL;
+
+   mutex_lock(>lock);
+
+   if (!tz->map[indx])
+   goto exit;
+
+   ret = sprintf(buf, "%s\n",
+   tz->map[indx]->trip_type == THERMAL_TRIP_ACTIVE ?
+   "active" : "passive");
+exit:
+   mutex_unlock(>lock);
+   return ret;
+}
+
+static ssize_t map_ts_name_show(struct device *dev,
+   struct device_attribute *attr, char *buf)
+{
+   int indx, ret = -EINVAL;
+   struct thermal_zone *tz = to_zone(dev);
+
+   if (!sscanf(attr->attr.name, "map%d_sensor_name", ))
+   return -EINVAL;
+
+   if (indx < 0 || indx >= MAX_MAPS_PER_ZONE)
+   return -EINVAL;
+
+   mutex_lock(>lock);
+
+   if (!tz->map[indx])
+   goto exit;
+
+   ret = sprintf(buf, "%s\n", tz->map[indx]->sensor_name);
+exit:
+   mutex_unlock(>lock);
+   return ret;
+}
+
+static ssize_t map_cdev_name_show(struct device *dev,
+   struct device_attribute *attr, char *buf)
+{
+   int indx, ret = -EINVAL;
+   struct thermal_zone *tz = to_zone(dev);
+
+   if (!sscanf(attr->attr.name, "map%d_cdev_name", ))
+   return -EINVAL;
+
+   if (indx < 0 || indx >= MAX_MAPS_PER_ZONE)
+   return -EINVAL;
+
+   mutex_lock(>lock);
+
+   if (!tz->map[indx])
+   goto exit;
+
+   ret = sprintf(buf, "%s\n", tz->map[indx]->cdev_name);
+exit:
+   mutex_unlock(>lock);
+   return ret;
+}
+
+static ssize_t map_trip_mask_show(struct device *dev,
+   struct device_attribute *attr, char *buf)
+{
+   int indx, ret = -EINVAL;
+   struct thermal_zone *tz = to_zone(dev);
+
+   if (!sscanf(attr->attr.name, "map%d_trip_mask", ))
+   return -EINVAL;
+
+   if (indx < 0 || indx >= MAX_MAPS_PER_ZONE)
+   return -EINVAL;
+
+   mutex_lock(>lock);
+
+   if (!tz->map[indx])
+   goto exit;
+
+   ret = sprintf(buf, "0x%x\n", tz->map[indx]->trip_mask);

[PATCHv4 6/9] Thermal: Create Thermal map sysfs attributes for a zone

2013-10-01 Thread Durgadoss R
This patch creates a thermal map sysfs node under
/sys/class/thermal/zoneX/. The thermal map
shows the binding relationship between a sensor
and a cooling device within a particular zone.
This contains entries named mapY_trip_type,
mapY_sensor_name, mapY_cdev_name, mapY_trip_mask,
mapY_weightX.

Signed-off-by: Durgadoss R durgados...@intel.com
---
 drivers/thermal/thermal_core.c |  252 +++-
 include/linux/thermal.h|   26 +
 2 files changed, 277 insertions(+), 1 deletion(-)

diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c
index d6e29f6..e1289a4 100644
--- a/drivers/thermal/thermal_core.c
+++ b/drivers/thermal/thermal_core.c
@@ -548,6 +548,24 @@ static void __remove_trip_attr(struct thermal_zone *tz, 
int indx)
tz-trip_attr[indx] = NULL;
 }
 
+static void __remove_map_entry(struct thermal_zone *tz, int indx)
+{
+   int i;
+   struct thermal_map_attr *attr;
+
+   attr = tz-map_attr[indx];
+
+   for (i = 0; i  NUM_MAP_ATTRS; i++)
+   device_remove_file(tz-device, attr-attrs[i].attr);
+
+   for (i = 0; i  tz-map[indx]-num_weights; i++)
+   device_remove_file(tz-device, attr-weights_attr[i].attr);
+
+   kfree(tz-map_attr[indx]);
+   tz-map_attr[indx] = NULL;
+   tz-map[indx] = NULL;
+}
+
 static void remove_sensor_from_zone(struct thermal_zone *tz,
struct thermal_sensor *ts)
 {
@@ -572,6 +590,14 @@ static void remove_sensor_from_zone(struct thermal_zone 
*tz,
}
 
tz-sensor_indx--;
+
+   /* Remove all mappings associated with this sensor */
+   for (j = 0; j  MAX_MAPS_PER_ZONE; j++) {
+   if (tz-map[j]  !strnicmp(ts-name, tz-map[j]-sensor_name,
+   THERMAL_NAME_LENGTH)) {
+   __remove_map_entry(tz, j);
+   }
+   }
mutex_unlock(tz-lock);
 }
 
@@ -593,6 +619,14 @@ static void remove_cdev_from_zone(struct thermal_zone *tz,
tz-cdevs[j] = tz-cdevs[j + 1];
 
tz-cdev_indx--;
+
+   /* Remove all mappings associated with this sensor */
+   for (j = 0; j  MAX_MAPS_PER_ZONE; j++) {
+   if (tz-map[j]  !strnicmp(cdev-type, tz-map[j]-cdev_name,
+   THERMAL_NAME_LENGTH)) {
+   __remove_map_entry(tz, j);
+   }
+   }
mutex_unlock(tz-lock);
 }
 
@@ -1117,6 +1151,126 @@ critical_trip_show(struct device *dev,
return sprintf(buf, %d\n, val);
 }
 
+static ssize_t
+map_ttype_show(struct device *dev, struct device_attribute *attr, char *buf)
+{
+   int indx, ret = -EINVAL;
+   struct thermal_zone *tz = to_zone(dev);
+
+   if (!sscanf(attr-attr.name, map%d_trip_type, indx))
+   return -EINVAL;
+
+   if (indx  0 || indx = MAX_MAPS_PER_ZONE)
+   return -EINVAL;
+
+   mutex_lock(tz-lock);
+
+   if (!tz-map[indx])
+   goto exit;
+
+   ret = sprintf(buf, %s\n,
+   tz-map[indx]-trip_type == THERMAL_TRIP_ACTIVE ?
+   active : passive);
+exit:
+   mutex_unlock(tz-lock);
+   return ret;
+}
+
+static ssize_t map_ts_name_show(struct device *dev,
+   struct device_attribute *attr, char *buf)
+{
+   int indx, ret = -EINVAL;
+   struct thermal_zone *tz = to_zone(dev);
+
+   if (!sscanf(attr-attr.name, map%d_sensor_name, indx))
+   return -EINVAL;
+
+   if (indx  0 || indx = MAX_MAPS_PER_ZONE)
+   return -EINVAL;
+
+   mutex_lock(tz-lock);
+
+   if (!tz-map[indx])
+   goto exit;
+
+   ret = sprintf(buf, %s\n, tz-map[indx]-sensor_name);
+exit:
+   mutex_unlock(tz-lock);
+   return ret;
+}
+
+static ssize_t map_cdev_name_show(struct device *dev,
+   struct device_attribute *attr, char *buf)
+{
+   int indx, ret = -EINVAL;
+   struct thermal_zone *tz = to_zone(dev);
+
+   if (!sscanf(attr-attr.name, map%d_cdev_name, indx))
+   return -EINVAL;
+
+   if (indx  0 || indx = MAX_MAPS_PER_ZONE)
+   return -EINVAL;
+
+   mutex_lock(tz-lock);
+
+   if (!tz-map[indx])
+   goto exit;
+
+   ret = sprintf(buf, %s\n, tz-map[indx]-cdev_name);
+exit:
+   mutex_unlock(tz-lock);
+   return ret;
+}
+
+static ssize_t map_trip_mask_show(struct device *dev,
+   struct device_attribute *attr, char *buf)
+{
+   int indx, ret = -EINVAL;
+   struct thermal_zone *tz = to_zone(dev);
+
+   if (!sscanf(attr-attr.name, map%d_trip_mask, indx))
+   return -EINVAL;
+
+   if (indx  0 || indx = MAX_MAPS_PER_ZONE)
+   return -EINVAL;
+
+   mutex_lock(tz-lock);
+
+   if (!tz-map[indx])
+   goto exit;
+
+   ret = sprintf(buf, 0x%x\n, tz-map[indx]-trip_mask);
+exit:
+