drivers: nct1008: Generic therm funcs to nct1008
Joshua Primero [Thu, 13 Oct 2011 21:42:56 +0000 (14:42 -0700)]
Preparing nct1008 for refactoring overhaul.  Added
generic functions which will be used by Tegra thermal
module.

Reviewed-on: http://git-master/r/57952
Reviewed-on: http://git-master/r/63337

cherry-picked from 2d5e2369511fb49f1895938fea935b12d72c4827

Change-Id: Ia850db408f533318db4b634e6f29b68c1ad8a1de
Signed-off-by: Joshua Primero <jprimero@nvidia.com>
Reviewed-on: http://git-master/r/66549
Reviewed-by: Diwakar Tundlam <dtundlam@nvidia.com>

Rebase-Id: Rda68bc2f0c9bcc3dc0f24b845715ace1a6287639

drivers/misc/nct1008.c
include/linux/nct1008.h

index cd42102..ad301a0 100644 (file)
 #define CELSIUS_TO_MILLICELSIUS(x) ((x)*1000)
 #define MILLICELSIUS_TO_CELSIUS(x) ((x)/1000)
 
-struct nct1008_data {
-       struct work_struct work;
-       struct i2c_client *client;
-       struct nct1008_platform_data plat_data;
-       struct mutex mutex;
-       struct dentry *dent;
-       u8 config;
-       s8 *limits;
-       u8 limits_sz;
-       void (*alarm_fn)(bool raised);
-       struct regulator *nct_reg;
-#ifdef CONFIG_TEGRA_THERMAL_SYSFS
-       struct thermal_zone_device *thz;
-#endif
-};
-
 static inline s8 value_to_temperature(bool extended, u8 value)
 {
        return extended ? (s8)(value - EXTENDED_RANGE_OFFSET) : (s8)value;
@@ -949,6 +933,9 @@ static int __devinit nct1008_configure_sensor(struct nct1008_data* data)
        }
 
        data->alarm_fn = pdata->alarm_fn;
+
+       data->current_lo_limit = -1;
+       data->current_hi_limit = -1;
        return 0;
 error:
        dev_err(&client->dev, "\n exit %s, err=%d ", __func__, err);
@@ -996,6 +983,70 @@ static unsigned int get_ext_mode_delay_ms(unsigned int conv_rate)
        }
 }
 
+int nct1008_thermal_get_temp(struct nct1008_data *data, long *temp)
+{
+       return nct1008_get_temp(&data->client->dev, temp);
+}
+
+int nct1008_thermal_set_limits(struct nct1008_data *data,
+                               long lo_limit_milli,
+                               long hi_limit_milli)
+{
+       int err;
+       u8 value;
+       bool extended_range = data->plat_data.ext_range;
+       long lo_limit = MILLICELSIUS_TO_CELSIUS(lo_limit_milli);
+       long hi_limit = MILLICELSIUS_TO_CELSIUS(hi_limit_milli);
+
+       if (lo_limit >= hi_limit)
+               return -EINVAL;
+
+       if (data->current_lo_limit == lo_limit &&
+               data->current_hi_limit == hi_limit)
+               return 0;
+
+       if (data->current_lo_limit != lo_limit) {
+               value = temperature_to_value(extended_range, lo_limit);
+               pr_debug("%s: %d\n", __func__, value);
+               err = i2c_smbus_write_byte_data(data->client,
+                               EXT_TEMP_LO_LIMIT_HI_BYTE_WR, value);
+               if (err)
+                       return err;
+
+               data->current_lo_limit = lo_limit;
+       }
+
+       if (data->current_hi_limit != hi_limit) {
+               value = temperature_to_value(extended_range, hi_limit);
+               pr_debug("%s: %d\n", __func__, value);
+               err = i2c_smbus_write_byte_data(data->client,
+                               EXT_TEMP_HI_LIMIT_HI_BYTE_WR, value);
+               if (err)
+                       return err;
+
+               data->current_hi_limit = hi_limit;
+       }
+
+}
+
+int nct1008_thermal_set_alert(struct nct1008_data *data,
+                               void (*alert_func)(void *),
+                               void *alert_data)
+{
+       data->alert_func = alert_func;
+       data->alert_data = alert_data;
+
+       return 0;
+}
+
+int nct1008_thermal_set_shutdown_temp(struct nct1008_data *data,
+                                       long shutdown_temp)
+{
+       data->shutdown_temp = shutdown_temp;
+
+       return 0;
+}
+
 #ifdef CONFIG_TEGRA_THERMAL_SYSFS
 
 static int nct1008_thermal_zone_bind(struct thermal_zone_device *thermal,
@@ -1084,7 +1135,6 @@ static int __devinit nct1008_probe(struct i2c_client *client,
 #endif
 
        data = kzalloc(sizeof(struct nct1008_data), GFP_KERNEL);
-
        if (!data)
                return -ENOMEM;
 
@@ -1159,6 +1209,9 @@ static int __devinit nct1008_probe(struct i2c_client *client,
        data->thz = thz;
 #endif
 
+       if (data->plat_data.probe_callback)
+               data->plat_data.probe_callback(data);
+
        return 0;
 
 error:
@@ -1190,6 +1243,7 @@ static int __devexit nct1008_remove(struct i2c_client *client)
        nct1008_power_control(data, false);
        if (data->nct_reg)
                regulator_put(data->nct_reg);
+
        kfree(data);
 
        return 0;
index 4051988..8e4b4bc 100644 (file)
@@ -29,6 +29,8 @@
 
 #define MAX_ZONES      16
 
+struct nct1008_data;
+
 struct nct1008_platform_data {
        bool supported_hwrev;
        bool ext_range;
@@ -41,6 +43,38 @@ struct nct1008_platform_data {
        s8 thermal_zones[MAX_ZONES];
        u8 thermal_zones_sz;
        void (*alarm_fn)(bool raised);
+       void (*probe_callback)(struct nct1008_data *);
+};
+
+struct nct1008_data {
+       struct work_struct work;
+       struct i2c_client *client;
+       struct nct1008_platform_data plat_data;
+       struct mutex mutex;
+       struct dentry *dent;
+       u8 config;
+       s8 *limits;
+       u8 limits_sz;
+       void (*alarm_fn)(bool raised);
+       struct regulator *nct_reg;
+#ifdef CONFIG_TEGRA_THERMAL_SYSFS
+       struct thermal_zone_device *thz;
+#endif
+       long current_lo_limit;
+       long current_hi_limit;
+
+       long shutdown_temp;
+       void (*alert_func)(void *);
+       void *alert_data;
 };
 
+int nct1008_thermal_get_temp(struct nct1008_data *data, long *temp);
+int nct1008_thermal_set_limits(struct nct1008_data *data,
+                               long lo_limit_milli,
+                               long hi_limit_milli);
+int nct1008_thermal_set_alert(struct nct1008_data *data,
+                               void (*alert_func)(void *),
+                               void *alert_data);
+int nct1008_thermal_set_shutdown_temp(struct nct1008_data *data,
+                                       long shutdown_temp);
 #endif /* _LINUX_NCT1008_H */