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