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