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