1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * TI Bandgap temperature sensor driver 4 * 5 * Copyright (C) 2011-2012 Texas Instruments Incorporated - http://www.ti.com/ 6 * Author: J Keerthy <j-keerthy@ti.com> 7 * Author: Moiz Sonasath <m-sonasath@ti.com> 8 * Couple of fixes, DT and MFD adaptation: 9 * Eduardo Valentin <eduardo.valentin@ti.com> 10 */ 11 12 #include <linux/module.h> 13 #include <linux/export.h> 14 #include <linux/init.h> 15 #include <linux/kernel.h> 16 #include <linux/interrupt.h> 17 #include <linux/clk.h> 18 #include <linux/gpio/consumer.h> 19 #include <linux/platform_device.h> 20 #include <linux/err.h> 21 #include <linux/types.h> 22 #include <linux/spinlock.h> 23 #include <linux/sys_soc.h> 24 #include <linux/reboot.h> 25 #include <linux/of_device.h> 26 #include <linux/of_platform.h> 27 #include <linux/of_irq.h> 28 #include <linux/io.h> 29 #include <linux/iopoll.h> 30 #include <linux/cpu_pm.h> 31 #include <linux/device.h> 32 #include <linux/pm_runtime.h> 33 #include <linux/pm.h> 34 #include <linux/of.h> 35 #include <linux/of_device.h> 36 37 #include "ti-bandgap.h" 38 39 static int ti_bandgap_force_single_read(struct ti_bandgap *bgp, int id); 40 #ifdef CONFIG_PM_SLEEP 41 static int bandgap_omap_cpu_notifier(struct notifier_block *nb, 42 unsigned long cmd, void *v); 43 #endif 44 45 /*** Helper functions to access registers and their bitfields ***/ 46 47 /** 48 * ti_bandgap_readl() - simple read helper function 49 * @bgp: pointer to ti_bandgap structure 50 * @reg: desired register (offset) to be read 51 * 52 * Helper function to read bandgap registers. It uses the io remapped area. 53 * Return: the register value. 54 */ 55 static u32 ti_bandgap_readl(struct ti_bandgap *bgp, u32 reg) 56 { 57 return readl(bgp->base + reg); 58 } 59 60 /** 61 * ti_bandgap_writel() - simple write helper function 62 * @bgp: pointer to ti_bandgap structure 63 * @val: desired register value to be written 64 * @reg: desired register (offset) to be written 65 * 66 * Helper function to write bandgap registers. It uses the io remapped area. 67 */ 68 static void ti_bandgap_writel(struct ti_bandgap *bgp, u32 val, u32 reg) 69 { 70 writel(val, bgp->base + reg); 71 } 72 73 /** 74 * DOC: macro to update bits. 75 * 76 * RMW_BITS() - used to read, modify and update bandgap bitfields. 77 * The value passed will be shifted. 78 */ 79 #define RMW_BITS(bgp, id, reg, mask, val) \ 80 do { \ 81 struct temp_sensor_registers *t; \ 82 u32 r; \ 83 \ 84 t = bgp->conf->sensors[(id)].registers; \ 85 r = ti_bandgap_readl(bgp, t->reg); \ 86 r &= ~t->mask; \ 87 r |= (val) << __ffs(t->mask); \ 88 ti_bandgap_writel(bgp, r, t->reg); \ 89 } while (0) 90 91 /*** Basic helper functions ***/ 92 93 /** 94 * ti_bandgap_power() - controls the power state of a bandgap device 95 * @bgp: pointer to ti_bandgap structure 96 * @on: desired power state (1 - on, 0 - off) 97 * 98 * Used to power on/off a bandgap device instance. Only used on those 99 * that features tempsoff bit. 100 * 101 * Return: 0 on success, -ENOTSUPP if tempsoff is not supported. 102 */ 103 static int ti_bandgap_power(struct ti_bandgap *bgp, bool on) 104 { 105 int i; 106 107 if (!TI_BANDGAP_HAS(bgp, POWER_SWITCH)) 108 return -ENOTSUPP; 109 110 for (i = 0; i < bgp->conf->sensor_count; i++) 111 /* active on 0 */ 112 RMW_BITS(bgp, i, temp_sensor_ctrl, bgap_tempsoff_mask, !on); 113 return 0; 114 } 115 116 /** 117 * ti_errata814_bandgap_read_temp() - helper function to read dra7 sensor temperature 118 * @bgp: pointer to ti_bandgap structure 119 * @reg: desired register (offset) to be read 120 * 121 * Function to read dra7 bandgap sensor temperature. This is done separately 122 * so as to workaround the errata "Bandgap Temperature read Dtemp can be 123 * corrupted" - Errata ID: i814". 124 * Read accesses to registers listed below can be corrupted due to incorrect 125 * resynchronization between clock domains. 126 * Read access to registers below can be corrupted : 127 * CTRL_CORE_DTEMP_MPU/GPU/CORE/DSPEVE/IVA_n (n = 0 to 4) 128 * CTRL_CORE_TEMP_SENSOR_MPU/GPU/CORE/DSPEVE/IVA_n 129 * 130 * Return: the register value. 131 */ 132 static u32 ti_errata814_bandgap_read_temp(struct ti_bandgap *bgp, u32 reg) 133 { 134 u32 val1, val2; 135 136 val1 = ti_bandgap_readl(bgp, reg); 137 val2 = ti_bandgap_readl(bgp, reg); 138 139 /* If both times we read the same value then that is right */ 140 if (val1 == val2) 141 return val1; 142 143 /* if val1 and val2 are different read it third time */ 144 return ti_bandgap_readl(bgp, reg); 145 } 146 147 /** 148 * ti_bandgap_read_temp() - helper function to read sensor temperature 149 * @bgp: pointer to ti_bandgap structure 150 * @id: bandgap sensor id 151 * 152 * Function to concentrate the steps to read sensor temperature register. 153 * This function is desired because, depending on bandgap device version, 154 * it might be needed to freeze the bandgap state machine, before fetching 155 * the register value. 156 * 157 * Return: temperature in ADC values. 158 */ 159 static u32 ti_bandgap_read_temp(struct ti_bandgap *bgp, int id) 160 { 161 struct temp_sensor_registers *tsr; 162 u32 temp, reg; 163 164 tsr = bgp->conf->sensors[id].registers; 165 reg = tsr->temp_sensor_ctrl; 166 167 if (TI_BANDGAP_HAS(bgp, FREEZE_BIT)) { 168 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 1); 169 /* 170 * In case we cannot read from cur_dtemp / dtemp_0, 171 * then we read from the last valid temp read 172 */ 173 reg = tsr->ctrl_dtemp_1; 174 } 175 176 /* read temperature */ 177 if (TI_BANDGAP_HAS(bgp, ERRATA_814)) 178 temp = ti_errata814_bandgap_read_temp(bgp, reg); 179 else 180 temp = ti_bandgap_readl(bgp, reg); 181 182 temp &= tsr->bgap_dtemp_mask; 183 184 if (TI_BANDGAP_HAS(bgp, FREEZE_BIT)) 185 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 0); 186 187 return temp; 188 } 189 190 /*** IRQ handlers ***/ 191 192 /** 193 * ti_bandgap_talert_irq_handler() - handles Temperature alert IRQs 194 * @irq: IRQ number 195 * @data: private data (struct ti_bandgap *) 196 * 197 * This is the Talert handler. Use it only if bandgap device features 198 * HAS(TALERT). This handler goes over all sensors and checks their 199 * conditions and acts accordingly. In case there are events pending, 200 * it will reset the event mask to wait for the opposite event (next event). 201 * Every time there is a new event, it will be reported to thermal layer. 202 * 203 * Return: IRQ_HANDLED 204 */ 205 static irqreturn_t ti_bandgap_talert_irq_handler(int irq, void *data) 206 { 207 struct ti_bandgap *bgp = data; 208 struct temp_sensor_registers *tsr; 209 u32 t_hot = 0, t_cold = 0, ctrl; 210 int i; 211 212 spin_lock(&bgp->lock); 213 for (i = 0; i < bgp->conf->sensor_count; i++) { 214 tsr = bgp->conf->sensors[i].registers; 215 ctrl = ti_bandgap_readl(bgp, tsr->bgap_status); 216 217 /* Read the status of t_hot */ 218 t_hot = ctrl & tsr->status_hot_mask; 219 220 /* Read the status of t_cold */ 221 t_cold = ctrl & tsr->status_cold_mask; 222 223 if (!t_cold && !t_hot) 224 continue; 225 226 ctrl = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl); 227 /* 228 * One TALERT interrupt: Two sources 229 * If the interrupt is due to t_hot then mask t_hot and 230 * and unmask t_cold else mask t_cold and unmask t_hot 231 */ 232 if (t_hot) { 233 ctrl &= ~tsr->mask_hot_mask; 234 ctrl |= tsr->mask_cold_mask; 235 } else if (t_cold) { 236 ctrl &= ~tsr->mask_cold_mask; 237 ctrl |= tsr->mask_hot_mask; 238 } 239 240 ti_bandgap_writel(bgp, ctrl, tsr->bgap_mask_ctrl); 241 242 dev_dbg(bgp->dev, 243 "%s: IRQ from %s sensor: hotevent %d coldevent %d\n", 244 __func__, bgp->conf->sensors[i].domain, 245 t_hot, t_cold); 246 247 /* report temperature to whom may concern */ 248 if (bgp->conf->report_temperature) 249 bgp->conf->report_temperature(bgp, i); 250 } 251 spin_unlock(&bgp->lock); 252 253 return IRQ_HANDLED; 254 } 255 256 /** 257 * ti_bandgap_tshut_irq_handler() - handles Temperature shutdown signal 258 * @irq: IRQ number 259 * @data: private data (unused) 260 * 261 * This is the Tshut handler. Use it only if bandgap device features 262 * HAS(TSHUT). If any sensor fires the Tshut signal, we simply shutdown 263 * the system. 264 * 265 * Return: IRQ_HANDLED 266 */ 267 static irqreturn_t ti_bandgap_tshut_irq_handler(int irq, void *data) 268 { 269 pr_emerg("%s: TSHUT temperature reached. Needs shut down...\n", 270 __func__); 271 272 orderly_poweroff(true); 273 274 return IRQ_HANDLED; 275 } 276 277 /*** Helper functions which manipulate conversion ADC <-> mi Celsius ***/ 278 279 /** 280 * ti_bandgap_adc_to_mcelsius() - converts an ADC value to mCelsius scale 281 * @bgp: struct ti_bandgap pointer 282 * @adc_val: value in ADC representation 283 * @t: address where to write the resulting temperature in mCelsius 284 * 285 * Simple conversion from ADC representation to mCelsius. In case the ADC value 286 * is out of the ADC conv table range, it returns -ERANGE, 0 on success. 287 * The conversion table is indexed by the ADC values. 288 * 289 * Return: 0 if conversion was successful, else -ERANGE in case the @adc_val 290 * argument is out of the ADC conv table range. 291 */ 292 static 293 int ti_bandgap_adc_to_mcelsius(struct ti_bandgap *bgp, int adc_val, int *t) 294 { 295 const struct ti_bandgap_data *conf = bgp->conf; 296 297 /* look up for temperature in the table and return the temperature */ 298 if (adc_val < conf->adc_start_val || adc_val > conf->adc_end_val) 299 return -ERANGE; 300 301 *t = bgp->conf->conv_table[adc_val - conf->adc_start_val]; 302 return 0; 303 } 304 305 /** 306 * ti_bandgap_validate() - helper to check the sanity of a struct ti_bandgap 307 * @bgp: struct ti_bandgap pointer 308 * @id: bandgap sensor id 309 * 310 * Checks if the bandgap pointer is valid and if the sensor id is also 311 * applicable. 312 * 313 * Return: 0 if no errors, -EINVAL for invalid @bgp pointer or -ERANGE if 314 * @id cannot index @bgp sensors. 315 */ 316 static inline int ti_bandgap_validate(struct ti_bandgap *bgp, int id) 317 { 318 if (!bgp || IS_ERR(bgp)) { 319 pr_err("%s: invalid bandgap pointer\n", __func__); 320 return -EINVAL; 321 } 322 323 if ((id < 0) || (id >= bgp->conf->sensor_count)) { 324 dev_err(bgp->dev, "%s: sensor id out of range (%d)\n", 325 __func__, id); 326 return -ERANGE; 327 } 328 329 return 0; 330 } 331 332 /** 333 * ti_bandgap_read_counter() - read the sensor counter 334 * @bgp: pointer to bandgap instance 335 * @id: sensor id 336 * @interval: resulting update interval in miliseconds 337 */ 338 static void ti_bandgap_read_counter(struct ti_bandgap *bgp, int id, 339 int *interval) 340 { 341 struct temp_sensor_registers *tsr; 342 int time; 343 344 tsr = bgp->conf->sensors[id].registers; 345 time = ti_bandgap_readl(bgp, tsr->bgap_counter); 346 time = (time & tsr->counter_mask) >> 347 __ffs(tsr->counter_mask); 348 time = time * 1000 / bgp->clk_rate; 349 *interval = time; 350 } 351 352 /** 353 * ti_bandgap_read_counter_delay() - read the sensor counter delay 354 * @bgp: pointer to bandgap instance 355 * @id: sensor id 356 * @interval: resulting update interval in miliseconds 357 */ 358 static void ti_bandgap_read_counter_delay(struct ti_bandgap *bgp, int id, 359 int *interval) 360 { 361 struct temp_sensor_registers *tsr; 362 int reg_val; 363 364 tsr = bgp->conf->sensors[id].registers; 365 366 reg_val = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl); 367 reg_val = (reg_val & tsr->mask_counter_delay_mask) >> 368 __ffs(tsr->mask_counter_delay_mask); 369 switch (reg_val) { 370 case 0: 371 *interval = 0; 372 break; 373 case 1: 374 *interval = 1; 375 break; 376 case 2: 377 *interval = 10; 378 break; 379 case 3: 380 *interval = 100; 381 break; 382 case 4: 383 *interval = 250; 384 break; 385 case 5: 386 *interval = 500; 387 break; 388 default: 389 dev_warn(bgp->dev, "Wrong counter delay value read from register %X", 390 reg_val); 391 } 392 } 393 394 /** 395 * ti_bandgap_read_update_interval() - read the sensor update interval 396 * @bgp: pointer to bandgap instance 397 * @id: sensor id 398 * @interval: resulting update interval in miliseconds 399 * 400 * Return: 0 on success or the proper error code 401 */ 402 int ti_bandgap_read_update_interval(struct ti_bandgap *bgp, int id, 403 int *interval) 404 { 405 int ret = 0; 406 407 ret = ti_bandgap_validate(bgp, id); 408 if (ret) 409 goto exit; 410 411 if (!TI_BANDGAP_HAS(bgp, COUNTER) && 412 !TI_BANDGAP_HAS(bgp, COUNTER_DELAY)) { 413 ret = -ENOTSUPP; 414 goto exit; 415 } 416 417 if (TI_BANDGAP_HAS(bgp, COUNTER)) { 418 ti_bandgap_read_counter(bgp, id, interval); 419 goto exit; 420 } 421 422 ti_bandgap_read_counter_delay(bgp, id, interval); 423 exit: 424 return ret; 425 } 426 427 /** 428 * ti_bandgap_write_counter_delay() - set the counter_delay 429 * @bgp: pointer to bandgap instance 430 * @id: sensor id 431 * @interval: desired update interval in miliseconds 432 * 433 * Return: 0 on success or the proper error code 434 */ 435 static int ti_bandgap_write_counter_delay(struct ti_bandgap *bgp, int id, 436 u32 interval) 437 { 438 int rval; 439 440 switch (interval) { 441 case 0: /* Immediate conversion */ 442 rval = 0x0; 443 break; 444 case 1: /* Conversion after ever 1ms */ 445 rval = 0x1; 446 break; 447 case 10: /* Conversion after ever 10ms */ 448 rval = 0x2; 449 break; 450 case 100: /* Conversion after ever 100ms */ 451 rval = 0x3; 452 break; 453 case 250: /* Conversion after ever 250ms */ 454 rval = 0x4; 455 break; 456 case 500: /* Conversion after ever 500ms */ 457 rval = 0x5; 458 break; 459 default: 460 dev_warn(bgp->dev, "Delay %d ms is not supported\n", interval); 461 return -EINVAL; 462 } 463 464 spin_lock(&bgp->lock); 465 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_counter_delay_mask, rval); 466 spin_unlock(&bgp->lock); 467 468 return 0; 469 } 470 471 /** 472 * ti_bandgap_write_counter() - set the bandgap sensor counter 473 * @bgp: pointer to bandgap instance 474 * @id: sensor id 475 * @interval: desired update interval in miliseconds 476 */ 477 static void ti_bandgap_write_counter(struct ti_bandgap *bgp, int id, 478 u32 interval) 479 { 480 interval = interval * bgp->clk_rate / 1000; 481 spin_lock(&bgp->lock); 482 RMW_BITS(bgp, id, bgap_counter, counter_mask, interval); 483 spin_unlock(&bgp->lock); 484 } 485 486 /** 487 * ti_bandgap_write_update_interval() - set the update interval 488 * @bgp: pointer to bandgap instance 489 * @id: sensor id 490 * @interval: desired update interval in miliseconds 491 * 492 * Return: 0 on success or the proper error code 493 */ 494 int ti_bandgap_write_update_interval(struct ti_bandgap *bgp, 495 int id, u32 interval) 496 { 497 int ret = ti_bandgap_validate(bgp, id); 498 if (ret) 499 goto exit; 500 501 if (!TI_BANDGAP_HAS(bgp, COUNTER) && 502 !TI_BANDGAP_HAS(bgp, COUNTER_DELAY)) { 503 ret = -ENOTSUPP; 504 goto exit; 505 } 506 507 if (TI_BANDGAP_HAS(bgp, COUNTER)) { 508 ti_bandgap_write_counter(bgp, id, interval); 509 goto exit; 510 } 511 512 ret = ti_bandgap_write_counter_delay(bgp, id, interval); 513 exit: 514 return ret; 515 } 516 517 /** 518 * ti_bandgap_read_temperature() - report current temperature 519 * @bgp: pointer to bandgap instance 520 * @id: sensor id 521 * @temperature: resulting temperature 522 * 523 * Return: 0 on success or the proper error code 524 */ 525 int ti_bandgap_read_temperature(struct ti_bandgap *bgp, int id, 526 int *temperature) 527 { 528 u32 temp; 529 int ret; 530 531 ret = ti_bandgap_validate(bgp, id); 532 if (ret) 533 return ret; 534 535 if (!TI_BANDGAP_HAS(bgp, MODE_CONFIG)) { 536 ret = ti_bandgap_force_single_read(bgp, id); 537 if (ret) 538 return ret; 539 } 540 541 spin_lock(&bgp->lock); 542 temp = ti_bandgap_read_temp(bgp, id); 543 spin_unlock(&bgp->lock); 544 545 ret = ti_bandgap_adc_to_mcelsius(bgp, temp, &temp); 546 if (ret) 547 return -EIO; 548 549 *temperature = temp; 550 551 return 0; 552 } 553 554 /** 555 * ti_bandgap_set_sensor_data() - helper function to store thermal 556 * framework related data. 557 * @bgp: pointer to bandgap instance 558 * @id: sensor id 559 * @data: thermal framework related data to be stored 560 * 561 * Return: 0 on success or the proper error code 562 */ 563 int ti_bandgap_set_sensor_data(struct ti_bandgap *bgp, int id, void *data) 564 { 565 int ret = ti_bandgap_validate(bgp, id); 566 if (ret) 567 return ret; 568 569 bgp->regval[id].data = data; 570 571 return 0; 572 } 573 574 /** 575 * ti_bandgap_get_sensor_data() - helper function to get thermal 576 * framework related data. 577 * @bgp: pointer to bandgap instance 578 * @id: sensor id 579 * 580 * Return: data stored by set function with sensor id on success or NULL 581 */ 582 void *ti_bandgap_get_sensor_data(struct ti_bandgap *bgp, int id) 583 { 584 int ret = ti_bandgap_validate(bgp, id); 585 if (ret) 586 return ERR_PTR(ret); 587 588 return bgp->regval[id].data; 589 } 590 591 /*** Helper functions used during device initialization ***/ 592 593 /** 594 * ti_bandgap_force_single_read() - executes 1 single ADC conversion 595 * @bgp: pointer to struct ti_bandgap 596 * @id: sensor id which it is desired to read 1 temperature 597 * 598 * Used to initialize the conversion state machine and set it to a valid 599 * state. Called during device initialization and context restore events. 600 * 601 * Return: 0 602 */ 603 static int 604 ti_bandgap_force_single_read(struct ti_bandgap *bgp, int id) 605 { 606 struct temp_sensor_registers *tsr = bgp->conf->sensors[id].registers; 607 void __iomem *temp_sensor_ctrl = bgp->base + tsr->temp_sensor_ctrl; 608 int error; 609 u32 val; 610 611 /* Select continuous or single conversion mode */ 612 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG)) { 613 if (TI_BANDGAP_HAS(bgp, CONT_MODE_ONLY)) 614 RMW_BITS(bgp, id, bgap_mode_ctrl, mode_ctrl_mask, 1); 615 else 616 RMW_BITS(bgp, id, bgap_mode_ctrl, mode_ctrl_mask, 0); 617 } 618 619 /* Set Start of Conversion if available */ 620 if (tsr->bgap_soc_mask) { 621 RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 1); 622 623 /* Wait for EOCZ going up */ 624 error = readl_poll_timeout_atomic(temp_sensor_ctrl, val, 625 val & tsr->bgap_eocz_mask, 626 1, 1000); 627 if (error) 628 dev_warn(bgp->dev, "eocz timed out waiting high\n"); 629 630 /* Clear Start of Conversion if available */ 631 RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 0); 632 } 633 634 /* Wait for EOCZ going down, always needed even if no bgap_soc_mask */ 635 error = readl_poll_timeout_atomic(temp_sensor_ctrl, val, 636 !(val & tsr->bgap_eocz_mask), 637 1, 1500); 638 if (error) 639 dev_warn(bgp->dev, "eocz timed out waiting low\n"); 640 641 return 0; 642 } 643 644 /** 645 * ti_bandgap_set_continuous_mode() - One time enabling of continuous mode 646 * @bgp: pointer to struct ti_bandgap 647 * 648 * Call this function only if HAS(MODE_CONFIG) is set. As this driver may 649 * be used for junction temperature monitoring, it is desirable that the 650 * sensors are operational all the time, so that alerts are generated 651 * properly. 652 * 653 * Return: 0 654 */ 655 static int ti_bandgap_set_continuous_mode(struct ti_bandgap *bgp) 656 { 657 int i; 658 659 for (i = 0; i < bgp->conf->sensor_count; i++) { 660 /* Perform a single read just before enabling continuous */ 661 ti_bandgap_force_single_read(bgp, i); 662 RMW_BITS(bgp, i, bgap_mode_ctrl, mode_ctrl_mask, 1); 663 } 664 665 return 0; 666 } 667 668 /** 669 * ti_bandgap_get_trend() - To fetch the temperature trend of a sensor 670 * @bgp: pointer to struct ti_bandgap 671 * @id: id of the individual sensor 672 * @trend: Pointer to trend. 673 * 674 * This function needs to be called to fetch the temperature trend of a 675 * Particular sensor. The function computes the difference in temperature 676 * w.r.t time. For the bandgaps with built in history buffer the temperatures 677 * are read from the buffer and for those without the Buffer -ENOTSUPP is 678 * returned. 679 * 680 * Return: 0 if no error, else return corresponding error. If no 681 * error then the trend value is passed on to trend parameter 682 */ 683 int ti_bandgap_get_trend(struct ti_bandgap *bgp, int id, int *trend) 684 { 685 struct temp_sensor_registers *tsr; 686 u32 temp1, temp2, reg1, reg2; 687 int t1, t2, interval, ret = 0; 688 689 ret = ti_bandgap_validate(bgp, id); 690 if (ret) 691 goto exit; 692 693 if (!TI_BANDGAP_HAS(bgp, HISTORY_BUFFER) || 694 !TI_BANDGAP_HAS(bgp, FREEZE_BIT)) { 695 ret = -ENOTSUPP; 696 goto exit; 697 } 698 699 spin_lock(&bgp->lock); 700 701 tsr = bgp->conf->sensors[id].registers; 702 703 /* Freeze and read the last 2 valid readings */ 704 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 1); 705 reg1 = tsr->ctrl_dtemp_1; 706 reg2 = tsr->ctrl_dtemp_2; 707 708 /* read temperature from history buffer */ 709 temp1 = ti_bandgap_readl(bgp, reg1); 710 temp1 &= tsr->bgap_dtemp_mask; 711 712 temp2 = ti_bandgap_readl(bgp, reg2); 713 temp2 &= tsr->bgap_dtemp_mask; 714 715 /* Convert from adc values to mCelsius temperature */ 716 ret = ti_bandgap_adc_to_mcelsius(bgp, temp1, &t1); 717 if (ret) 718 goto unfreeze; 719 720 ret = ti_bandgap_adc_to_mcelsius(bgp, temp2, &t2); 721 if (ret) 722 goto unfreeze; 723 724 /* Fetch the update interval */ 725 ret = ti_bandgap_read_update_interval(bgp, id, &interval); 726 if (ret) 727 goto unfreeze; 728 729 /* Set the interval to 1 ms if bandgap counter delay is not set */ 730 if (interval == 0) 731 interval = 1; 732 733 *trend = (t1 - t2) / interval; 734 735 dev_dbg(bgp->dev, "The temperatures are t1 = %d and t2 = %d and trend =%d\n", 736 t1, t2, *trend); 737 738 unfreeze: 739 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 0); 740 spin_unlock(&bgp->lock); 741 exit: 742 return ret; 743 } 744 745 /** 746 * ti_bandgap_tshut_init() - setup and initialize tshut handling 747 * @bgp: pointer to struct ti_bandgap 748 * @pdev: pointer to device struct platform_device 749 * 750 * Call this function only in case the bandgap features HAS(TSHUT). 751 * In this case, the driver needs to handle the TSHUT signal as an IRQ. 752 * The IRQ is wired as a GPIO, and for this purpose, it is required 753 * to specify which GPIO line is used. TSHUT IRQ is fired anytime 754 * one of the bandgap sensors violates the TSHUT high/hot threshold. 755 * And in that case, the system must go off. 756 * 757 * Return: 0 if no error, else error status 758 */ 759 static int ti_bandgap_tshut_init(struct ti_bandgap *bgp, 760 struct platform_device *pdev) 761 { 762 int status; 763 764 status = request_irq(gpiod_to_irq(bgp->tshut_gpiod), 765 ti_bandgap_tshut_irq_handler, 766 IRQF_TRIGGER_RISING, "tshut", NULL); 767 if (status) 768 dev_err(bgp->dev, "request irq failed for TSHUT"); 769 770 return 0; 771 } 772 773 /** 774 * ti_bandgap_alert_init() - setup and initialize talert handling 775 * @bgp: pointer to struct ti_bandgap 776 * @pdev: pointer to device struct platform_device 777 * 778 * Call this function only in case the bandgap features HAS(TALERT). 779 * In this case, the driver needs to handle the TALERT signals as an IRQs. 780 * TALERT is a normal IRQ and it is fired any time thresholds (hot or cold) 781 * are violated. In these situation, the driver must reprogram the thresholds, 782 * accordingly to specified policy. 783 * 784 * Return: 0 if no error, else return corresponding error. 785 */ 786 static int ti_bandgap_talert_init(struct ti_bandgap *bgp, 787 struct platform_device *pdev) 788 { 789 int ret; 790 791 bgp->irq = platform_get_irq(pdev, 0); 792 if (bgp->irq < 0) 793 return bgp->irq; 794 795 ret = request_threaded_irq(bgp->irq, NULL, 796 ti_bandgap_talert_irq_handler, 797 IRQF_TRIGGER_HIGH | IRQF_ONESHOT, 798 "talert", bgp); 799 if (ret) { 800 dev_err(&pdev->dev, "Request threaded irq failed.\n"); 801 return ret; 802 } 803 804 return 0; 805 } 806 807 static const struct of_device_id of_ti_bandgap_match[]; 808 /** 809 * ti_bandgap_build() - parse DT and setup a struct ti_bandgap 810 * @pdev: pointer to device struct platform_device 811 * 812 * Used to read the device tree properties accordingly to the bandgap 813 * matching version. Based on bandgap version and its capabilities it 814 * will build a struct ti_bandgap out of the required DT entries. 815 * 816 * Return: valid bandgap structure if successful, else returns ERR_PTR 817 * return value must be verified with IS_ERR. 818 */ 819 static struct ti_bandgap *ti_bandgap_build(struct platform_device *pdev) 820 { 821 struct device_node *node = pdev->dev.of_node; 822 const struct of_device_id *of_id; 823 struct ti_bandgap *bgp; 824 struct resource *res; 825 int i; 826 827 /* just for the sake */ 828 if (!node) { 829 dev_err(&pdev->dev, "no platform information available\n"); 830 return ERR_PTR(-EINVAL); 831 } 832 833 bgp = devm_kzalloc(&pdev->dev, sizeof(*bgp), GFP_KERNEL); 834 if (!bgp) 835 return ERR_PTR(-ENOMEM); 836 837 of_id = of_match_device(of_ti_bandgap_match, &pdev->dev); 838 if (of_id) 839 bgp->conf = of_id->data; 840 841 /* register shadow for context save and restore */ 842 bgp->regval = devm_kcalloc(&pdev->dev, bgp->conf->sensor_count, 843 sizeof(*bgp->regval), GFP_KERNEL); 844 if (!bgp->regval) 845 return ERR_PTR(-ENOMEM); 846 847 i = 0; 848 do { 849 void __iomem *chunk; 850 851 res = platform_get_resource(pdev, IORESOURCE_MEM, i); 852 if (!res) 853 break; 854 chunk = devm_ioremap_resource(&pdev->dev, res); 855 if (i == 0) 856 bgp->base = chunk; 857 if (IS_ERR(chunk)) 858 return ERR_CAST(chunk); 859 860 i++; 861 } while (res); 862 863 if (TI_BANDGAP_HAS(bgp, TSHUT)) { 864 bgp->tshut_gpiod = devm_gpiod_get(&pdev->dev, NULL, GPIOD_IN); 865 if (IS_ERR(bgp->tshut_gpiod)) { 866 dev_err(&pdev->dev, "invalid gpio for tshut\n"); 867 return ERR_CAST(bgp->tshut_gpiod); 868 } 869 } 870 871 return bgp; 872 } 873 874 /* 875 * List of SoCs on which the CPU PM notifier can cause erros on the DTEMP 876 * readout. 877 * Enabled notifier on these machines results in erroneous, random values which 878 * could trigger unexpected thermal shutdown. 879 */ 880 static const struct soc_device_attribute soc_no_cpu_notifier[] = { 881 { .machine = "OMAP4430" }, 882 { /* sentinel */ }, 883 }; 884 885 /*** Device driver call backs ***/ 886 887 static 888 int ti_bandgap_probe(struct platform_device *pdev) 889 { 890 struct ti_bandgap *bgp; 891 int clk_rate, ret, i; 892 893 bgp = ti_bandgap_build(pdev); 894 if (IS_ERR(bgp)) { 895 dev_err(&pdev->dev, "failed to fetch platform data\n"); 896 return PTR_ERR(bgp); 897 } 898 bgp->dev = &pdev->dev; 899 900 if (TI_BANDGAP_HAS(bgp, UNRELIABLE)) 901 dev_warn(&pdev->dev, 902 "This OMAP thermal sensor is unreliable. You've been warned\n"); 903 904 if (TI_BANDGAP_HAS(bgp, TSHUT)) { 905 ret = ti_bandgap_tshut_init(bgp, pdev); 906 if (ret) { 907 dev_err(&pdev->dev, 908 "failed to initialize system tshut IRQ\n"); 909 return ret; 910 } 911 } 912 913 bgp->fclock = clk_get(NULL, bgp->conf->fclock_name); 914 if (IS_ERR(bgp->fclock)) { 915 dev_err(&pdev->dev, "failed to request fclock reference\n"); 916 ret = PTR_ERR(bgp->fclock); 917 goto free_irqs; 918 } 919 920 bgp->div_clk = clk_get(NULL, bgp->conf->div_ck_name); 921 if (IS_ERR(bgp->div_clk)) { 922 dev_err(&pdev->dev, "failed to request div_ts_ck clock ref\n"); 923 ret = PTR_ERR(bgp->div_clk); 924 goto put_fclock; 925 } 926 927 for (i = 0; i < bgp->conf->sensor_count; i++) { 928 struct temp_sensor_registers *tsr; 929 u32 val; 930 931 tsr = bgp->conf->sensors[i].registers; 932 /* 933 * check if the efuse has a non-zero value if not 934 * it is an untrimmed sample and the temperatures 935 * may not be accurate 936 */ 937 val = ti_bandgap_readl(bgp, tsr->bgap_efuse); 938 if (!val) 939 dev_info(&pdev->dev, 940 "Non-trimmed BGAP, Temp not accurate\n"); 941 } 942 943 clk_rate = clk_round_rate(bgp->div_clk, 944 bgp->conf->sensors[0].ts_data->max_freq); 945 if (clk_rate < bgp->conf->sensors[0].ts_data->min_freq || 946 clk_rate <= 0) { 947 ret = -ENODEV; 948 dev_err(&pdev->dev, "wrong clock rate (%d)\n", clk_rate); 949 goto put_clks; 950 } 951 952 ret = clk_set_rate(bgp->div_clk, clk_rate); 953 if (ret) 954 dev_err(&pdev->dev, "Cannot re-set clock rate. Continuing\n"); 955 956 bgp->clk_rate = clk_rate; 957 if (TI_BANDGAP_HAS(bgp, CLK_CTRL)) 958 clk_prepare_enable(bgp->fclock); 959 960 961 spin_lock_init(&bgp->lock); 962 bgp->dev = &pdev->dev; 963 platform_set_drvdata(pdev, bgp); 964 965 ti_bandgap_power(bgp, true); 966 967 /* Set default counter to 1 for now */ 968 if (TI_BANDGAP_HAS(bgp, COUNTER)) 969 for (i = 0; i < bgp->conf->sensor_count; i++) 970 RMW_BITS(bgp, i, bgap_counter, counter_mask, 1); 971 972 /* Set default thresholds for alert and shutdown */ 973 for (i = 0; i < bgp->conf->sensor_count; i++) { 974 struct temp_sensor_data *ts_data; 975 976 ts_data = bgp->conf->sensors[i].ts_data; 977 978 if (TI_BANDGAP_HAS(bgp, TALERT)) { 979 /* Set initial Talert thresholds */ 980 RMW_BITS(bgp, i, bgap_threshold, 981 threshold_tcold_mask, ts_data->t_cold); 982 RMW_BITS(bgp, i, bgap_threshold, 983 threshold_thot_mask, ts_data->t_hot); 984 /* Enable the alert events */ 985 RMW_BITS(bgp, i, bgap_mask_ctrl, mask_hot_mask, 1); 986 RMW_BITS(bgp, i, bgap_mask_ctrl, mask_cold_mask, 1); 987 } 988 989 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG)) { 990 /* Set initial Tshut thresholds */ 991 RMW_BITS(bgp, i, tshut_threshold, 992 tshut_hot_mask, ts_data->tshut_hot); 993 RMW_BITS(bgp, i, tshut_threshold, 994 tshut_cold_mask, ts_data->tshut_cold); 995 } 996 } 997 998 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG)) 999 ti_bandgap_set_continuous_mode(bgp); 1000 1001 /* Set .250 seconds time as default counter */ 1002 if (TI_BANDGAP_HAS(bgp, COUNTER)) 1003 for (i = 0; i < bgp->conf->sensor_count; i++) 1004 RMW_BITS(bgp, i, bgap_counter, counter_mask, 1005 bgp->clk_rate / 4); 1006 1007 /* Every thing is good? Then expose the sensors */ 1008 for (i = 0; i < bgp->conf->sensor_count; i++) { 1009 char *domain; 1010 1011 if (bgp->conf->sensors[i].register_cooling) { 1012 ret = bgp->conf->sensors[i].register_cooling(bgp, i); 1013 if (ret) 1014 goto remove_sensors; 1015 } 1016 1017 if (bgp->conf->expose_sensor) { 1018 domain = bgp->conf->sensors[i].domain; 1019 ret = bgp->conf->expose_sensor(bgp, i, domain); 1020 if (ret) 1021 goto remove_last_cooling; 1022 } 1023 } 1024 1025 /* 1026 * Enable the Interrupts once everything is set. Otherwise irq handler 1027 * might be called as soon as it is enabled where as rest of framework 1028 * is still getting initialised. 1029 */ 1030 if (TI_BANDGAP_HAS(bgp, TALERT)) { 1031 ret = ti_bandgap_talert_init(bgp, pdev); 1032 if (ret) { 1033 dev_err(&pdev->dev, "failed to initialize Talert IRQ\n"); 1034 i = bgp->conf->sensor_count; 1035 goto disable_clk; 1036 } 1037 } 1038 1039 #ifdef CONFIG_PM_SLEEP 1040 bgp->nb.notifier_call = bandgap_omap_cpu_notifier; 1041 if (!soc_device_match(soc_no_cpu_notifier)) 1042 cpu_pm_register_notifier(&bgp->nb); 1043 #endif 1044 1045 return 0; 1046 1047 remove_last_cooling: 1048 if (bgp->conf->sensors[i].unregister_cooling) 1049 bgp->conf->sensors[i].unregister_cooling(bgp, i); 1050 remove_sensors: 1051 for (i--; i >= 0; i--) { 1052 if (bgp->conf->sensors[i].unregister_cooling) 1053 bgp->conf->sensors[i].unregister_cooling(bgp, i); 1054 if (bgp->conf->remove_sensor) 1055 bgp->conf->remove_sensor(bgp, i); 1056 } 1057 ti_bandgap_power(bgp, false); 1058 disable_clk: 1059 if (TI_BANDGAP_HAS(bgp, CLK_CTRL)) 1060 clk_disable_unprepare(bgp->fclock); 1061 put_clks: 1062 clk_put(bgp->div_clk); 1063 put_fclock: 1064 clk_put(bgp->fclock); 1065 free_irqs: 1066 if (TI_BANDGAP_HAS(bgp, TSHUT)) 1067 free_irq(gpiod_to_irq(bgp->tshut_gpiod), NULL); 1068 1069 return ret; 1070 } 1071 1072 static 1073 int ti_bandgap_remove(struct platform_device *pdev) 1074 { 1075 struct ti_bandgap *bgp = platform_get_drvdata(pdev); 1076 int i; 1077 1078 if (!soc_device_match(soc_no_cpu_notifier)) 1079 cpu_pm_unregister_notifier(&bgp->nb); 1080 1081 /* Remove sensor interfaces */ 1082 for (i = 0; i < bgp->conf->sensor_count; i++) { 1083 if (bgp->conf->sensors[i].unregister_cooling) 1084 bgp->conf->sensors[i].unregister_cooling(bgp, i); 1085 1086 if (bgp->conf->remove_sensor) 1087 bgp->conf->remove_sensor(bgp, i); 1088 } 1089 1090 ti_bandgap_power(bgp, false); 1091 1092 if (TI_BANDGAP_HAS(bgp, CLK_CTRL)) 1093 clk_disable_unprepare(bgp->fclock); 1094 clk_put(bgp->fclock); 1095 clk_put(bgp->div_clk); 1096 1097 if (TI_BANDGAP_HAS(bgp, TALERT)) 1098 free_irq(bgp->irq, bgp); 1099 1100 if (TI_BANDGAP_HAS(bgp, TSHUT)) 1101 free_irq(gpiod_to_irq(bgp->tshut_gpiod), NULL); 1102 1103 return 0; 1104 } 1105 1106 #ifdef CONFIG_PM_SLEEP 1107 static int ti_bandgap_save_ctxt(struct ti_bandgap *bgp) 1108 { 1109 int i; 1110 1111 for (i = 0; i < bgp->conf->sensor_count; i++) { 1112 struct temp_sensor_registers *tsr; 1113 struct temp_sensor_regval *rval; 1114 1115 rval = &bgp->regval[i]; 1116 tsr = bgp->conf->sensors[i].registers; 1117 1118 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG)) 1119 rval->bg_mode_ctrl = ti_bandgap_readl(bgp, 1120 tsr->bgap_mode_ctrl); 1121 if (TI_BANDGAP_HAS(bgp, COUNTER)) 1122 rval->bg_counter = ti_bandgap_readl(bgp, 1123 tsr->bgap_counter); 1124 if (TI_BANDGAP_HAS(bgp, TALERT)) { 1125 rval->bg_threshold = ti_bandgap_readl(bgp, 1126 tsr->bgap_threshold); 1127 rval->bg_ctrl = ti_bandgap_readl(bgp, 1128 tsr->bgap_mask_ctrl); 1129 } 1130 1131 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG)) 1132 rval->tshut_threshold = ti_bandgap_readl(bgp, 1133 tsr->tshut_threshold); 1134 } 1135 1136 return 0; 1137 } 1138 1139 static int ti_bandgap_restore_ctxt(struct ti_bandgap *bgp) 1140 { 1141 int i; 1142 1143 for (i = 0; i < bgp->conf->sensor_count; i++) { 1144 struct temp_sensor_registers *tsr; 1145 struct temp_sensor_regval *rval; 1146 u32 val = 0; 1147 1148 rval = &bgp->regval[i]; 1149 tsr = bgp->conf->sensors[i].registers; 1150 1151 if (TI_BANDGAP_HAS(bgp, COUNTER)) 1152 val = ti_bandgap_readl(bgp, tsr->bgap_counter); 1153 1154 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG)) 1155 ti_bandgap_writel(bgp, rval->tshut_threshold, 1156 tsr->tshut_threshold); 1157 /* Force immediate temperature measurement and update 1158 * of the DTEMP field 1159 */ 1160 ti_bandgap_force_single_read(bgp, i); 1161 1162 if (TI_BANDGAP_HAS(bgp, COUNTER)) 1163 ti_bandgap_writel(bgp, rval->bg_counter, 1164 tsr->bgap_counter); 1165 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG)) 1166 ti_bandgap_writel(bgp, rval->bg_mode_ctrl, 1167 tsr->bgap_mode_ctrl); 1168 if (TI_BANDGAP_HAS(bgp, TALERT)) { 1169 ti_bandgap_writel(bgp, rval->bg_threshold, 1170 tsr->bgap_threshold); 1171 ti_bandgap_writel(bgp, rval->bg_ctrl, 1172 tsr->bgap_mask_ctrl); 1173 } 1174 } 1175 1176 return 0; 1177 } 1178 1179 static int ti_bandgap_suspend(struct device *dev) 1180 { 1181 struct ti_bandgap *bgp = dev_get_drvdata(dev); 1182 int err; 1183 1184 err = ti_bandgap_save_ctxt(bgp); 1185 ti_bandgap_power(bgp, false); 1186 1187 if (TI_BANDGAP_HAS(bgp, CLK_CTRL)) 1188 clk_disable_unprepare(bgp->fclock); 1189 1190 bgp->is_suspended = true; 1191 1192 return err; 1193 } 1194 1195 static int bandgap_omap_cpu_notifier(struct notifier_block *nb, 1196 unsigned long cmd, void *v) 1197 { 1198 struct ti_bandgap *bgp; 1199 1200 bgp = container_of(nb, struct ti_bandgap, nb); 1201 1202 spin_lock(&bgp->lock); 1203 switch (cmd) { 1204 case CPU_CLUSTER_PM_ENTER: 1205 if (bgp->is_suspended) 1206 break; 1207 ti_bandgap_save_ctxt(bgp); 1208 ti_bandgap_power(bgp, false); 1209 if (TI_BANDGAP_HAS(bgp, CLK_CTRL)) 1210 clk_disable(bgp->fclock); 1211 break; 1212 case CPU_CLUSTER_PM_ENTER_FAILED: 1213 case CPU_CLUSTER_PM_EXIT: 1214 if (bgp->is_suspended) 1215 break; 1216 if (TI_BANDGAP_HAS(bgp, CLK_CTRL)) 1217 clk_enable(bgp->fclock); 1218 ti_bandgap_power(bgp, true); 1219 ti_bandgap_restore_ctxt(bgp); 1220 break; 1221 } 1222 spin_unlock(&bgp->lock); 1223 1224 return NOTIFY_OK; 1225 } 1226 1227 static int ti_bandgap_resume(struct device *dev) 1228 { 1229 struct ti_bandgap *bgp = dev_get_drvdata(dev); 1230 1231 if (TI_BANDGAP_HAS(bgp, CLK_CTRL)) 1232 clk_prepare_enable(bgp->fclock); 1233 1234 ti_bandgap_power(bgp, true); 1235 bgp->is_suspended = false; 1236 1237 return ti_bandgap_restore_ctxt(bgp); 1238 } 1239 static SIMPLE_DEV_PM_OPS(ti_bandgap_dev_pm_ops, ti_bandgap_suspend, 1240 ti_bandgap_resume); 1241 1242 #define DEV_PM_OPS (&ti_bandgap_dev_pm_ops) 1243 #else 1244 #define DEV_PM_OPS NULL 1245 #endif 1246 1247 static const struct of_device_id of_ti_bandgap_match[] = { 1248 #ifdef CONFIG_OMAP3_THERMAL 1249 { 1250 .compatible = "ti,omap34xx-bandgap", 1251 .data = (void *)&omap34xx_data, 1252 }, 1253 { 1254 .compatible = "ti,omap36xx-bandgap", 1255 .data = (void *)&omap36xx_data, 1256 }, 1257 #endif 1258 #ifdef CONFIG_OMAP4_THERMAL 1259 { 1260 .compatible = "ti,omap4430-bandgap", 1261 .data = (void *)&omap4430_data, 1262 }, 1263 { 1264 .compatible = "ti,omap4460-bandgap", 1265 .data = (void *)&omap4460_data, 1266 }, 1267 { 1268 .compatible = "ti,omap4470-bandgap", 1269 .data = (void *)&omap4470_data, 1270 }, 1271 #endif 1272 #ifdef CONFIG_OMAP5_THERMAL 1273 { 1274 .compatible = "ti,omap5430-bandgap", 1275 .data = (void *)&omap5430_data, 1276 }, 1277 #endif 1278 #ifdef CONFIG_DRA752_THERMAL 1279 { 1280 .compatible = "ti,dra752-bandgap", 1281 .data = (void *)&dra752_data, 1282 }, 1283 #endif 1284 /* Sentinel */ 1285 { }, 1286 }; 1287 MODULE_DEVICE_TABLE(of, of_ti_bandgap_match); 1288 1289 static struct platform_driver ti_bandgap_sensor_driver = { 1290 .probe = ti_bandgap_probe, 1291 .remove = ti_bandgap_remove, 1292 .driver = { 1293 .name = "ti-soc-thermal", 1294 .pm = DEV_PM_OPS, 1295 .of_match_table = of_ti_bandgap_match, 1296 }, 1297 }; 1298 1299 module_platform_driver(ti_bandgap_sensor_driver); 1300 1301 MODULE_DESCRIPTION("OMAP4+ bandgap temperature sensor driver"); 1302 MODULE_LICENSE("GPL v2"); 1303 MODULE_ALIAS("platform:ti-soc-thermal"); 1304 MODULE_AUTHOR("Texas Instrument Inc."); 1305