hwmon: tegra: tsensor: error handling change
[linux-2.6.git] / drivers / hwmon / tegra-tsensor.c
1 /*
2  * NVIDIA Tegra SOC - temperature sensor driver
3  *
4  * Copyright (C) 2011 NVIDIA Corporation
5  *
6  * This software is licensed under the terms of the GNU General Public
7  * License version 2, as published by the Free Software Foundation, and
8  * may be copied, distributed, and modified under those terms.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  */
16
17 #if 0
18 #define VERBOSE_DEBUG
19 #define DEBUG
20 #endif
21
22 #include <linux/slab.h>
23 #include <linux/platform_device.h>
24 #include <linux/hwmon.h>
25 #include <linux/clk.h>
26 #include <linux/io.h>
27 #include <linux/irq.h>
28 #include <linux/interrupt.h>
29 #include <linux/irqreturn.h>
30 #include <linux/err.h>
31 #include <linux/spinlock.h>
32 #include <linux/hwmon-sysfs.h>
33 #include <linux/regulator/consumer.h>
34 #include <linux/delay.h>
35
36 #include <mach/iomap.h>
37 #include <mach/clk.h>
38 #include <mach/delay.h>
39 #include <mach/tsensor.h>
40 #include <mach/tegra_fuse.h>
41
42 /* We have multiple tsensor instances with following registers */
43 #define SENSOR_CFG0                             0x40
44 #define SENSOR_CFG1                             0x48
45 #define SENSOR_CFG2                             0x4c
46 #define SENSOR_STATUS0                          0x58
47 #define SENSOR_TS_STATUS1                       0x5c
48 #define SENSOR_TS_STATUS2                       0x60
49
50 /* interrupt mask in tsensor status register */
51 #define TSENSOR_SENSOR_X_STATUS0_0_INTR_MASK    (1 << 8)
52
53 #define SENSOR_CFG0_M_MASK                      0xffff
54 #define SENSOR_CFG0_M_SHIFT                     8
55 #define SENSOR_CFG0_N_MASK                      0xff
56 #define SENSOR_CFG0_N_SHIFT                     24
57 #define SENSOR_CFG0_RST_INTR_SHIFT              6
58 #define SENSOR_CFG0_HW_DIV2_INTR_SHIFT          5
59 #define SENSOR_CFG0_OVERFLOW_INTR               4
60 #define SENSOR_CFG0_RST_ENABLE_SHIFT            2
61 #define SENSOR_CFG0_HW_DIV2_ENABLE_SHIFT        1
62 #define SENSOR_CFG0_STOP_SHIFT                  0
63
64 #define SENSOR_CFG_X_TH_X_MASK                  0xffff
65 #define SENSOR_CFG1_TH2_SHIFT                   16
66 #define SENSOR_CFG1_TH1_SHIFT                   0
67 #define SENSOR_CFG2_TH3_SHIFT                   0
68 #define SENSOR_CFG2_TH0_SHIFT                   16
69
70 #define SENSOR_STATUS_AVG_VALID_SHIFT           10
71 #define SENSOR_STATUS_CURR_VALID_SHIFT          9
72
73 #define STATE_MASK                              0x7
74 #define STATUS0_STATE_SHIFT                     0
75 #define STATUS0_PREV_STATE_SHIFT                4
76
77 #define LOCAL_STR_SIZE1                         60
78 #define MAX_STR_LINE                            100
79 #define MAX_TSENSOR_LOOP1                       (1000 * 2)
80
81 #define TSENSOR_COUNTER_TOLERANCE               100
82
83 #define SENSOR_CTRL_RST_SHIFT                   1
84 #define RST_SRC_MASK                            0x7
85 #define RST_SRC_SENSOR                          2
86 #define TEGRA_REV_REG_OFFSET                    0x804
87 #define CCLK_G_BURST_POLICY_REG_REL_OFFSET      0x368
88 #define TSENSOR_SLOWDOWN_BIT                    23
89
90 /* tsensor instance used for temperature calculation */
91 #define TSENSOR_FUSE_REVISION_DECIMAL_REV1      8
92 #define TSENSOR_FUSE_REVISION_DECIMAL_REV2      21
93
94 /* macros used for temperature calculations */
95 #define get_temperature_int(X)                  ((X) / 100)
96 #define get_temperature_fraction(X)             (((int)(abs(X))) % 100)
97 #define get_temperature_round(X)                DIV_ROUND_CLOSEST(X, 100)
98
99 /* tsensor states */
100 enum ts_state {
101         TS_INVALID = 0,
102         TS_LEVEL0,
103         TS_LEVEL1,
104         TS_LEVEL2,
105         TS_LEVEL3,
106         TS_OVERFLOW,
107         TS_MAX_STATE = TS_OVERFLOW
108 };
109
110 /* composite type with tsensor state */
111 struct tsensor_state {
112         unsigned int prev_state;
113         unsigned int state;
114 };
115
116 enum {
117         /* temperature is sensed from 2 points on tegra */
118         TSENSOR_COUNT = 2,
119         /* divide by 2 temperature threshold */
120         DIV2_CELSIUS_TEMP_THRESHOLD_DEFAULT = 70,
121         /* reset chip temperature threshold */
122         RESET_CELSIUS_TEMP_THRESHOLD_DEFAULT = 75,
123         /* tsensor frequency in Hz for clk src CLK_M and divisor=24 */
124         DEFAULT_TSENSOR_CLK_HZ = 500000,
125         DEFAULT_TSENSOR_N = 255,
126         DEFAULT_TSENSOR_M = 500,
127         /* tsensor instance offset */
128         TSENSOR_INSTANCE_OFFSET = 0x40,
129         MIN_THRESHOLD = 0x0,
130         MAX_THRESHOLD = 0xffff,
131         DEFAULT_THRESHOLD_TH0 = MAX_THRESHOLD,
132         DEFAULT_THRESHOLD_TH1 = MAX_THRESHOLD,
133         DEFAULT_THRESHOLD_TH2 = MAX_THRESHOLD,
134         DEFAULT_THRESHOLD_TH3 = MAX_THRESHOLD,
135 };
136
137 /* constants used to implement sysfs interface */
138 enum tsensor_params {
139         TSENSOR_PARAM_TH1 = 0,
140         TSENSOR_PARAM_TH2,
141         TSENSOR_PARAM_TH3,
142         TSENSOR_TEMPERATURE
143 };
144
145 /*
146  * For each registered chip, we need to keep some data in memory.
147  * The structure is dynamically allocated.
148  */
149 struct tegra_tsensor_data {
150         struct device *hwmon_dev;
151         spinlock_t tsensor_lock;
152         struct clk *dev_clk;
153         /* tsensor register space */
154         void __iomem            *base;
155         unsigned long           phys;
156         unsigned long           phys_end;
157         /* pmc register space */
158         void __iomem            *pmc_rst_base;
159         unsigned long           pmc_phys;
160         unsigned long           pmc_phys_end;
161         /* clk register space */
162         void __iomem            *clk_rst_base;
163         int                     irq;
164         unsigned int            int_status[TSENSOR_COUNT];
165
166         /* threshold for hardware triggered clock divide by 2 */
167         int div2_temp;
168         /* temperature threshold for hardware triggered system reset */
169         int reset_temp;
170         /* temperature threshold to trigger software interrupt */
171         int sw_intr_temp;
172         int hysteresis;
173         unsigned int ts_state_saved[TSENSOR_COUNT];
174         /* save configuration before suspend and restore after resume */
175         unsigned int config0[TSENSOR_COUNT];
176         unsigned int config1[TSENSOR_COUNT];
177         unsigned int config2[TSENSOR_COUNT];
178 };
179
180 enum {
181         TSENSOR_COEFF_SET1 = 0,
182         TSENSOR_COEFF_END
183 };
184
185 struct tegra_tsensor_coeff {
186         int e_minus6_m;
187         int e_minus6_n;
188         int e_minus2_p;
189 };
190
191 static struct tegra_tsensor_coeff coeff_table[] = {
192         [TSENSOR_COEFF_SET1] = {
193                 -2775,
194                 1338811,
195                 -730
196         }
197         /* FIXME: add tsensor coefficients after chip characterization */
198 };
199
200 static unsigned int tsensor_index;
201 static char my_fixed_str[LOCAL_STR_SIZE1] = "YYYYYY";
202 static char error_str[LOCAL_STR_SIZE1] = "ERROR:";
203 static unsigned int fuse_T1, fuse_F1, fuse_T2, fuse_F2;
204 static int A_e_minus6, B_e_minus2;
205 static int m_e_minus6;
206 static int n_e_minus6;
207 static int p_e_minus2;
208 static unsigned int init_flag;
209
210 static int tsensor_count_2_temp(struct tegra_tsensor_data *data,
211         unsigned int count, int *p_temperature);
212 static unsigned int tsensor_get_threshold_counter(
213         struct tegra_tsensor_data *data, unsigned int temp);
214
215 /* tsensor register access functions */
216
217 static void tsensor_writel(struct tegra_tsensor_data *data, u32 val,
218                                 unsigned long reg)
219 {
220         unsigned int reg_offset = reg & 0xffff;
221         unsigned char inst = (reg >> 16) & 0xffff;
222         writel(val, data->base + (inst * TSENSOR_INSTANCE_OFFSET) +
223                 reg_offset);
224         return;
225 }
226
227 static unsigned int tsensor_readl(struct tegra_tsensor_data *data,
228                                 unsigned long reg)
229 {
230         unsigned int reg_offset = reg & 0xffff;
231         unsigned char inst = (reg >> 16) & 0xffff;
232         return readl(data->base +
233                 (inst * TSENSOR_INSTANCE_OFFSET) + reg_offset);
234 }
235
236 static unsigned int tsensor_get_reg_field(
237         struct tegra_tsensor_data *data, unsigned int reg,
238         unsigned int shift, unsigned int mask)
239 {
240         unsigned int reg_val;
241         reg_val = tsensor_readl(data, reg);
242         return (reg_val & (mask << shift)) >> shift;
243 }
244
245 static int tsensor_set_reg_field(
246         struct tegra_tsensor_data *data, unsigned int value,
247         unsigned int reg, unsigned int shift, unsigned int mask)
248 {
249         unsigned int reg_val;
250         unsigned int rd_val;
251         reg_val = tsensor_readl(data, reg);
252         reg_val &= ~(mask << shift);
253         reg_val |= ((value & mask) << shift);
254         tsensor_writel(data, reg_val, reg);
255         rd_val = tsensor_readl(data, reg);
256         if (rd_val == reg_val)
257                 return 0;
258         else
259                 return -EINVAL;
260 }
261
262 /* enable argument is true to enable reset, false disables pmc reset */
263 static void pmc_rst_enable(struct tegra_tsensor_data *data, bool enable)
264 {
265         unsigned int val;
266         /* mapped first pmc reg is SENSOR_CTRL */
267         val = readl(data->pmc_rst_base);
268         if (enable)
269                 val |= (1 << SENSOR_CTRL_RST_SHIFT);
270         else
271                 val &= ~(1 << SENSOR_CTRL_RST_SHIFT);
272         writel(val, data->pmc_rst_base);
273 }
274
275 /* true returned when pmc reset source is tsensor */
276 static bool pmc_check_rst_sensor(struct tegra_tsensor_data *data)
277 {
278         unsigned int val;
279         unsigned char src;
280         val = readl(data->pmc_rst_base + 4);
281         src = (unsigned char)(val & RST_SRC_MASK);
282         if (src == RST_SRC_SENSOR)
283                 return true;
284         else
285                 return false;
286 }
287
288 /* function to get chip revision */
289 static void get_chip_rev(unsigned short *p_id, unsigned short *p_major,
290                 unsigned short *p_minor)
291 {
292         unsigned int reg;
293
294         reg = readl(IO_TO_VIRT(TEGRA_APB_MISC_BASE) +
295                 TEGRA_REV_REG_OFFSET);
296         *p_id = (reg >> 8) & 0xff;
297         *p_major = (reg >> 4) & 0xf;
298         *p_minor = (reg >> 16) & 0xf;
299         pr_info("\n Tegra chip revision for tsensor detected as: "
300                 " Chip Id=%x, Major=%d, Minor=%d ", (int)*p_id,
301                 (int)*p_major, (int)*p_minor);
302 }
303
304 /*
305  * function to get chip revision specific tsensor coefficients
306  * obtained after chip characterization
307  */
308 static int get_chip_tsensor_coeff(void)
309 {
310         unsigned short chip_id, major_rev, minor_rev;
311         unsigned short coeff_index;
312
313         get_chip_rev(&chip_id, &major_rev, &minor_rev);
314         switch (minor_rev) {
315         case 2:
316                 pr_info("\n Warning: tsensor coefficient for chip pending ");
317         case 1:
318                 coeff_index = TSENSOR_COEFF_SET1;
319                 break;
320         default:
321                 pr_err("\n Error: tsensor unsupported for detected chip ");
322                 return -ENOENT;
323         }
324         m_e_minus6 = coeff_table[coeff_index].e_minus6_m;
325         n_e_minus6 = coeff_table[coeff_index].e_minus6_n;
326         p_e_minus2 = coeff_table[coeff_index].e_minus2_p;
327         return 0;
328 }
329
330 /* tsensor counter read function */
331 static unsigned int tsensor_read_counter(
332         struct tegra_tsensor_data *data, u8 instance,
333         unsigned int *p_counterA, unsigned int *p_counterB)
334 {
335         unsigned int status_reg;
336         unsigned int config0;
337         int iter_count = 0;
338         const int max_loop = 50;
339
340         do {
341                 config0 = tsensor_readl(data, ((instance << 16) |
342                         SENSOR_CFG0));
343                 if (config0 & (1 << SENSOR_CFG0_STOP_SHIFT)) {
344                         dev_dbg(data->hwmon_dev, "\n Error: tsensor "
345                                 "counter read with STOP bit not supported ");
346                         *p_counterA = 0;
347                         *p_counterB = 0;
348                         return 0;
349                 }
350                 status_reg = tsensor_readl(data,
351                         (instance << 16) | SENSOR_STATUS0);
352                 if ((status_reg & (1 <<
353                         SENSOR_STATUS_AVG_VALID_SHIFT)) &&
354                         (status_reg & (1 <<
355                         SENSOR_STATUS_CURR_VALID_SHIFT))) {
356                         *p_counterA = tsensor_readl(data, (instance
357                                 << 16) | SENSOR_TS_STATUS1);
358                         *p_counterB = tsensor_readl(data, (instance
359                                 << 16) | SENSOR_TS_STATUS2);
360                         break;
361                 }
362                 if (!(iter_count % 10))
363                         dev_dbg(data->hwmon_dev, "\n retry %d ", iter_count);
364                 msleep(1);
365                 iter_count++;
366         } while (iter_count < max_loop);
367         if (iter_count == max_loop)
368                 return -ENODEV;
369         return 0;
370 }
371
372 /* tsensor threshold print function */
373 static void dump_threshold(struct tegra_tsensor_data *data)
374 {
375         int i;
376         unsigned int TH_2_1, TH_0_3;
377         unsigned int curr_avg, min_max;
378         int err;
379         for (i = 0; i < TSENSOR_COUNT; i++) {
380                 TH_2_1 = tsensor_readl(data, ((i << 16) | SENSOR_CFG1));
381                 TH_0_3 = tsensor_readl(data, ((i << 16) | SENSOR_CFG2));
382                 dev_dbg(data->hwmon_dev, "\n Tsensor[%d]: TH_2_1=0x%x, "
383                         "TH_0_3=0x%x ", i, TH_2_1, TH_0_3);
384                 err = tsensor_read_counter(data, i, &curr_avg, &min_max);
385                 if (err < 0)
386                         pr_err("\n Error: tsensor %d counter read, "
387                                 "err=%d ", i, err);
388                 else
389                         dev_dbg(data->hwmon_dev, "\n Tsensor[%d]: "
390                                 "curr_avg=0x%x, min_max=0x%x ",
391                                 i, curr_avg, min_max);
392         }
393 }
394
395 /* tsensor temperature show function */
396 static ssize_t tsensor_show_counters(struct device *dev,
397         struct device_attribute *da, char *buf)
398 {
399         int i;
400         unsigned int curr_avg[TSENSOR_COUNT];
401         unsigned int min_max[TSENSOR_COUNT];
402         char err_str[] = "error-sysfs-counter-read\n";
403         char fixed_str[MAX_STR_LINE];
404         struct tegra_tsensor_data *data = dev_get_drvdata(dev);
405         struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
406         int err;
407         int temp0, temp1;
408
409         if (attr->index == TSENSOR_TEMPERATURE)
410                 snprintf(fixed_str, MAX_STR_LINE, "temperature: ");
411         else
412                 goto error;
413         for (i = 0; i < TSENSOR_COUNT; i++) {
414                 err = tsensor_read_counter(data, i,
415                         &curr_avg[i], &min_max[i]);
416                 if (err < 0)
417                         goto error;
418         }
419         if (attr->index == TSENSOR_TEMPERATURE) {
420                 /* use current counter value to calculate temperature */
421                 err = tsensor_count_2_temp(data,
422                         ((curr_avg[0] & 0xFFFF0000) >> 16), &temp0);
423                 dev_vdbg(data->hwmon_dev, "\n %s has curr_avg=0x%x, "
424                         "minmax=0x%x, temp0=%d ", __func__,
425                         curr_avg[0], min_max[0], temp0);
426                 if (err < 0)
427                         goto error;
428                 err = tsensor_count_2_temp(data,
429                         ((curr_avg[1] & 0xFFFF0000) >> 16), &temp1);
430                 dev_vdbg(data->hwmon_dev, "\n %s has curr_avg=0x%x, "
431                         "minmax=0x%x, temp1=%d ", __func__,
432                         curr_avg[1], min_max[1], temp1);
433                 if (err < 0)
434                         goto error;
435                 snprintf(buf, (((LOCAL_STR_SIZE1 << 1) + 3) +
436                         strlen(fixed_str)),
437                         "%s "
438                         "[%d]: current counter=0x%x, %d.%d"
439                         " deg Celsius ", fixed_str,
440                         tsensor_index,
441                         ((curr_avg[tsensor_index] & 0xFFFF0000) >> 16),
442                         get_temperature_int(temp1),
443                         get_temperature_fraction(temp1));
444         }
445         strcat(buf, "\n");
446         return strlen(buf);
447 error:
448         return snprintf(buf, strlen(err_str),
449                 "%s", err_str);
450 }
451
452 /* utility function to check hw clock divide by 2 condition */
453 static bool cclkg_check_hwdiv2_sensor(struct tegra_tsensor_data *data)
454 {
455         unsigned int val;
456         val = readl(IO_ADDRESS(TEGRA_CLK_RESET_BASE +
457                 CCLK_G_BURST_POLICY_REG_REL_OFFSET));
458         if ((1 << TSENSOR_SLOWDOWN_BIT) & val) {
459                 dev_err(data->hwmon_dev, "\n Warning: ***** tsensor "
460                         "slowdown bit detected ");
461                 return true;
462         } else {
463                 return false;
464         }
465 }
466
467 /*
468  * function with table to return register, field shift and mask
469  * values for supported parameters
470  */
471 static int get_param_values(unsigned int indx,
472         unsigned int *p_reg, unsigned int *p_sft, unsigned int *p_msk)
473 {
474         switch (indx) {
475         case TSENSOR_PARAM_TH1:
476                 *p_reg = ((tsensor_index << 16) | SENSOR_CFG1);
477                 *p_sft = SENSOR_CFG1_TH1_SHIFT;
478                 *p_msk = SENSOR_CFG_X_TH_X_MASK;
479                 snprintf(my_fixed_str, LOCAL_STR_SIZE1, "TH1[%d]: ",
480                         tsensor_index);
481                 break;
482         case TSENSOR_PARAM_TH2:
483                 *p_reg = ((tsensor_index << 16) | SENSOR_CFG1);
484                 *p_sft = SENSOR_CFG1_TH2_SHIFT;
485                 *p_msk = SENSOR_CFG_X_TH_X_MASK;
486                 snprintf(my_fixed_str, LOCAL_STR_SIZE1, "TH2[%d]: ",
487                         tsensor_index);
488                 break;
489         case TSENSOR_PARAM_TH3:
490                 *p_reg = ((tsensor_index << 16) | SENSOR_CFG2);
491                 *p_sft = SENSOR_CFG2_TH3_SHIFT;
492                 *p_msk = SENSOR_CFG_X_TH_X_MASK;
493                 snprintf(my_fixed_str, LOCAL_STR_SIZE1, "TH3[%d]: ",
494                         tsensor_index);
495                 break;
496         default:
497                 return -ENOENT;
498         }
499         return 0;
500 }
501
502 /* tsensor driver sysfs show function */
503 static ssize_t show_tsensor_param(struct device *dev,
504                                 struct device_attribute *da,
505                                 char *buf)
506 {
507         unsigned int val;
508         struct tegra_tsensor_data *data = dev_get_drvdata(dev);
509         struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
510         unsigned int reg;
511         unsigned int sft;
512         unsigned int msk;
513         int err;
514         int temp;
515
516         err = get_param_values(attr->index, &reg, &sft, &msk);
517         if (err < 0)
518                 goto labelErr;
519         val = tsensor_get_reg_field(data, reg, sft, msk);
520         if (val == MAX_THRESHOLD)
521                 snprintf(buf, LOCAL_STR_SIZE1 + strlen(my_fixed_str),
522                         "%s un-initialized threshold ",
523                         my_fixed_str);
524         else {
525                 err = tsensor_count_2_temp(data, val, &temp);
526                 if (err != 0)
527                         goto labelErr;
528                 snprintf(buf, LOCAL_STR_SIZE1 + strlen(my_fixed_str),
529                         "%s threshold: %d.%d Celsius ",
530                         my_fixed_str, get_temperature_int(temp),
531                         get_temperature_fraction(temp));
532         }
533         strcat(buf, "\n");
534         return strlen(buf);
535 labelErr:
536         snprintf(buf, strlen(error_str), "%s", error_str);
537         return strlen(buf);
538 }
539
540 /* tsensor driver sysfs store function */
541 static ssize_t set_tsensor_param(struct device *dev,
542                         struct device_attribute *da,
543                         const char *buf, size_t count)
544 {
545         int num;
546         struct tegra_tsensor_data *data = dev_get_drvdata(dev);
547         struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
548         unsigned int reg;
549         unsigned int sft;
550         unsigned int msk;
551         int err;
552         unsigned int counter;
553         unsigned int val;
554
555         if (strict_strtoul(buf, 0, (long int *)&num)) {
556                 dev_err(dev, "\n file: %s, line=%d return %s() ",
557                         __FILE__, __LINE__, __func__);
558                 return -EINVAL;
559         }
560
561         counter = tsensor_get_threshold_counter(data, num);
562
563         err = get_param_values(attr->index, &reg, &sft, &msk);
564         if (err < 0)
565                 goto labelErr;
566
567         err = tsensor_set_reg_field(data, counter, reg, sft, msk);
568         if (err < 0)
569                 goto labelErr;
570
571         /* TH2 clk divide check */
572         if (attr->index == TSENSOR_PARAM_TH2) {
573                 msleep(20);
574                 (void)cclkg_check_hwdiv2_sensor(data);
575         }
576         val = tsensor_get_reg_field(data, reg, sft, msk);
577         dev_dbg(dev, "\n %s 0x%x ", my_fixed_str, val);
578         return count;
579 labelErr:
580         dev_err(dev, "\n file: %s, line=%d, %s(), error=0x%x ", __FILE__,
581                 __LINE__, __func__, err);
582         return 0;
583 }
584
585 static SENSOR_DEVICE_ATTR(tsensor_TH1, S_IRUGO | S_IWUSR,
586                 show_tsensor_param, set_tsensor_param, TSENSOR_PARAM_TH1);
587 static SENSOR_DEVICE_ATTR(tsensor_TH2, S_IRUGO | S_IWUSR,
588                 show_tsensor_param, set_tsensor_param, TSENSOR_PARAM_TH2);
589 static SENSOR_DEVICE_ATTR(tsensor_TH3, S_IRUGO | S_IWUSR,
590                 show_tsensor_param, set_tsensor_param, TSENSOR_PARAM_TH3);
591 static SENSOR_DEVICE_ATTR(tsensor_temperature, S_IRUGO | S_IWUSR,
592                 tsensor_show_counters, NULL, TSENSOR_TEMPERATURE);
593
594 static struct attribute *tsensor_attributes[] = {
595         &sensor_dev_attr_tsensor_TH1.dev_attr.attr,
596         &sensor_dev_attr_tsensor_TH2.dev_attr.attr,
597         &sensor_dev_attr_tsensor_TH3.dev_attr.attr,
598         &sensor_dev_attr_tsensor_temperature.dev_attr.attr,
599         NULL
600 };
601
602 static const struct attribute_group tsensor_attr_group = {
603         .attrs = tsensor_attributes,
604 };
605
606 /*
607  * returns current state of tsensor
608  * input: tsensor instance
609  *      initializes argument pointer to tsensor_state
610  */
611 static void get_ts_state(struct tegra_tsensor_data *data,
612         unsigned char inst, struct tsensor_state *p_state)
613 {
614         p_state->prev_state =
615                 tsensor_get_reg_field(data,
616                 ((inst << 16) | SENSOR_STATUS0),
617                 STATUS0_PREV_STATE_SHIFT, STATE_MASK);
618         p_state->state =
619                 tsensor_get_reg_field(data,
620                 ((inst << 16) | SENSOR_STATUS0),
621                 STATUS0_STATE_SHIFT, STATE_MASK);
622 }
623
624 /* tsensor driver interrupt handler */
625 static irqreturn_t tegra_tsensor_isr(int irq, void *arg_data)
626 {
627         struct tegra_tsensor_data *data =
628                 (struct tegra_tsensor_data *)arg_data;
629         unsigned long flags;
630         unsigned int val;
631         unsigned int i;
632         struct tsensor_state new_state;
633
634         spin_lock_irqsave(&data->tsensor_lock, flags);
635
636         for (i = 0; i < TSENSOR_COUNT; i++) {
637                 val = tsensor_readl(data, ((i << 16) | SENSOR_STATUS0));
638                 tsensor_writel(data, val, ((i << 16) | SENSOR_STATUS0));
639                 if (val & TSENSOR_SENSOR_X_STATUS0_0_INTR_MASK) {
640                         dev_err(data->hwmon_dev, "\n tsensor instance-%d "
641                                 "interrupt ", i);
642                         get_ts_state(data, (unsigned char)i, &new_state);
643                         /* counter overflow check */
644                         if (new_state.state == TS_OVERFLOW)
645                                 dev_err(data->hwmon_dev, "\nWarning: "
646                                         "***** OVERFLOW tsensor ");
647                         if (new_state.state != data->ts_state_saved[i]) {
648                                 dev_err(data->hwmon_dev, "\nTS state "
649                                         "change: old=%d, new=%d ",
650                                         data->ts_state_saved[i],
651                                         new_state.state);
652                                 data->ts_state_saved[i] = new_state.state;
653                         }
654                 }
655         }
656
657         spin_unlock_irqrestore(&data->tsensor_lock, flags);
658
659         return IRQ_HANDLED;
660 }
661
662 /*
663  * function to read fuse registers and give - T1, T2, F1 and F2
664  */
665 static int read_tsensor_fuse_regs(struct tegra_tsensor_data *data)
666 {
667         unsigned int reg1;
668         unsigned int T1 = 0, T2 = 0;
669         unsigned int spare_bits;
670         int err;
671
672         /* read tsensor calibration register */
673         /*
674          * High (~90 DegC) Temperature Calibration value (upper 16 bits of
675          *          FUSE_TSENSOR_CALIB_0) - F2
676          * Low (~25 deg C) Temperature Calibration value (lower 16 bits of
677          *          FUSE_TSENSOR_CALIB_0) - F1
678          */
679         err = tegra_fuse_get_tsensor_calibration_data(&reg1);
680         if (err)
681                 goto errLabel;
682         fuse_F1 = reg1 & 0xFFFF;
683         fuse_F2 = (reg1 >> 16) & 0xFFFF;
684
685         err = tegra_fuse_get_tsensor_spare_bits(&spare_bits);
686         if (err) {
687                 pr_err("\n tsensor spare bit fuse read error=%d ", err);
688                 goto errLabel;
689         }
690
691         /*
692          * FUSE_TJ_ADT_LOWT = T1, FUSE_TJ_ADJ = T2
693          */
694
695         /*
696          * Low temp is:
697          * FUSE_TJ_ADT_LOWT = bits [20:14] or’ed with bits [27:21]
698          */
699         T1 = ((spare_bits >> 14) & 0x7F) |
700                 ((spare_bits >> 21) & 0x7F);
701         dev_vdbg(data->hwmon_dev, "\n Tsensor low temp (T1) fuse : ");
702
703         /*
704          * High temp is:
705          * FUSE_TJ_ADJ = bits [6:0] or’ed with bits [13:7]
706          */
707         dev_vdbg(data->hwmon_dev, "\n Tsensor low temp (T2) fuse : ");
708         T2 = (spare_bits & 0x7F) | ((spare_bits >> 7) & 0x7F);
709         pr_info("\n Tsensor fuse calibration F1=%d, F2=%d, T1=%d, T2=%d "
710                 , fuse_F1, fuse_F2, T1, T2);
711         fuse_T1 = T1;
712         fuse_T2 = T2;
713         return 0;
714 errLabel:
715         return err;
716 }
717
718 /* function to calculate interim temperature */
719 static int calc_interim_temp(struct tegra_tsensor_data *data,
720         unsigned int counter, int *p_interim_temp)
721 {
722         int val1;
723         /*
724          * T-int = A * Counter + B
725          * (Counter is the sensor frequency output)
726          */
727         if ((fuse_F2 - fuse_F1) <= (fuse_T2 - fuse_T1)) {
728                 dev_err(data->hwmon_dev, "\n Error: F2=%d, F1=%d "
729                         "difference unexpectedly low. "
730                         "Aborting temperature processing ", fuse_F2, fuse_F1);
731                 return -EINVAL;
732         } else {
733                 /* expression modified after assuming s_A is 10^6 times,
734                  * s_B is 10^2 times and want end result to be 10^2 times
735                  * actual value
736                  */
737                 val1 = DIV_ROUND_CLOSEST((A_e_minus6 * counter) , 10000);
738                 dev_vdbg(data->hwmon_dev, "\n A*counter / 100 = %d ",
739                         val1);
740                 *p_interim_temp = (val1 + B_e_minus2);
741         }
742         dev_dbg(data->hwmon_dev, "\n tsensor: counter=0x%x, interim "
743                 "temp*100=%d ",
744                 counter, *p_interim_temp);
745         return 0;
746 }
747
748 /*
749  * function to calculate final temperature, given
750  * interim temperature
751  */
752 static void calc_final_temp(struct tegra_tsensor_data *data,
753         int interim_temp, int *p_final_temp)
754 {
755         int temp1, temp2, temp;
756         /*
757          * T-final = m * T-int ^2 + n * T-int + p
758          * m = -0.002775
759          * n = 1.338811
760          * p = -7.3
761          */
762
763         dev_vdbg(data->hwmon_dev, "\n interim_temp=%d ", interim_temp);
764         temp1 = (DIV_ROUND_CLOSEST((interim_temp * interim_temp) , 100));
765         dev_vdbg(data->hwmon_dev, "\n temp1=%d ", temp1);
766         temp1 *= (DIV_ROUND_CLOSEST(m_e_minus6 , 10));
767         dev_vdbg(data->hwmon_dev, "\n m*T-int^2=%d ", temp1);
768         temp1 = (DIV_ROUND_CLOSEST(temp1, 10000));
769         /* we want to keep 3 decimal point digits */
770         dev_vdbg(data->hwmon_dev, "\n m*T-int^2 / 10000=%d ", temp1);
771         dev_dbg(data->hwmon_dev, "\n temp1*100=%d ", temp1);
772
773         temp2 = (DIV_ROUND_CLOSEST(interim_temp * (
774                 DIV_ROUND_CLOSEST(n_e_minus6, 100)
775                 ), 1000)); /* 1000 times actual */
776         dev_vdbg(data->hwmon_dev, "\n n*T-int =%d ", temp2);
777
778         temp = temp1 + temp2;
779         dev_vdbg(data->hwmon_dev, "\n m*T-int^2 + n*T-int =%d ", temp);
780         temp += (p_e_minus2 * 10);
781         temp = DIV_ROUND_CLOSEST(temp, 10);
782         /* final temperature(temp) is 100 times actual value
783          * to preserve 2 decimal digits and enable fixed point
784          * computation
785          */
786         dev_vdbg(data->hwmon_dev, "\n m*T-int^2 + n*T-int + p =%d ",
787                 temp);
788         dev_dbg(data->hwmon_dev, "\n Final temp=%d.%d ",
789                 get_temperature_int(temp), get_temperature_fraction(temp));
790         *p_final_temp = (int)(temp);
791 }
792
793 /*
794  * Function to compute constants A and B needed for temperature
795  * calculation
796  * A = (T2-T1) / (F2-F1)
797  * B = T1 – A * F1
798  */
799 static int tsensor_get_const_AB(struct tegra_tsensor_data *data)
800 {
801         int err;
802
803         /*
804          *   1. Find fusing registers for 25C (T1, F1) and 90C (T2, F2);
805          */
806         err = read_tsensor_fuse_regs(data);
807         if (err) {
808                 dev_err(data->hwmon_dev, "\n Fuse register read required "
809                         "for internal tsensor returns err=%d ", err);
810                 return err;
811         }
812
813         if (fuse_F2 != fuse_F1) {
814                 if ((fuse_F2 - fuse_F1) <= (fuse_T2 - fuse_T1)) {
815                         dev_err(data->hwmon_dev, "\n Error: F2=%d, "
816                                 "F1=%d, difference"
817                                 " unexpectedly low. Aborting temperature"
818                                 "computation ", fuse_F2, fuse_F1);
819                         return -EINVAL;
820                 } else {
821                         A_e_minus6 = ((fuse_T2 - fuse_T1) * 1000000);
822                         A_e_minus6 /= (fuse_F2 - fuse_F1);
823                         B_e_minus2 = (fuse_T1 * 100) - (
824                                 DIV_ROUND_CLOSEST((A_e_minus6 *
825                                 fuse_F1), 10000));
826                         /* B is 100 times now */
827                 }
828         }
829         dev_dbg(data->hwmon_dev, "\n A_e_minus6 = %d ", A_e_minus6);
830         dev_dbg(data->hwmon_dev, "\n B_e_minus2 = %d ", B_e_minus2);
831         return 0;
832 }
833
834 /*
835  * function calculates expected temperature corresponding to
836  * given tsensor counter value
837  * Value returned is 100 times calculated temperature since the
838  * calculations are using fixed point arithmetic instead of floating point
839  */
840 static int tsensor_count_2_temp(struct tegra_tsensor_data *data,
841         unsigned int count, int *p_temperature)
842 {
843         int interim_temp;
844         int err;
845
846         /*
847          *
848          * 2. Calculate interim temperature:
849          */
850         err = calc_interim_temp(data, count, &interim_temp);
851         if (err < 0) {
852                 dev_err(data->hwmon_dev, "\n cannot read temperature\n");
853                 *p_temperature = -1;
854                 return err;
855         }
856
857         /*
858          *
859          * 3. Calculate final temperature:
860          */
861         calc_final_temp(data, interim_temp, p_temperature);
862         return 0;
863 }
864
865 /*
866  * utility function implements ceil to power of 10 -
867  * e.g. given 987 it returns 1000
868  */
869 static int my_ceil_pow10(int num)
870 {
871         int tmp;
872         int val = 1;
873         tmp = (num < 0) ? -num : num;
874         if (tmp == 0)
875                 return 0;
876         while (tmp > 1) {
877                 val *= 10;
878                 tmp /= 10;
879         }
880         return val;
881 }
882
883 /*
884  * function to solve quadratic roots of equation
885  * used to get counter corresponding to given temperature
886  */
887 static void get_quadratic_roots(struct tegra_tsensor_data *data,
888                 unsigned int temp, unsigned int *p_counter1,
889                 unsigned int *p_counter2)
890 {
891         /* expr1 = 2 * m * B + n */
892         int expr1_e_minus6;
893         /* expr2 = expr1^2 */
894         int expr2_e_minus6;
895         /* expr3 = m * B^2 + n * B + p */
896         int expr3_e_minus4_1;
897         int expr3_e_minus4_2;
898         int expr3_e_minus4;
899         int expr4_e_minus6;
900         int expr4_e_minus2_1;
901         int expr4_e_minus6_2;
902         int expr4_e_minus6_3;
903         int expr5_e_minus6, expr5_e_minus6_1, expr6, expr7;
904         int expr8_e_minus6, expr9_e_minus6;
905         int multiplier;
906         const int multiplier2 = 1000000;
907         int expr10_e_minus6, expr11_e_minus6;
908         int expr12, expr13;
909
910         dev_vdbg(data->hwmon_dev, "\n A_e_minus6=%d, B_e_minus2=%d, "
911                 "m_e_minus6=%d, n_e_minus6=%d, p_e_minus2=%d, "
912                 "temp=%d ", A_e_minus6, B_e_minus2, m_e_minus6,
913                 n_e_minus6, p_e_minus2, (int)temp);
914         expr1_e_minus6 = (DIV_ROUND_CLOSEST((2 * m_e_minus6 * B_e_minus2),
915                 100) + n_e_minus6);
916         dev_vdbg(data->hwmon_dev, "\n 2_m_B_plun_e_minus6=%d ",
917                 expr1_e_minus6);
918         expr2_e_minus6 = (DIV_ROUND_CLOSEST(expr1_e_minus6, 1000)) *
919                 (DIV_ROUND_CLOSEST(expr1_e_minus6, 1000));
920         dev_vdbg(data->hwmon_dev, "\n expr1^2=%d ", expr2_e_minus6);
921         expr3_e_minus4_1 = (DIV_ROUND_CLOSEST((
922                 (DIV_ROUND_CLOSEST((m_e_minus6 * B_e_minus2), 1000)) *
923                 (DIV_ROUND_CLOSEST(B_e_minus2, 10))
924                 ), 100));
925         dev_vdbg(data->hwmon_dev, "\n expr3_e_minus4_1=%d ",
926                 expr3_e_minus4_1);
927         expr3_e_minus4_2 = DIV_ROUND_CLOSEST(
928                 (DIV_ROUND_CLOSEST(n_e_minus6, 100) * B_e_minus2),
929                 100);
930         dev_vdbg(data->hwmon_dev, "\n expr3_e_minus4_2=%d ",
931                 expr3_e_minus4_2);
932         expr3_e_minus4 = expr3_e_minus4_1 + expr3_e_minus4_2;
933         dev_vdbg(data->hwmon_dev, "\n expr3=%d ", expr3_e_minus4);
934         expr4_e_minus2_1 = DIV_ROUND_CLOSEST((expr3_e_minus4 +
935                 (p_e_minus2 * 100)), 100);
936         dev_vdbg(data->hwmon_dev, "\n expr4_e_minus2_1=%d ",
937                 expr4_e_minus2_1);
938         expr4_e_minus6_2 = (4 * m_e_minus6);
939         dev_vdbg(data->hwmon_dev, "\n expr4_e_minus6_2=%d ",
940                 expr4_e_minus6_2);
941         expr4_e_minus6 = DIV_ROUND_CLOSEST((expr4_e_minus2_1 *
942                 expr4_e_minus6_2), 100);
943         dev_vdbg(data->hwmon_dev, "\n expr4_minus6=%d ", expr4_e_minus6);
944         expr5_e_minus6_1 = expr2_e_minus6 - expr4_e_minus6;
945         dev_vdbg(data->hwmon_dev, "\n expr5_e_minus6_1=%d ",
946                 expr5_e_minus6_1);
947         expr4_e_minus6_3 = (expr4_e_minus6_2 * temp);
948         dev_vdbg(data->hwmon_dev, "\n expr4_e_minus6_3=%d ",
949                 expr4_e_minus6_3);
950         expr5_e_minus6 = (expr5_e_minus6_1 + expr4_e_minus6_3);
951         dev_vdbg(data->hwmon_dev, "\n expr5_e_minus6=%d ",
952                 expr5_e_minus6);
953         multiplier = my_ceil_pow10(expr5_e_minus6);
954         dev_vdbg(data->hwmon_dev, "\n multiplier=%d ", multiplier);
955         expr6 = int_sqrt(expr5_e_minus6);
956         dev_vdbg(data->hwmon_dev, "\n sqrt top=%d ", expr6);
957         expr7 = int_sqrt(multiplier);
958         dev_vdbg(data->hwmon_dev, "\n sqrt bot=%d ", expr7);
959         if (expr7 == 0) {
960                 pr_err("\n Error: %s line=%d, expr7=%d ",
961                         __func__, __LINE__, expr7);
962                 return;
963         } else {
964                 expr8_e_minus6 = (expr6 * multiplier2) / expr7;
965         }
966         dev_vdbg(data->hwmon_dev, "\n sqrt final=%d ", expr8_e_minus6);
967         dev_vdbg(data->hwmon_dev, "\n 2_m_B_plus_n_e_minus6=%d ",
968                 expr1_e_minus6);
969         expr9_e_minus6 = DIV_ROUND_CLOSEST((2 * m_e_minus6 * A_e_minus6),
970                 1000000);
971         dev_vdbg(data->hwmon_dev, "\n denominator=%d ", expr9_e_minus6);
972         if (expr9_e_minus6 == 0) {
973                 pr_err("\n Error: %s line=%d, expr9_e_minus6=%d ",
974                         __func__, __LINE__, expr9_e_minus6);
975                 return;
976         }
977         expr10_e_minus6 = -expr1_e_minus6 - expr8_e_minus6;
978         dev_vdbg(data->hwmon_dev, "\n expr10_e_minus6=%d ",
979                 expr10_e_minus6);
980         expr11_e_minus6 = -expr1_e_minus6 + expr8_e_minus6;
981         dev_vdbg(data->hwmon_dev, "\n expr11_e_minus6=%d ",
982                 expr11_e_minus6);
983         expr12 = (expr10_e_minus6 / expr9_e_minus6);
984         dev_vdbg(data->hwmon_dev, "\ncounter1=%d ", expr12);
985         expr13 = (expr11_e_minus6 / expr9_e_minus6);
986         dev_vdbg(data->hwmon_dev, "\ncounter2=%d ", expr13);
987         *p_counter1 = expr12;
988         *p_counter2 = expr13;
989 }
990
991 /*
992  * function returns tsensor expected counter corresponding to input
993  * temperature in degree Celsius.
994  * e.g. for temperature of 35C, temp=35
995  */
996 static void tsensor_temp_2_count(struct tegra_tsensor_data *data,
997                                 int temp,
998                                 unsigned int *p_counter1,
999                                 unsigned int *p_counter2)
1000 {
1001         if (temp > 0) {
1002                 dev_dbg(data->hwmon_dev, "\n Trying to calculate counter"
1003                         " for requested temperature"
1004                         " threshold=%d ", temp);
1005                 /*
1006                  * calculate the constants needed to get roots of
1007                  * following quadratic eqn:
1008                  * m * A^2 * Counter^2 +
1009                  * A * (2 * m * B + n) * Counter +
1010                  * (m * B^2 + n * B + p - Temperature) = 0
1011                  */
1012                 get_quadratic_roots(data, temp, p_counter1, p_counter2);
1013                 /*
1014                  * checked at current temperature=35 the counter=11418
1015                  * for 50 deg temperature: counter1=22731, counter2=11817
1016                  * at 35 deg temperature: counter1=23137, counter2=11411
1017                  * hence, for above values we are assuming counter2 has
1018                  * the correct value
1019                  */
1020         } else {
1021                 if (temp == data->div2_temp) {
1022                         *p_counter1 = DEFAULT_THRESHOLD_TH2;
1023                         *p_counter2 = DEFAULT_THRESHOLD_TH2;
1024                 } else {
1025                         *p_counter1 = DEFAULT_THRESHOLD_TH3;
1026                         *p_counter2 = DEFAULT_THRESHOLD_TH3;
1027                 }
1028         }
1029 }
1030
1031 /*
1032  * function to compare computed and expected values with
1033  * certain tolerance setting hard coded here
1034  */
1035 static bool cmp_counter(
1036         struct tegra_tsensor_data *data,
1037         unsigned int actual, unsigned int exp)
1038 {
1039         unsigned int smaller;
1040         unsigned int larger;
1041         smaller = (actual > exp) ? exp : actual;
1042         larger = (smaller == actual) ? exp : actual;
1043         if ((larger - smaller) > TSENSOR_COUNTER_TOLERANCE) {
1044                 dev_dbg(data->hwmon_dev, "\n actual=%d, exp=%d, larger=%d, "
1045                 "smaller=%d, tolerance=%d ", actual, exp, larger, smaller,
1046                 TSENSOR_COUNTER_TOLERANCE);
1047                 return false;
1048         }
1049         return true;
1050 }
1051
1052 /* function to print chart of temperature to counter values */
1053 static void print_temperature_2_counter_table(
1054         struct tegra_tsensor_data *data)
1055 {
1056         int i;
1057         /* static list of temperature tested */
1058         unsigned int temp_list[] = {
1059                 30,
1060                 35,
1061                 40,
1062                 45,
1063                 50,
1064                 55,
1065                 60,
1066                 61,
1067                 62,
1068                 63,
1069                 64,
1070                 65,
1071                 70,
1072                 75,
1073                 80,
1074                 85,
1075                 90,
1076                 95,
1077                 100,
1078                 105,
1079                 110,
1080                 115,
1081                 120
1082         };
1083         unsigned int counter1, counter2;
1084         dev_dbg(data->hwmon_dev, "\n Temperature and counter1 and "
1085                 "counter2 chart ********** ");
1086         for (i = 0; i < ARRAY_SIZE(temp_list); i++) {
1087                 tsensor_temp_2_count(data, temp_list[i],
1088                         &counter1, &counter2);
1089                 dev_dbg(data->hwmon_dev, "\n temperature[%d]=%d, "
1090                         "counter1=0x%x, counter2=0x%x ",
1091                         i, temp_list[i], counter1, counter2);
1092         }
1093         dev_dbg(data->hwmon_dev, "\n\n ");
1094 }
1095
1096 /*
1097  * function to test if conversion of counter to temperature
1098  * and vice-versa is working
1099  */
1100 static bool test_temperature_algo(struct tegra_tsensor_data *data)
1101 {
1102         unsigned int actual_counter;
1103         unsigned int curr_avg, min_max;
1104         unsigned int counter1, counter2;
1105         unsigned int T1;
1106         int err;
1107         bool result1, result2;
1108         bool result = false;
1109
1110         /* read actual counter */
1111         err = tsensor_read_counter(data, tsensor_index, &curr_avg, &min_max);
1112         if (err < 0) {
1113                 pr_err("\n Error: tsensor0 counter read, err=%d ", err);
1114                 goto endLabel;
1115         }
1116         actual_counter = ((curr_avg & 0xFFFF0000) >> 16);
1117         dev_dbg(data->hwmon_dev, "\n counter read=0x%x ", actual_counter);
1118
1119         /* calculate temperature */
1120         err = tsensor_count_2_temp(data, actual_counter, &T1);
1121         dev_dbg(data->hwmon_dev, "\n %s actual counter=0x%x, calculated "
1122                 "temperature=%d.%d ", __func__,
1123                 actual_counter, get_temperature_int(T1),
1124                 get_temperature_fraction(T1));
1125         if (err < 0) {
1126                 pr_err("\n Error: calculate temperature step ");
1127                 goto endLabel;
1128         }
1129
1130         /* calculate counter corresponding to read temperature */
1131         tsensor_temp_2_count(data, get_temperature_round(T1),
1132                 &counter1, &counter2);
1133         dev_dbg(data->hwmon_dev, "\n given temperature=%d, counter1=0x%x,"
1134                 " counter2=0x%x ",
1135                 get_temperature_round(T1), counter1, counter2);
1136
1137         /* compare counter calculated with actual original counter */
1138         result1 = cmp_counter(data, actual_counter, counter1);
1139         result2 = cmp_counter(data, actual_counter, counter2);
1140         if (result1) {
1141                 dev_dbg(data->hwmon_dev, "\n counter1 matches: actual=%d,"
1142                         " calc=%d ", actual_counter, counter1);
1143                 result = true;
1144         }
1145         if (result2) {
1146                 dev_dbg(data->hwmon_dev, "\n counter2 matches: actual=%d,"
1147                         " calc=%d ", actual_counter, counter2);
1148                 result = true;
1149         }
1150
1151 endLabel:
1152         return result;
1153 }
1154
1155 /* tsensor threshold temperature to threshold counter conversion function */
1156 static unsigned int tsensor_get_threshold_counter(
1157         struct tegra_tsensor_data *data,
1158         unsigned int temp_threshold)
1159 {
1160         unsigned int counter1, counter2;
1161         unsigned int curr_avg, min_max;
1162         unsigned int counter;
1163         int err;
1164
1165         tsensor_temp_2_count(data, temp_threshold, &counter1, &counter2);
1166         err = tsensor_read_counter(data, tsensor_index, &curr_avg, &min_max);
1167         if (err < 0) {
1168                 pr_err("\n Error: tsensor0 counter read, err=%d ", err);
1169                 return MAX_THRESHOLD;
1170         }
1171         if (counter2 > ((curr_avg & 0xFFFF0000) >> 16)) {
1172                 dev_dbg(data->hwmon_dev, "\n choosing counter 2=0x%x as "
1173                         "root ", counter2);
1174         } else {
1175                 pr_err("\n Warning: tsensor choosing counter 2=0x%x as "
1176                         "root, counter 1=0x%x, counter=0x%x ", counter2,
1177                         counter1, ((curr_avg & 0xFFFF0000) >> 16));
1178         }
1179         counter = counter2;
1180         return counter;
1181 }
1182
1183 /* tsensor temperature threshold setup function */
1184 static void tsensor_threshold_setup(
1185                 struct tegra_tsensor_data *data,
1186                 unsigned char index, bool is_default_threshold)
1187 {
1188         unsigned long config0;
1189         unsigned char i = index;
1190         unsigned int th2_count = DEFAULT_THRESHOLD_TH2;
1191         unsigned int th3_count = DEFAULT_THRESHOLD_TH3;
1192         unsigned int th1_count = DEFAULT_THRESHOLD_TH1;
1193         unsigned int hysteresis_count;
1194         int th0_diff = (DEFAULT_THRESHOLD_TH1 - MIN_THRESHOLD);
1195
1196         dev_dbg(data->hwmon_dev, "\n started tsensor_threshold_setup %d ",
1197                 index);
1198         config0 = tsensor_readl(data, ((i << 16) | SENSOR_CFG0));
1199
1200         /* Choose thresholds for sensor0 and sensor1 */
1201         /* set to very high values initially - DEFAULT_THRESHOLD */
1202         if ((!is_default_threshold) && (i == tsensor_index)) {
1203                 dev_dbg(data->hwmon_dev, "\n before div2 temp_2_count ");
1204                 th2_count = tsensor_get_threshold_counter(data,
1205                         data->div2_temp);
1206                 dev_dbg(data->hwmon_dev, "\n div2_temp=%d, count=%d ",
1207                         data->div2_temp, th2_count);
1208                 dev_dbg(data->hwmon_dev, "\n before reset temp_2_count ");
1209                 th3_count = tsensor_get_threshold_counter(data,
1210                         data->reset_temp);
1211                 dev_dbg(data->hwmon_dev, "\n reset_temp=%d, count=%d ",
1212                         (unsigned int)data->reset_temp, th3_count);
1213                 dev_dbg(data->hwmon_dev, "\n before sw_intr temp_2_count ");
1214                 th1_count = tsensor_get_threshold_counter(data,
1215                         data->sw_intr_temp);
1216                 dev_dbg(data->hwmon_dev, "\n sw_intr_temp=%d, count=%d ",
1217                         (unsigned int)data->sw_intr_temp, th1_count);
1218                 dev_dbg(data->hwmon_dev, "\n before hysteresis temp_2_count ");
1219                 hysteresis_count = tsensor_get_threshold_counter(data,
1220                         (data->sw_intr_temp - data->hysteresis));
1221                 dev_dbg(data->hwmon_dev, "\n hysteresis_temp=%d, count=%d ",
1222                         (unsigned int)(data->sw_intr_temp - data->hysteresis),
1223                         hysteresis_count);
1224                 th0_diff = th1_count - hysteresis_count;
1225                 dev_dbg(data->hwmon_dev, "\n th0_diff=%d ", th0_diff);
1226         }
1227         dev_dbg(data->hwmon_dev, "\n before threshold program TH dump: ");
1228         dump_threshold(data);
1229         dev_dbg(data->hwmon_dev, "\n th3=0x%x, th2=0x%x, th1=0x%x, th0=0x%x ",
1230                 th3_count, th2_count, th1_count, th0_diff);
1231         config0 = (((th2_count & SENSOR_CFG_X_TH_X_MASK)
1232                 << SENSOR_CFG1_TH2_SHIFT) |
1233                 ((th1_count & SENSOR_CFG_X_TH_X_MASK) <<
1234                 SENSOR_CFG1_TH1_SHIFT));
1235         tsensor_writel(data, config0, ((i << 16) | SENSOR_CFG1));
1236         config0 = (((th0_diff & SENSOR_CFG_X_TH_X_MASK)
1237                 << SENSOR_CFG2_TH0_SHIFT) |
1238                 ((th3_count & SENSOR_CFG_X_TH_X_MASK) <<
1239                 SENSOR_CFG2_TH3_SHIFT));
1240         tsensor_writel(data, config0, ((i << 16) | SENSOR_CFG2));
1241         dev_dbg(data->hwmon_dev, "\n after threshold program TH dump: ");
1242         dump_threshold(data);
1243 }
1244
1245 /* tsensor config programming function */
1246 static int tsensor_config_setup(struct tegra_tsensor_data *data)
1247 {
1248         unsigned int config0;
1249         unsigned int i;
1250         unsigned int status_reg;
1251         unsigned int no_resp_count;
1252         int err;
1253         struct tsensor_state curr_state;
1254
1255         for (i = 0; i < TSENSOR_COUNT; i++) {
1256                 /*
1257                  * Pre-read setup:
1258                  * Set M and N values
1259                  * Enable HW features HW_FREQ_DIV_EN, THERMAL_RST_EN
1260                  */
1261                 config0 = tsensor_readl(data, ((i << 16) | SENSOR_CFG0));
1262                 config0 &= ~((SENSOR_CFG0_M_MASK << SENSOR_CFG0_M_SHIFT) |
1263                         (SENSOR_CFG0_N_MASK << SENSOR_CFG0_N_SHIFT) |
1264                         (1 << SENSOR_CFG0_OVERFLOW_INTR) |
1265                         (1 << SENSOR_CFG0_RST_INTR_SHIFT) |
1266                         (1 << SENSOR_CFG0_HW_DIV2_INTR_SHIFT) |
1267                         (1 << SENSOR_CFG0_RST_ENABLE_SHIFT) |
1268                         (1 << SENSOR_CFG0_HW_DIV2_ENABLE_SHIFT)
1269                         );
1270                 /* Set STOP bit */
1271                 /* Set M and N values */
1272                 /* Enable HW features HW_FREQ_DIV_EN, THERMAL_RST_EN */
1273                 config0 |= (((DEFAULT_TSENSOR_M & SENSOR_CFG0_M_MASK) <<
1274                         SENSOR_CFG0_M_SHIFT) |
1275                         ((DEFAULT_TSENSOR_N & SENSOR_CFG0_N_MASK) <<
1276                         SENSOR_CFG0_N_SHIFT) |
1277                         (1 << SENSOR_CFG0_OVERFLOW_INTR) |
1278                         /*(1 << SENSOR_CFG0_RST_INTR_SHIFT) |
1279                         (1 << SENSOR_CFG0_HW_DIV2_INTR_SHIFT) |*/
1280                         (1 << SENSOR_CFG0_STOP_SHIFT) |
1281                         (1 << SENSOR_CFG0_RST_ENABLE_SHIFT) |
1282                         (1 << SENSOR_CFG0_HW_DIV2_ENABLE_SHIFT));
1283
1284                 tsensor_writel(data, config0, ((i << 16) | SENSOR_CFG0));
1285                 tsensor_threshold_setup(data, i, true);
1286         }
1287
1288         for (i = 0; i < TSENSOR_COUNT; i++) {
1289                 config0 = tsensor_readl(data, ((i << 16) | SENSOR_CFG0));
1290                 /* Enables interrupts and clears sensor stop */
1291                 /*
1292                  * Interrupts not enabled as software handling is not
1293                  * needed in rev1 driver
1294                  */
1295                 /* Disable sensor stop bit */
1296                 config0 &= ~(1 << SENSOR_CFG0_STOP_SHIFT);
1297                 tsensor_writel(data, config0, ((i << 16) | SENSOR_CFG0));
1298         }
1299
1300         /* Check if counters are getting updated */
1301         no_resp_count = 0;
1302
1303         for (i = 0; i < TSENSOR_COUNT; i++) {
1304                 /* if STOP bit is set skip this check */
1305                 config0 = tsensor_readl(data, ((i << 16) | SENSOR_CFG0));
1306                 if (!(config0 & (1 << SENSOR_CFG0_STOP_SHIFT))) {
1307                         unsigned int loop_count = 0;
1308                         do {
1309                                 status_reg = tsensor_readl(data,
1310                                         (i << 16) | SENSOR_STATUS0);
1311                                 if ((status_reg & (1 <<
1312                                         SENSOR_STATUS_AVG_VALID_SHIFT)) &&
1313                                         (status_reg & (1 <<
1314                                         SENSOR_STATUS_CURR_VALID_SHIFT))) {
1315                                         msleep(1);
1316                                         loop_count++;
1317                                         if (!(loop_count % 200))
1318                                                 dev_err(data->hwmon_dev,
1319                                                 "\n Warning: Tsensor Counter "
1320                                                 "sensor%d not Valid yet. ", i);
1321                                         if (loop_count > MAX_TSENSOR_LOOP1) {
1322                                                 no_resp_count++;
1323                                                 break;
1324                                         }
1325                                 }
1326                         } while (!(status_reg &
1327                                 (1 << SENSOR_STATUS_AVG_VALID_SHIFT)) ||
1328                                 (!(status_reg &
1329                                 (1 << SENSOR_STATUS_CURR_VALID_SHIFT))));
1330                         if (no_resp_count == TSENSOR_COUNT)
1331                                 return -ENODEV;
1332                 }
1333                 /* check initial state */
1334                 get_ts_state(data, (unsigned char)i, &curr_state);
1335                 data->ts_state_saved[i] = curr_state.state;
1336         }
1337         /* initialize tsensor chip coefficients */
1338         err = get_chip_tsensor_coeff();
1339         return err;
1340 }
1341
1342 /* function to enable tsensor clock */
1343 static int tsensor_clk_enable(
1344         struct tegra_tsensor_data *data,
1345         bool enable)
1346 {
1347         int err = 0;
1348         unsigned long rate;
1349         struct clk *clk_m;
1350
1351         if (enable) {
1352                 clk_enable(data->dev_clk);
1353                 rate = clk_get_rate(data->dev_clk);
1354                 clk_m = clk_get_sys(NULL, "clk_m");
1355                 if (clk_get_parent(data->dev_clk) != clk_m) {
1356                         err = clk_set_parent(data->dev_clk, clk_m);
1357                         if (err < 0)
1358                                 goto fail;
1359                 }
1360                 rate = DEFAULT_TSENSOR_CLK_HZ;
1361                 if (rate != clk_get_rate(clk_m)) {
1362                         err = clk_set_rate(data->dev_clk, rate);
1363                         if (err < 0)
1364                                 goto fail;
1365                 }
1366         } else {
1367                 clk_disable(data->dev_clk);
1368                 clk_put(data->dev_clk);
1369         }
1370 fail:
1371         return err;
1372 }
1373
1374 /*
1375  * This function enables the tsensor using default configuration
1376  * 1. We would need some configuration APIs to calibrate
1377  *    the tsensor counters to right temperature
1378  * 2. hardware triggered divide cpu clock by 2 as well pmu reset is enabled
1379  *    implementation. No software actions are enabled at this point
1380  */
1381 static int tegra_tsensor_setup(struct platform_device *pdev)
1382 {
1383         struct tegra_tsensor_data *data = platform_get_drvdata(pdev);
1384         struct resource *r;
1385         int err;
1386         struct tegra_tsensor_platform_data *tsensor_data;
1387
1388         data->dev_clk = clk_get(&pdev->dev, NULL);
1389         if ((!data->dev_clk) || ((int)data->dev_clk == -(ENOENT))) {
1390                 dev_err(&pdev->dev, "Couldn't get the clock\n");
1391                 err = PTR_ERR(data->dev_clk);
1392                 goto fail;
1393         }
1394
1395         /* Enable tsensor clock */
1396         err = tsensor_clk_enable(data, true);
1397         if (err < 0)
1398                 goto err_irq;
1399
1400         /* Reset tsensor */
1401         dev_dbg(&pdev->dev, "\n before tsensor reset %s ", __func__);
1402         tegra_periph_reset_assert(data->dev_clk);
1403         msleep(1);
1404         tegra_periph_reset_deassert(data->dev_clk);
1405         msleep(1);
1406
1407         dev_dbg(&pdev->dev, "\n before tsensor chk pmc reset %s ",
1408                 __func__);
1409         /* Check for previous resets in pmc */
1410         if (pmc_check_rst_sensor(data)) {
1411                 dev_err(data->hwmon_dev, "\n Warning: ***** Last PMC "
1412                         "Reset source: tsensor detected ");
1413         }
1414
1415         dev_dbg(&pdev->dev, "\n before tsensor pmc reset enable %s ",
1416                 __func__);
1417         /* Enable the sensor reset in PMC */
1418         pmc_rst_enable(data, true);
1419
1420         /* register interrupt */
1421         r = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1422         if (!r) {
1423                 dev_err(&pdev->dev, "Failed to get IRQ\n");
1424                 err = -ENXIO;
1425                 goto err_irq;
1426         }
1427         data->irq = r->start;
1428         err = request_irq(data->irq, tegra_tsensor_isr,
1429                         IRQF_DISABLED, pdev->name, data);
1430         if (err < 0) {
1431                 dev_err(&pdev->dev, "Failed to register IRQ\n");
1432                 goto err_irq;
1433         }
1434
1435         /* tsensor thresholds are read from board/platform specific files */
1436         dev_dbg(&pdev->dev, "\n before tsensor get platform data %s ",
1437                 __func__);
1438         tsensor_data = pdev->dev.platform_data;
1439         dev_dbg(&pdev->dev, "\n tsensor platform_data=0x%x ",
1440                 (unsigned int)pdev->dev.platform_data);
1441         dev_dbg(&pdev->dev, "\n clk_div temperature=%d ",
1442                 tsensor_data->hw_clk_div_temperature);
1443         data->div2_temp = tsensor_data->hw_clk_div_temperature;
1444         dev_dbg(&pdev->dev, "\n reset temperature=%d ",
1445                 tsensor_data->hw_reset_temperature);
1446         data->reset_temp = tsensor_data->hw_reset_temperature;
1447         dev_dbg(&pdev->dev, "\n sw_intr temperature=%d ",
1448                 tsensor_data->sw_intr_temperature);
1449         data->sw_intr_temp = tsensor_data->sw_intr_temperature;
1450         dev_dbg(&pdev->dev, "\n hysteresis temperature=%d ",
1451                 tsensor_data->hysteresis);
1452         data->hysteresis = tsensor_data->hysteresis;
1453
1454         dev_dbg(&pdev->dev, "\n before tsensor_config_setup ");
1455         if (tsensor_config_setup(data)) {
1456                 dev_err(&pdev->dev, " [%s,line=%d]: tsensor counters dead! ",
1457                         __func__, __LINE__);
1458                 goto err_setup;
1459         }
1460         dev_dbg(&pdev->dev, "\n before tsensor_get_const_AB ");
1461         /* calculate constants needed for temperature conversion */
1462         err = tsensor_get_const_AB(data);
1463         if (err < 0) {
1464                 dev_err(&pdev->dev, " Failed to extract temperature "
1465                         "const\n");
1466                 goto err_setup;
1467         }
1468
1469         /* test if counter-to-temperature and temperature-to-counter
1470          * are matching */
1471         if (!test_temperature_algo(data)) {
1472                 dev_err(&pdev->dev, " Error: read temperature "
1473                         "algorithm broken ");
1474                 err = -EIO;
1475                 goto err_setup;
1476         }
1477
1478         print_temperature_2_counter_table(data);
1479
1480         dev_dbg(&pdev->dev, "\n before tsensor_threshold_setup ");
1481         /* change tsensor threshold for active instance */
1482         tsensor_threshold_setup(data, tsensor_index, false);
1483         dev_dbg(&pdev->dev, "\n end tsensor_threshold_setup ");
1484
1485         return 0;
1486 err_setup:
1487         free_irq(data->irq, data);
1488 err_irq:
1489         tsensor_clk_enable(data, false);
1490 fail:
1491         dev_err(&pdev->dev, "\n %s error=%d returned ", __func__, err);
1492         return err;
1493 }
1494
1495 static int __devinit tegra_tsensor_probe(struct platform_device *pdev)
1496 {
1497         struct tegra_tsensor_data *data;
1498         struct resource *r;
1499         int err;
1500         unsigned int reg;
1501
1502         data = kzalloc(sizeof(struct tegra_tsensor_data), GFP_KERNEL);
1503         if (!data) {
1504                 dev_err(&pdev->dev, " [%s,line=%d]: Failed to allocate "
1505                         "memory\n", __func__, __LINE__);
1506                 err = -ENOMEM;
1507                 goto exit;
1508         }
1509         platform_set_drvdata(pdev, data);
1510
1511         /* Register sysfs hooks */
1512         err = sysfs_create_group(&pdev->dev.kobj, &tsensor_attr_group);
1513         if (err < 0)
1514                 goto err0;
1515
1516         data->hwmon_dev = hwmon_device_register(&pdev->dev);
1517         if (IS_ERR(data->hwmon_dev)) {
1518                 err = PTR_ERR(data->hwmon_dev);
1519                 goto err0b;
1520         }
1521
1522         dev_set_drvdata(data->hwmon_dev, data);
1523
1524         spin_lock_init(&data->tsensor_lock);
1525
1526         /* map tsensor register space */
1527         r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1528         if (r == NULL) {
1529                 dev_err(&pdev->dev, " [%s,line=%d]: Failed to get io "
1530                         "resource\n", __func__, __LINE__);
1531                 err = -ENODEV;
1532                 goto err0b;
1533         }
1534
1535         if (!request_mem_region(r->start, (r->end - r->start) + 1,
1536                                 dev_name(&pdev->dev))) {
1537                 dev_err(&pdev->dev, " [%s,line=%d]: Error mem busy\n",
1538                         __func__, __LINE__);
1539                 err = -EBUSY;
1540                 goto err0b;
1541         }
1542
1543         data->phys = r->start;
1544         data->phys_end = r->end;
1545         data->base = ioremap(r->start, r->end - r->start + 1);
1546         if (!data->base) {
1547                 dev_err(&pdev->dev, " [%s, line=%d]: can't ioremap "
1548                         "tsensor iomem\n", __FILE__, __LINE__);
1549                 err = -ENOMEM;
1550                 goto err1;
1551         }
1552
1553         /* map pmc rst_status register  */
1554         r = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1555         if (r == NULL) {
1556                 dev_err(&pdev->dev, " [%s,line=%d]: Failed to get io "
1557                         "resource\n", __func__, __LINE__);
1558                 err = -ENODEV;
1559                 goto err2;
1560         }
1561
1562         if (!request_mem_region(r->start, (r->end - r->start) + 1,
1563                                 dev_name(&pdev->dev))) {
1564                 dev_err(&pdev->dev, " [%s, line=%d]: Error mem busy\n",
1565                         __func__, __LINE__);
1566                 err = -EBUSY;
1567                 goto err2;
1568         }
1569
1570         data->pmc_phys = r->start;
1571         data->pmc_phys_end = r->end;
1572         data->pmc_rst_base = ioremap(r->start, r->end - r->start + 1);
1573         if (!data->pmc_rst_base) {
1574                 dev_err(&pdev->dev, " [%s, line=%d]: can't ioremap "
1575                         "pmc iomem\n", __FILE__, __LINE__);
1576                 err = -ENOMEM;
1577                 goto err3;
1578         }
1579
1580         /* tsensor active instance decided based on fuse revision */
1581         err = tegra_fuse_get_revision(&reg);
1582         if (err)
1583                 goto err4;
1584         /* instance 1 is used for fuse revision 8 till 20 */
1585         /* instance 0 is used for fuse revision 21 onwards */
1586         if ((reg & 0xf) >= TSENSOR_FUSE_REVISION_DECIMAL_REV1)
1587                 tsensor_index = 1;
1588         else if ((reg & 0xf) >= TSENSOR_FUSE_REVISION_DECIMAL_REV2)
1589                 tsensor_index = 0;
1590         pr_info("\n tsensor active instance=%d ", tsensor_index);
1591
1592         /* tegra tsensor - setup and init */
1593         if (tegra_tsensor_setup(pdev) != 0)
1594                 goto err4;
1595
1596         dev_dbg(&pdev->dev, "\n end tegra_tsensor_probe ");
1597         return 0;
1598 err4:
1599         iounmap(data->pmc_rst_base);
1600 err3:
1601         release_mem_region(data->pmc_phys, (data->pmc_phys_end -
1602                 data->pmc_phys) + 1);
1603 err2:
1604         iounmap(data->base);
1605 err1:
1606         release_mem_region(data->phys, (data->phys_end -
1607                 data->phys) + 1);
1608 err0b:
1609         sysfs_remove_group(&pdev->dev.kobj, &tsensor_attr_group);
1610         hwmon_device_unregister(data->hwmon_dev);
1611
1612 err0:
1613         kfree(data);
1614 exit:
1615         dev_err(&pdev->dev, "\n %s error=%d returned ", __func__, err);
1616         return err;
1617 }
1618
1619 static int __devexit tegra_tsensor_remove(struct platform_device *pdev)
1620 {
1621         struct tegra_tsensor_data *data = platform_get_drvdata(pdev);
1622
1623         hwmon_device_unregister(data->hwmon_dev);
1624
1625         free_irq(data->irq, data);
1626
1627         iounmap(data->pmc_rst_base);
1628         release_mem_region(data->pmc_phys, (data->pmc_phys_end -
1629                 data->pmc_phys) + 1);
1630         iounmap(data->base);
1631         release_mem_region(data->phys, (data->phys_end -
1632                 data->phys) + 1);
1633         sysfs_remove_group(&pdev->dev.kobj, &tsensor_attr_group);
1634         platform_set_drvdata(pdev, NULL);
1635         kfree(data);
1636
1637         return 0;
1638 }
1639
1640 static void dump_a_tsensor_reg(struct tegra_tsensor_data *data,
1641         unsigned int addr)
1642 {
1643         dev_dbg(data->hwmon_dev, "\n tsensor[%d][0x%x]: 0x%x ", (addr >> 16),
1644                 addr & 0xFFFF, tsensor_readl(data, addr));
1645 }
1646
1647 static void dump_tsensor_regs(struct tegra_tsensor_data *data)
1648 {
1649         int i;
1650         for (i = 0; i < TSENSOR_COUNT; i++) {
1651                 /* if STOP bit is set skip this check */
1652                 dump_a_tsensor_reg(data, ((i << 16) | SENSOR_CFG0));
1653                 dump_a_tsensor_reg(data, ((i << 16) | SENSOR_CFG1));
1654                 dump_a_tsensor_reg(data, ((i << 16) | SENSOR_CFG2));
1655                 dump_a_tsensor_reg(data, ((i << 16) | SENSOR_STATUS0));
1656                 dump_a_tsensor_reg(data, ((i << 16) | SENSOR_TS_STATUS1));
1657                 dump_a_tsensor_reg(data, ((i << 16) | SENSOR_TS_STATUS2));
1658                 dump_a_tsensor_reg(data, ((i << 16) | 0x0));
1659                 dump_a_tsensor_reg(data, ((i << 16) | 0x44));
1660                 dump_a_tsensor_reg(data, ((i << 16) | 0x50));
1661                 dump_a_tsensor_reg(data, ((i << 16) | 0x54));
1662                 dump_a_tsensor_reg(data, ((i << 16) | 0x64));
1663                 dump_a_tsensor_reg(data, ((i << 16) | 0x68));
1664         }
1665 }
1666
1667 static void save_tsensor_regs(struct tegra_tsensor_data *data)
1668 {
1669         int i;
1670         for (i = 0; i < TSENSOR_COUNT; i++) {
1671                 data->config0[i] = tsensor_readl(data,
1672                         ((i << 16) | SENSOR_CFG0));
1673                 data->config1[i] = tsensor_readl(data,
1674                         ((i << 16) | SENSOR_CFG1));
1675                 data->config2[i] = tsensor_readl(data,
1676                         ((i << 16) | SENSOR_CFG2));
1677         }
1678 }
1679
1680 static void restore_tsensor_regs(struct tegra_tsensor_data *data)
1681 {
1682         int i;
1683         for (i = 0; i < TSENSOR_COUNT; i++) {
1684                 tsensor_writel(data, data->config0[i],
1685                         ((i << 16) | SENSOR_CFG0));
1686                 tsensor_writel(data, data->config1[i],
1687                         ((i << 16) | SENSOR_CFG1));
1688                 tsensor_writel(data, data->config2[i],
1689                         ((i << 16) | SENSOR_CFG2));
1690         }
1691 }
1692
1693 #ifdef CONFIG_PM
1694 static int tsensor_suspend(struct platform_device *pdev,
1695         pm_message_t state)
1696 {
1697         struct tegra_tsensor_data *data = platform_get_drvdata(pdev);
1698         unsigned int config0;
1699         int i;
1700         dev_dbg(data->hwmon_dev, "\n tsensor before suspend reg dump: ");
1701         dump_tsensor_regs(data);
1702         /* set STOP bit, else OVERFLOW interrupt seen in LP1 */
1703         for (i = 0; i < TSENSOR_COUNT; i++) {
1704                 config0 = tsensor_readl(data, ((i << 16) | SENSOR_CFG0));
1705                 config0 |= (1 << SENSOR_CFG0_STOP_SHIFT);
1706                 tsensor_writel(data, config0, ((i << 16) | SENSOR_CFG0));
1707         }
1708         /* save current settings before suspend, when STOP bit is set */
1709         save_tsensor_regs(data);
1710         tsensor_clk_enable(data, false);
1711
1712         return 0;
1713 }
1714
1715 static int tsensor_resume(struct platform_device *pdev)
1716 {
1717         struct tegra_tsensor_data *data = platform_get_drvdata(pdev);
1718         unsigned int config0;
1719         int i;
1720         tsensor_clk_enable(data, true);
1721         /* restore current settings before suspend, no need
1722          * to clear STOP bit */
1723         restore_tsensor_regs(data);
1724         /* clear STOP bit, after restoring regs */
1725         for (i = 0; i < TSENSOR_COUNT; i++) {
1726                 config0 = tsensor_readl(data, ((i << 16) | SENSOR_CFG0));
1727                 config0 &= ~(1 << SENSOR_CFG0_STOP_SHIFT);
1728                 tsensor_writel(data, config0, ((i << 16) | SENSOR_CFG0));
1729         }
1730         msleep(1);
1731         dev_dbg(data->hwmon_dev, "\n tsensor after resume reg dump: ");
1732         dump_tsensor_regs(data);
1733
1734         return 0;
1735 }
1736 #endif
1737
1738 static struct platform_driver tegra_tsensor_driver = {
1739         .driver = {
1740                 .owner  = THIS_MODULE,
1741                 .name   = "tegra-tsensor",
1742         },
1743         .probe          = tegra_tsensor_probe,
1744         .remove         = __devexit_p(tegra_tsensor_remove),
1745 #ifdef CONFIG_PM
1746         .suspend        = tsensor_suspend,
1747         .resume         = tsensor_resume,
1748 #endif
1749 };
1750
1751 static int __init tegra_tsensor_init(void)
1752 {
1753         init_flag = 0;
1754         if (platform_driver_register(&tegra_tsensor_driver))
1755                 goto exit;
1756         init_flag = 1;
1757         return 0;
1758
1759 exit:
1760         return -ENODEV;
1761 }
1762
1763 static void __exit tegra_tsensor_exit(void)
1764 {
1765         if (init_flag) {
1766                 platform_driver_unregister(&tegra_tsensor_driver);
1767                 init_flag = 0;
1768         }
1769 }
1770
1771 MODULE_AUTHOR("nvidia");
1772 MODULE_DESCRIPTION("Nvidia Tegra Temperature Sensor driver");
1773 MODULE_LICENSE("GPL");
1774
1775 module_init(tegra_tsensor_init);
1776 module_exit(tegra_tsensor_exit);
1777