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