1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2015, The Linux Foundation. All rights reserved.
4 * Copyright (c) 2019, 2020, Linaro Ltd.
7 #include <linux/debugfs.h>
10 #include <linux/module.h>
11 #include <linux/nvmem-consumer.h>
13 #include <linux/of_address.h>
14 #include <linux/of_platform.h>
15 #include <linux/mfd/syscon.h>
16 #include <linux/platform_device.h>
18 #include <linux/regmap.h>
19 #include <linux/slab.h>
20 #include <linux/suspend.h>
21 #include <linux/thermal.h>
22 #include "../thermal_hwmon.h"
26 * struct tsens_irq_data - IRQ status and temperature violations
27 * @up_viol: upper threshold violated
28 * @up_thresh: upper threshold temperature value
29 * @up_irq_mask: mask register for upper threshold irqs
30 * @up_irq_clear: clear register for uppper threshold irqs
31 * @low_viol: lower threshold violated
32 * @low_thresh: lower threshold temperature value
33 * @low_irq_mask: mask register for lower threshold irqs
34 * @low_irq_clear: clear register for lower threshold irqs
35 * @crit_viol: critical threshold violated
36 * @crit_thresh: critical threshold temperature value
37 * @crit_irq_mask: mask register for critical threshold irqs
38 * @crit_irq_clear: clear register for critical threshold irqs
40 * Structure containing data about temperature threshold settings and
41 * irq status if they were violated.
43 struct tsens_irq_data
{
58 char *qfprom_read(struct device
*dev
, const char *cname
)
60 struct nvmem_cell
*cell
;
64 cell
= nvmem_cell_get(dev
, cname
);
66 return ERR_CAST(cell
);
68 ret
= nvmem_cell_read(cell
, &data
);
74 int tsens_read_calibration(struct tsens_priv
*priv
, int shift
, u32
*p1
, u32
*p2
, bool backup
)
78 char name
[] = "sXX_pY_backup"; /* s10_p1_backup */
81 if (priv
->num_sensors
> MAX_SENSORS
)
84 ret
= snprintf(name
, sizeof(name
), "mode%s", backup
? "_backup" : "");
88 ret
= nvmem_cell_read_variable_le_u32(priv
->dev
, name
, &mode
);
90 dev_warn(priv
->dev
, "Please migrate to separate nvmem cells for calibration data\n");
94 dev_dbg(priv
->dev
, "calibration mode is %d\n", mode
);
96 ret
= snprintf(name
, sizeof(name
), "base1%s", backup
? "_backup" : "");
100 ret
= nvmem_cell_read_variable_le_u32(priv
->dev
, name
, &base1
);
104 ret
= snprintf(name
, sizeof(name
), "base2%s", backup
? "_backup" : "");
108 ret
= nvmem_cell_read_variable_le_u32(priv
->dev
, name
, &base2
);
112 for (i
= 0; i
< priv
->num_sensors
; i
++) {
113 ret
= snprintf(name
, sizeof(name
), "s%d_p1%s", priv
->sensor
[i
].hw_id
,
114 backup
? "_backup" : "");
118 ret
= nvmem_cell_read_variable_le_u32(priv
->dev
, name
, &p1
[i
]);
122 ret
= snprintf(name
, sizeof(name
), "s%d_p2%s", priv
->sensor
[i
].hw_id
,
123 backup
? "_backup" : "");
127 ret
= nvmem_cell_read_variable_le_u32(priv
->dev
, name
, &p2
[i
]);
134 for (i
= 0; i
< priv
->num_sensors
; i
++)
135 p1
[i
] = p1
[i
] + (base1
<< shift
);
138 case TWO_PT_CALIB_NO_OFFSET
:
139 for (i
= 0; i
< priv
->num_sensors
; i
++)
140 p2
[i
] = (p2
[i
] + base2
) << shift
;
143 case ONE_PT_CALIB2_NO_OFFSET
:
144 for (i
= 0; i
< priv
->num_sensors
; i
++)
145 p1
[i
] = (p1
[i
] + base1
) << shift
;
148 dev_dbg(priv
->dev
, "calibrationless mode\n");
149 for (i
= 0; i
< priv
->num_sensors
; i
++) {
155 /* Apply calibration offset workaround except for _NO_OFFSET modes */
158 for (i
= 0; i
< priv
->num_sensors
; i
++)
159 p2
[i
] += priv
->sensor
[i
].p2_calib_offset
;
162 for (i
= 0; i
< priv
->num_sensors
; i
++)
163 p1
[i
] += priv
->sensor
[i
].p1_calib_offset
;
170 int tsens_calibrate_nvmem(struct tsens_priv
*priv
, int shift
)
172 u32 p1
[MAX_SENSORS
], p2
[MAX_SENSORS
];
175 mode
= tsens_read_calibration(priv
, shift
, p1
, p2
, false);
179 compute_intercept_slope(priv
, p1
, p2
, mode
);
184 int tsens_calibrate_common(struct tsens_priv
*priv
)
186 return tsens_calibrate_nvmem(priv
, 2);
189 static u32
tsens_read_cell(const struct tsens_single_value
*cell
, u8 len
, u32
*data0
, u32
*data1
)
192 u32
*data
= cell
->blob
? data1
: data0
;
194 if (cell
->shift
+ len
<= 32) {
195 val
= data
[cell
->idx
] >> cell
->shift
;
197 u8 part
= 32 - cell
->shift
;
199 val
= data
[cell
->idx
] >> cell
->shift
;
200 val
|= data
[cell
->idx
+ 1] << part
;
203 return val
& ((1 << len
) - 1);
206 int tsens_read_calibration_legacy(struct tsens_priv
*priv
,
207 const struct tsens_legacy_calibration_format
*format
,
209 u32
*cdata0
, u32
*cdata1
)
215 mode
= tsens_read_cell(&format
->mode
, 2, cdata0
, cdata1
);
216 invalid
= tsens_read_cell(&format
->invalid
, 1, cdata0
, cdata1
);
219 dev_dbg(priv
->dev
, "calibration mode is %d\n", mode
);
221 base1
= tsens_read_cell(&format
->base
[0], format
->base_len
, cdata0
, cdata1
);
222 base2
= tsens_read_cell(&format
->base
[1], format
->base_len
, cdata0
, cdata1
);
224 for (i
= 0; i
< priv
->num_sensors
; i
++) {
225 p1
[i
] = tsens_read_cell(&format
->sp
[i
][0], format
->sp_len
, cdata0
, cdata1
);
226 p2
[i
] = tsens_read_cell(&format
->sp
[i
][1], format
->sp_len
, cdata0
, cdata1
);
231 for (i
= 0; i
< priv
->num_sensors
; i
++)
232 p1
[i
] = p1
[i
] + (base1
<< format
->base_shift
);
235 for (i
= 0; i
< priv
->num_sensors
; i
++)
236 p2
[i
] = (p2
[i
] + base2
) << format
->base_shift
;
239 for (i
= 0; i
< priv
->num_sensors
; i
++)
240 p1
[i
] = (p1
[i
] + base1
) << format
->base_shift
;
243 dev_dbg(priv
->dev
, "calibrationless mode\n");
244 for (i
= 0; i
< priv
->num_sensors
; i
++) {
254 * Use this function on devices where slope and offset calculations
255 * depend on calibration data read from qfprom. On others the slope
256 * and offset values are derived from tz->tzp->slope and tz->tzp->offset
259 void compute_intercept_slope(struct tsens_priv
*priv
, u32
*p1
,
265 for (i
= 0; i
< priv
->num_sensors
; i
++) {
267 "%s: sensor%d - data_point1:%#x data_point2:%#x\n",
268 __func__
, i
, p1
[i
], p2
? p2
[i
] : 0);
270 if (!priv
->sensor
[i
].slope
)
271 priv
->sensor
[i
].slope
= SLOPE_DEFAULT
;
272 if (mode
== TWO_PT_CALIB
|| mode
== TWO_PT_CALIB_NO_OFFSET
) {
274 * slope (m) = adc_code2 - adc_code1 (y2 - y1)/
275 * temp_120_degc - temp_30_degc (x2 - x1)
279 den
= CAL_DEGC_PT2
- CAL_DEGC_PT1
;
280 priv
->sensor
[i
].slope
= num
/ den
;
283 priv
->sensor
[i
].offset
= (p1
[i
] * SLOPE_FACTOR
) -
285 priv
->sensor
[i
].slope
);
286 dev_dbg(priv
->dev
, "%s: offset:%d\n", __func__
,
287 priv
->sensor
[i
].offset
);
291 static inline u32
degc_to_code(int degc
, const struct tsens_sensor
*s
)
293 u64 code
= div_u64(((u64
)degc
* s
->slope
+ s
->offset
), SLOPE_FACTOR
);
295 pr_debug("%s: raw_code: 0x%llx, degc:%d\n", __func__
, code
, degc
);
296 return clamp_val(code
, THRESHOLD_MIN_ADC_CODE
, THRESHOLD_MAX_ADC_CODE
);
299 static inline int code_to_degc(u32 adc_code
, const struct tsens_sensor
*s
)
303 num
= (adc_code
* SLOPE_FACTOR
) - s
->offset
;
307 degc
= num
+ (den
/ 2);
309 degc
= num
- (den
/ 2);
319 * tsens_hw_to_mC - Return sign-extended temperature in mCelsius.
320 * @s: Pointer to sensor struct
321 * @field: Index into regmap_field array pointing to temperature data
323 * This function handles temperature returned in ADC code or deciCelsius
324 * depending on IP version.
326 * Return: Temperature in milliCelsius on success, a negative errno will
327 * be returned in error cases
329 static int tsens_hw_to_mC(const struct tsens_sensor
*s
, int field
)
331 struct tsens_priv
*priv
= s
->priv
;
336 resolution
= priv
->fields
[LAST_TEMP_0
].msb
-
337 priv
->fields
[LAST_TEMP_0
].lsb
;
339 ret
= regmap_field_read(priv
->rf
[field
], &temp
);
343 /* Convert temperature from ADC code to milliCelsius */
345 return code_to_degc(temp
, s
) * 1000;
347 /* deciCelsius -> milliCelsius along with sign extension */
348 return sign_extend32(temp
, resolution
) * 100;
352 * tsens_mC_to_hw - Convert temperature to hardware register value
353 * @s: Pointer to sensor struct
354 * @temp: temperature in milliCelsius to be programmed to hardware
356 * This function outputs the value to be written to hardware in ADC code
357 * or deciCelsius depending on IP version.
359 * Return: ADC code or temperature in deciCelsius.
361 static int tsens_mC_to_hw(const struct tsens_sensor
*s
, int temp
)
363 struct tsens_priv
*priv
= s
->priv
;
365 /* milliC to adc code */
367 return degc_to_code(temp
/ 1000, s
);
369 /* milliC to deciC */
373 static inline enum tsens_ver
tsens_version(struct tsens_priv
*priv
)
375 return priv
->feat
->ver_major
;
378 static void tsens_set_interrupt_v1(struct tsens_priv
*priv
, u32 hw_id
,
379 enum tsens_irq_type irq_type
, bool enable
)
385 index
= UP_INT_CLEAR_0
+ hw_id
;
388 index
= LOW_INT_CLEAR_0
+ hw_id
;
391 /* No critical interrupts before v2 */
394 regmap_field_write(priv
->rf
[index
], enable
? 0 : 1);
397 static void tsens_set_interrupt_v2(struct tsens_priv
*priv
, u32 hw_id
,
398 enum tsens_irq_type irq_type
, bool enable
)
400 u32 index_mask
= 0, index_clear
= 0;
403 * To enable the interrupt flag for a sensor:
404 * - clear the mask bit
405 * To disable the interrupt flag for a sensor:
406 * - Mask further interrupts for this sensor
407 * - Write 1 followed by 0 to clear the interrupt
411 index_mask
= UP_INT_MASK_0
+ hw_id
;
412 index_clear
= UP_INT_CLEAR_0
+ hw_id
;
415 index_mask
= LOW_INT_MASK_0
+ hw_id
;
416 index_clear
= LOW_INT_CLEAR_0
+ hw_id
;
419 index_mask
= CRIT_INT_MASK_0
+ hw_id
;
420 index_clear
= CRIT_INT_CLEAR_0
+ hw_id
;
425 regmap_field_write(priv
->rf
[index_mask
], 0);
427 regmap_field_write(priv
->rf
[index_mask
], 1);
428 regmap_field_write(priv
->rf
[index_clear
], 1);
429 regmap_field_write(priv
->rf
[index_clear
], 0);
434 * tsens_set_interrupt - Set state of an interrupt
435 * @priv: Pointer to tsens controller private data
436 * @hw_id: Hardware ID aka. sensor number
437 * @irq_type: irq_type from enum tsens_irq_type
438 * @enable: false = disable, true = enable
440 * Call IP-specific function to set state of an interrupt
444 static void tsens_set_interrupt(struct tsens_priv
*priv
, u32 hw_id
,
445 enum tsens_irq_type irq_type
, bool enable
)
447 dev_dbg(priv
->dev
, "[%u] %s: %s -> %s\n", hw_id
, __func__
,
448 irq_type
? ((irq_type
== 1) ? "UP" : "CRITICAL") : "LOW",
449 enable
? "en" : "dis");
450 if (tsens_version(priv
) > VER_1_X
)
451 tsens_set_interrupt_v2(priv
, hw_id
, irq_type
, enable
);
453 tsens_set_interrupt_v1(priv
, hw_id
, irq_type
, enable
);
457 * tsens_threshold_violated - Check if a sensor temperature violated a preset threshold
458 * @priv: Pointer to tsens controller private data
459 * @hw_id: Hardware ID aka. sensor number
460 * @d: Pointer to irq state data
462 * Return: 0 if threshold was not violated, 1 if it was violated and negative
463 * errno in case of errors
465 static int tsens_threshold_violated(struct tsens_priv
*priv
, u32 hw_id
,
466 struct tsens_irq_data
*d
)
470 ret
= regmap_field_read(priv
->rf
[UPPER_STATUS_0
+ hw_id
], &d
->up_viol
);
473 ret
= regmap_field_read(priv
->rf
[LOWER_STATUS_0
+ hw_id
], &d
->low_viol
);
477 if (priv
->feat
->crit_int
) {
478 ret
= regmap_field_read(priv
->rf
[CRITICAL_STATUS_0
+ hw_id
],
484 if (d
->up_viol
|| d
->low_viol
|| d
->crit_viol
)
490 static int tsens_read_irq_state(struct tsens_priv
*priv
, u32 hw_id
,
491 const struct tsens_sensor
*s
,
492 struct tsens_irq_data
*d
)
496 ret
= regmap_field_read(priv
->rf
[UP_INT_CLEAR_0
+ hw_id
], &d
->up_irq_clear
);
499 ret
= regmap_field_read(priv
->rf
[LOW_INT_CLEAR_0
+ hw_id
], &d
->low_irq_clear
);
502 if (tsens_version(priv
) > VER_1_X
) {
503 ret
= regmap_field_read(priv
->rf
[UP_INT_MASK_0
+ hw_id
], &d
->up_irq_mask
);
506 ret
= regmap_field_read(priv
->rf
[LOW_INT_MASK_0
+ hw_id
], &d
->low_irq_mask
);
509 ret
= regmap_field_read(priv
->rf
[CRIT_INT_CLEAR_0
+ hw_id
],
513 ret
= regmap_field_read(priv
->rf
[CRIT_INT_MASK_0
+ hw_id
],
518 d
->crit_thresh
= tsens_hw_to_mC(s
, CRIT_THRESH_0
+ hw_id
);
520 /* No mask register on older TSENS */
523 d
->crit_irq_clear
= 0;
524 d
->crit_irq_mask
= 0;
528 d
->up_thresh
= tsens_hw_to_mC(s
, UP_THRESH_0
+ hw_id
);
529 d
->low_thresh
= tsens_hw_to_mC(s
, LOW_THRESH_0
+ hw_id
);
531 dev_dbg(priv
->dev
, "[%u] %s%s: status(%u|%u|%u) | clr(%u|%u|%u) | mask(%u|%u|%u)\n",
533 (d
->up_viol
|| d
->low_viol
|| d
->crit_viol
) ? "(V)" : "",
534 d
->low_viol
, d
->up_viol
, d
->crit_viol
,
535 d
->low_irq_clear
, d
->up_irq_clear
, d
->crit_irq_clear
,
536 d
->low_irq_mask
, d
->up_irq_mask
, d
->crit_irq_mask
);
537 dev_dbg(priv
->dev
, "[%u] %s%s: thresh: (%d:%d:%d)\n", hw_id
, __func__
,
538 (d
->up_viol
|| d
->low_viol
|| d
->crit_viol
) ? "(V)" : "",
539 d
->low_thresh
, d
->up_thresh
, d
->crit_thresh
);
544 static inline u32
masked_irq(u32 hw_id
, u32 mask
, enum tsens_ver ver
)
547 return mask
& (1 << hw_id
);
549 /* v1, v0.1 don't have a irq mask register */
554 * tsens_critical_irq_thread() - Threaded handler for critical interrupts
556 * @data: tsens controller private data
558 * Check FSM watchdog bark status and clear if needed.
559 * Check all sensors to find ones that violated their critical threshold limits.
560 * Clear and then re-enable the interrupt.
562 * The level-triggered interrupt might deassert if the temperature returned to
563 * within the threshold limits by the time the handler got scheduled. We
564 * consider the irq to have been handled in that case.
566 * Return: IRQ_HANDLED
568 static irqreturn_t
tsens_critical_irq_thread(int irq
, void *data
)
570 struct tsens_priv
*priv
= data
;
571 struct tsens_irq_data d
;
573 u32 wdog_status
, wdog_count
;
575 if (priv
->feat
->has_watchdog
) {
576 ret
= regmap_field_read(priv
->rf
[WDOG_BARK_STATUS
],
582 /* Clear WDOG interrupt */
583 regmap_field_write(priv
->rf
[WDOG_BARK_CLEAR
], 1);
584 regmap_field_write(priv
->rf
[WDOG_BARK_CLEAR
], 0);
585 ret
= regmap_field_read(priv
->rf
[WDOG_BARK_COUNT
],
590 dev_dbg(priv
->dev
, "%s: watchdog count: %d\n",
591 __func__
, wdog_count
);
593 /* Fall through to handle critical interrupts if any */
597 for (i
= 0; i
< priv
->num_sensors
; i
++) {
598 const struct tsens_sensor
*s
= &priv
->sensor
[i
];
599 u32 hw_id
= s
->hw_id
;
603 if (!tsens_threshold_violated(priv
, hw_id
, &d
))
605 ret
= get_temp_tsens_valid(s
, &temp
);
607 dev_err(priv
->dev
, "[%u] %s: error reading sensor\n",
612 tsens_read_irq_state(priv
, hw_id
, s
, &d
);
614 !masked_irq(hw_id
, d
.crit_irq_mask
, tsens_version(priv
))) {
615 /* Mask critical interrupts, unused on Linux */
616 tsens_set_interrupt(priv
, hw_id
, CRITICAL
, false);
624 * tsens_irq_thread - Threaded interrupt handler for uplow interrupts
626 * @data: tsens controller private data
628 * Check all sensors to find ones that violated their threshold limits. If the
629 * temperature is still outside the limits, call thermal_zone_device_update() to
630 * update the thresholds, else re-enable the interrupts.
632 * The level-triggered interrupt might deassert if the temperature returned to
633 * within the threshold limits by the time the handler got scheduled. We
634 * consider the irq to have been handled in that case.
636 * Return: IRQ_HANDLED
638 static irqreturn_t
tsens_irq_thread(int irq
, void *data
)
640 struct tsens_priv
*priv
= data
;
641 struct tsens_irq_data d
;
644 for (i
= 0; i
< priv
->num_sensors
; i
++) {
645 const struct tsens_sensor
*s
= &priv
->sensor
[i
];
646 u32 hw_id
= s
->hw_id
;
650 if (!tsens_threshold_violated(priv
, hw_id
, &d
))
653 thermal_zone_device_update(s
->tzd
, THERMAL_EVENT_UNSPECIFIED
);
655 if (tsens_version(priv
) < VER_0_1
) {
656 /* Constraint: There is only 1 interrupt control register for all
657 * 11 temperature sensor. So monitoring more than 1 sensor based
658 * on interrupts will yield inconsistent result. To overcome this
659 * issue we will monitor only sensor 0 which is the master sensor.
669 * tsens_combined_irq_thread() - Threaded interrupt handler for combined interrupts
671 * @data: tsens controller private data
673 * Handle the combined interrupt as if it were 2 separate interrupts, so call the
674 * critical handler first and then the up/low one.
676 * Return: IRQ_HANDLED
678 static irqreturn_t
tsens_combined_irq_thread(int irq
, void *data
)
682 ret
= tsens_critical_irq_thread(irq
, data
);
683 if (ret
!= IRQ_HANDLED
)
686 return tsens_irq_thread(irq
, data
);
689 static int tsens_set_trips(struct thermal_zone_device
*tz
, int low
, int high
)
691 struct tsens_sensor
*s
= thermal_zone_device_priv(tz
);
692 struct tsens_priv
*priv
= s
->priv
;
693 struct device
*dev
= priv
->dev
;
694 struct tsens_irq_data d
;
696 int high_val
, low_val
, cl_high
, cl_low
;
697 u32 hw_id
= s
->hw_id
;
699 if (tsens_version(priv
) < VER_0_1
) {
700 /* Pre v0.1 IP had a single register for each type of interrupt
706 dev_dbg(dev
, "[%u] %s: proposed thresholds: (%d:%d)\n",
707 hw_id
, __func__
, low
, high
);
709 cl_high
= clamp_val(high
, priv
->feat
->trip_min_temp
, priv
->feat
->trip_max_temp
);
710 cl_low
= clamp_val(low
, priv
->feat
->trip_min_temp
, priv
->feat
->trip_max_temp
);
712 high_val
= tsens_mC_to_hw(s
, cl_high
);
713 low_val
= tsens_mC_to_hw(s
, cl_low
);
715 spin_lock_irqsave(&priv
->ul_lock
, flags
);
717 tsens_read_irq_state(priv
, hw_id
, s
, &d
);
719 /* Write the new thresholds and clear the status */
720 regmap_field_write(priv
->rf
[LOW_THRESH_0
+ hw_id
], low_val
);
721 regmap_field_write(priv
->rf
[UP_THRESH_0
+ hw_id
], high_val
);
722 tsens_set_interrupt(priv
, hw_id
, LOWER
, true);
723 tsens_set_interrupt(priv
, hw_id
, UPPER
, true);
725 spin_unlock_irqrestore(&priv
->ul_lock
, flags
);
727 dev_dbg(dev
, "[%u] %s: (%d:%d)->(%d:%d)\n",
728 hw_id
, __func__
, d
.low_thresh
, d
.up_thresh
, cl_low
, cl_high
);
733 static int tsens_enable_irq(struct tsens_priv
*priv
)
736 int val
= tsens_version(priv
) > VER_1_X
? 7 : 1;
738 ret
= regmap_field_write(priv
->rf
[INT_EN
], val
);
740 dev_err(priv
->dev
, "%s: failed to enable interrupts\n",
746 static void tsens_disable_irq(struct tsens_priv
*priv
)
748 regmap_field_write(priv
->rf
[INT_EN
], 0);
751 int get_temp_tsens_valid(const struct tsens_sensor
*s
, int *temp
)
753 struct tsens_priv
*priv
= s
->priv
;
754 int hw_id
= s
->hw_id
;
755 u32 temp_idx
= LAST_TEMP_0
+ hw_id
;
756 u32 valid_idx
= VALID_0
+ hw_id
;
760 /* VER_0 doesn't have VALID bit */
761 if (tsens_version(priv
) == VER_0
)
764 /* Valid bit is 0 for 6 AHB clock cycles.
765 * At 19.2MHz, 1 AHB clock is ~60ns.
766 * We should enter this loop very, very rarely.
767 * Wait 1 us since it's the min of poll_timeout macro.
768 * Old value was 400 ns.
770 ret
= regmap_field_read_poll_timeout(priv
->rf
[valid_idx
], valid
,
771 valid
, 1, 20 * USEC_PER_MSEC
);
776 /* Valid bit is set, OK to read the temperature */
777 *temp
= tsens_hw_to_mC(s
, temp_idx
);
782 int get_temp_common(const struct tsens_sensor
*s
, int *temp
)
784 struct tsens_priv
*priv
= s
->priv
;
785 int hw_id
= s
->hw_id
;
786 int last_temp
= 0, ret
, trdy
;
787 unsigned long timeout
;
789 timeout
= jiffies
+ usecs_to_jiffies(TIMEOUT_US
);
791 if (tsens_version(priv
) == VER_0
) {
792 ret
= regmap_field_read(priv
->rf
[TRDY
], &trdy
);
799 ret
= regmap_field_read(priv
->rf
[LAST_TEMP_0
+ hw_id
], &last_temp
);
803 *temp
= code_to_degc(last_temp
, s
) * 1000;
806 } while (time_before(jiffies
, timeout
));
811 #ifdef CONFIG_DEBUG_FS
812 static int dbg_sensors_show(struct seq_file
*s
, void *data
)
814 struct platform_device
*pdev
= s
->private;
815 struct tsens_priv
*priv
= platform_get_drvdata(pdev
);
818 seq_printf(s
, "max: %2d\nnum: %2d\n\n",
819 priv
->feat
->max_sensors
, priv
->num_sensors
);
821 seq_puts(s
, " id slope offset\n--------------------------\n");
822 for (i
= 0; i
< priv
->num_sensors
; i
++) {
823 seq_printf(s
, "%8d %8d %8d\n", priv
->sensor
[i
].hw_id
,
824 priv
->sensor
[i
].slope
, priv
->sensor
[i
].offset
);
830 static int dbg_version_show(struct seq_file
*s
, void *data
)
832 struct platform_device
*pdev
= s
->private;
833 struct tsens_priv
*priv
= platform_get_drvdata(pdev
);
834 u32 maj_ver
, min_ver
, step_ver
;
837 if (tsens_version(priv
) > VER_0_1
) {
838 ret
= regmap_field_read(priv
->rf
[VER_MAJOR
], &maj_ver
);
841 ret
= regmap_field_read(priv
->rf
[VER_MINOR
], &min_ver
);
844 ret
= regmap_field_read(priv
->rf
[VER_STEP
], &step_ver
);
847 seq_printf(s
, "%d.%d.%d\n", maj_ver
, min_ver
, step_ver
);
849 seq_printf(s
, "0.%d.0\n", priv
->feat
->ver_major
);
855 DEFINE_SHOW_ATTRIBUTE(dbg_version
);
856 DEFINE_SHOW_ATTRIBUTE(dbg_sensors
);
858 static void tsens_debug_init(struct platform_device
*pdev
)
860 struct tsens_priv
*priv
= platform_get_drvdata(pdev
);
862 priv
->debug_root
= debugfs_lookup("tsens", NULL
);
863 if (!priv
->debug_root
)
864 priv
->debug_root
= debugfs_create_dir("tsens", NULL
);
866 /* A directory for each instance of the TSENS IP */
867 priv
->debug
= debugfs_create_dir(dev_name(&pdev
->dev
), priv
->debug_root
);
868 debugfs_create_file("version", 0444, priv
->debug
, pdev
, &dbg_version_fops
);
869 debugfs_create_file("sensors", 0444, priv
->debug
, pdev
, &dbg_sensors_fops
);
872 static inline void tsens_debug_init(struct platform_device
*pdev
) {}
875 static const struct regmap_config tsens_config
= {
882 static const struct regmap_config tsens_srot_config
= {
889 int __init
init_common(struct tsens_priv
*priv
)
891 void __iomem
*tm_base
, *srot_base
;
892 struct device
*dev
= priv
->dev
;
894 struct resource
*res
;
897 struct platform_device
*op
= of_find_device_by_node(priv
->dev
->of_node
);
902 if (op
->num_resources
> 1) {
903 /* DT with separate SROT and TM address space */
905 res
= platform_get_resource(op
, IORESOURCE_MEM
, 1);
906 srot_base
= devm_ioremap_resource(dev
, res
);
907 if (IS_ERR(srot_base
)) {
908 ret
= PTR_ERR(srot_base
);
912 priv
->srot_map
= devm_regmap_init_mmio(dev
, srot_base
,
914 if (IS_ERR(priv
->srot_map
)) {
915 ret
= PTR_ERR(priv
->srot_map
);
919 /* old DTs where SROT and TM were in a contiguous 2K block */
920 priv
->tm_offset
= 0x1000;
923 if (tsens_version(priv
) >= VER_0_1
) {
924 res
= platform_get_resource(op
, IORESOURCE_MEM
, 0);
925 tm_base
= devm_ioremap_resource(dev
, res
);
926 if (IS_ERR(tm_base
)) {
927 ret
= PTR_ERR(tm_base
);
931 priv
->tm_map
= devm_regmap_init_mmio(dev
, tm_base
, &tsens_config
);
932 } else { /* VER_0 share the same gcc regs using a syscon */
933 struct device
*parent
= priv
->dev
->parent
;
936 priv
->tm_map
= syscon_node_to_regmap(parent
->of_node
);
939 if (IS_ERR_OR_NULL(priv
->tm_map
)) {
943 ret
= PTR_ERR(priv
->tm_map
);
947 /* VER_0 have only tm_map */
949 priv
->srot_map
= priv
->tm_map
;
951 if (tsens_version(priv
) > VER_0_1
) {
952 for (i
= VER_MAJOR
; i
<= VER_STEP
; i
++) {
953 priv
->rf
[i
] = devm_regmap_field_alloc(dev
, priv
->srot_map
,
955 if (IS_ERR(priv
->rf
[i
])) {
956 ret
= PTR_ERR(priv
->rf
[i
]);
960 ret
= regmap_field_read(priv
->rf
[VER_MINOR
], &ver_minor
);
965 priv
->rf
[TSENS_EN
] = devm_regmap_field_alloc(dev
, priv
->srot_map
,
966 priv
->fields
[TSENS_EN
]);
967 if (IS_ERR(priv
->rf
[TSENS_EN
])) {
968 ret
= PTR_ERR(priv
->rf
[TSENS_EN
]);
971 /* in VER_0 TSENS need to be explicitly enabled */
972 if (tsens_version(priv
) == VER_0
)
973 regmap_field_write(priv
->rf
[TSENS_EN
], 1);
975 ret
= regmap_field_read(priv
->rf
[TSENS_EN
], &enabled
);
979 dev_err(dev
, "%s: device not enabled\n", __func__
);
984 priv
->rf
[SENSOR_EN
] = devm_regmap_field_alloc(dev
, priv
->srot_map
,
985 priv
->fields
[SENSOR_EN
]);
986 if (IS_ERR(priv
->rf
[SENSOR_EN
])) {
987 ret
= PTR_ERR(priv
->rf
[SENSOR_EN
]);
990 priv
->rf
[INT_EN
] = devm_regmap_field_alloc(dev
, priv
->tm_map
,
991 priv
->fields
[INT_EN
]);
992 if (IS_ERR(priv
->rf
[INT_EN
])) {
993 ret
= PTR_ERR(priv
->rf
[INT_EN
]);
997 priv
->rf
[TSENS_SW_RST
] =
998 devm_regmap_field_alloc(dev
, priv
->srot_map
, priv
->fields
[TSENS_SW_RST
]);
999 if (IS_ERR(priv
->rf
[TSENS_SW_RST
])) {
1000 ret
= PTR_ERR(priv
->rf
[TSENS_SW_RST
]);
1001 goto err_put_device
;
1004 priv
->rf
[TRDY
] = devm_regmap_field_alloc(dev
, priv
->tm_map
, priv
->fields
[TRDY
]);
1005 if (IS_ERR(priv
->rf
[TRDY
])) {
1006 ret
= PTR_ERR(priv
->rf
[TRDY
]);
1007 goto err_put_device
;
1010 /* This loop might need changes if enum regfield_ids is reordered */
1011 for (j
= LAST_TEMP_0
; j
<= UP_THRESH_15
; j
+= 16) {
1012 for (i
= 0; i
< priv
->feat
->max_sensors
; i
++) {
1015 priv
->rf
[idx
] = devm_regmap_field_alloc(dev
,
1018 if (IS_ERR(priv
->rf
[idx
])) {
1019 ret
= PTR_ERR(priv
->rf
[idx
]);
1020 goto err_put_device
;
1025 if (priv
->feat
->crit_int
|| tsens_version(priv
) < VER_0_1
) {
1026 /* Loop might need changes if enum regfield_ids is reordered */
1027 for (j
= CRITICAL_STATUS_0
; j
<= CRIT_THRESH_15
; j
+= 16) {
1028 for (i
= 0; i
< priv
->feat
->max_sensors
; i
++) {
1032 devm_regmap_field_alloc(dev
,
1035 if (IS_ERR(priv
->rf
[idx
])) {
1036 ret
= PTR_ERR(priv
->rf
[idx
]);
1037 goto err_put_device
;
1043 if (tsens_version(priv
) > VER_1_X
&& ver_minor
> 2) {
1044 /* Watchdog is present only on v2.3+ */
1045 priv
->feat
->has_watchdog
= 1;
1046 for (i
= WDOG_BARK_STATUS
; i
<= CC_MON_MASK
; i
++) {
1047 priv
->rf
[i
] = devm_regmap_field_alloc(dev
, priv
->tm_map
,
1049 if (IS_ERR(priv
->rf
[i
])) {
1050 ret
= PTR_ERR(priv
->rf
[i
]);
1051 goto err_put_device
;
1055 * Watchdog is already enabled, unmask the bark.
1056 * Disable cycle completion monitoring
1058 regmap_field_write(priv
->rf
[WDOG_BARK_MASK
], 0);
1059 regmap_field_write(priv
->rf
[CC_MON_MASK
], 1);
1062 spin_lock_init(&priv
->ul_lock
);
1064 /* VER_0 interrupt doesn't need to be enabled */
1065 if (tsens_version(priv
) >= VER_0_1
)
1066 tsens_enable_irq(priv
);
1069 put_device(&op
->dev
);
1073 static int tsens_get_temp(struct thermal_zone_device
*tz
, int *temp
)
1075 struct tsens_sensor
*s
= thermal_zone_device_priv(tz
);
1076 struct tsens_priv
*priv
= s
->priv
;
1078 return priv
->ops
->get_temp(s
, temp
);
1081 static int __maybe_unused
tsens_suspend(struct device
*dev
)
1083 struct tsens_priv
*priv
= dev_get_drvdata(dev
);
1085 if (priv
->ops
&& priv
->ops
->suspend
)
1086 return priv
->ops
->suspend(priv
);
1091 static int __maybe_unused
tsens_resume(struct device
*dev
)
1093 struct tsens_priv
*priv
= dev_get_drvdata(dev
);
1095 if (priv
->ops
&& priv
->ops
->resume
)
1096 return priv
->ops
->resume(priv
);
1101 static SIMPLE_DEV_PM_OPS(tsens_pm_ops
, tsens_suspend
, tsens_resume
);
1103 static const struct of_device_id tsens_table
[] = {
1105 .compatible
= "qcom,ipq8064-tsens",
1108 .compatible
= "qcom,ipq8074-tsens",
1109 .data
= &data_ipq8074
,
1111 .compatible
= "qcom,mdm9607-tsens",
1114 .compatible
= "qcom,msm8226-tsens",
1117 .compatible
= "qcom,msm8909-tsens",
1120 .compatible
= "qcom,msm8916-tsens",
1123 .compatible
= "qcom,msm8937-tsens",
1126 .compatible
= "qcom,msm8939-tsens",
1129 .compatible
= "qcom,msm8956-tsens",
1132 .compatible
= "qcom,msm8960-tsens",
1135 .compatible
= "qcom,msm8974-tsens",
1138 .compatible
= "qcom,msm8976-tsens",
1141 .compatible
= "qcom,msm8996-tsens",
1144 .compatible
= "qcom,tsens-v1",
1145 .data
= &data_tsens_v1
,
1147 .compatible
= "qcom,tsens-v2",
1148 .data
= &data_tsens_v2
,
1152 MODULE_DEVICE_TABLE(of
, tsens_table
);
1154 static const struct thermal_zone_device_ops tsens_of_ops
= {
1155 .get_temp
= tsens_get_temp
,
1156 .set_trips
= tsens_set_trips
,
1159 static int tsens_register_irq(struct tsens_priv
*priv
, char *irqname
,
1160 irq_handler_t thread_fn
)
1162 struct platform_device
*pdev
;
1165 pdev
= of_find_device_by_node(priv
->dev
->of_node
);
1169 irq
= platform_get_irq_byname(pdev
, irqname
);
1172 /* For old DTs with no IRQ defined */
1176 /* VER_0 interrupt is TRIGGER_RISING, VER_0_1 and up is ONESHOT */
1177 if (tsens_version(priv
) == VER_0
)
1178 ret
= devm_request_threaded_irq(&pdev
->dev
, irq
,
1180 IRQF_TRIGGER_RISING
,
1181 dev_name(&pdev
->dev
),
1184 ret
= devm_request_threaded_irq(&pdev
->dev
, irq
, NULL
,
1185 thread_fn
, IRQF_ONESHOT
,
1186 dev_name(&pdev
->dev
),
1190 dev_err(&pdev
->dev
, "%s: failed to get irq\n",
1193 enable_irq_wake(irq
);
1196 put_device(&pdev
->dev
);
1200 #ifdef CONFIG_SUSPEND
1201 static int tsens_reinit(struct tsens_priv
*priv
)
1203 if (tsens_version(priv
) >= VER_2_X
) {
1205 * Re-enable the watchdog, unmask the bark.
1206 * Disable cycle completion monitoring
1208 if (priv
->feat
->has_watchdog
) {
1209 regmap_field_write(priv
->rf
[WDOG_BARK_MASK
], 0);
1210 regmap_field_write(priv
->rf
[CC_MON_MASK
], 1);
1213 /* Re-enable interrupts */
1214 tsens_enable_irq(priv
);
1220 int tsens_resume_common(struct tsens_priv
*priv
)
1222 if (pm_suspend_target_state
== PM_SUSPEND_MEM
)
1228 #endif /* !CONFIG_SUSPEND */
1230 static int tsens_register(struct tsens_priv
*priv
)
1233 struct thermal_zone_device
*tzd
;
1235 for (i
= 0; i
< priv
->num_sensors
; i
++) {
1236 priv
->sensor
[i
].priv
= priv
;
1237 tzd
= devm_thermal_of_zone_register(priv
->dev
, priv
->sensor
[i
].hw_id
,
1242 priv
->sensor
[i
].tzd
= tzd
;
1243 if (priv
->ops
->enable
)
1244 priv
->ops
->enable(priv
, i
);
1246 devm_thermal_add_hwmon_sysfs(priv
->dev
, tzd
);
1249 /* VER_0 require to set MIN and MAX THRESH
1250 * These 2 regs are set using the:
1251 * - CRIT_THRESH_0 for MAX THRESH hardcoded to 120°C
1252 * - CRIT_THRESH_1 for MIN THRESH hardcoded to 0°C
1254 if (tsens_version(priv
) < VER_0_1
) {
1255 regmap_field_write(priv
->rf
[CRIT_THRESH_0
],
1256 tsens_mC_to_hw(priv
->sensor
, 120000));
1258 regmap_field_write(priv
->rf
[CRIT_THRESH_1
],
1259 tsens_mC_to_hw(priv
->sensor
, 0));
1262 if (priv
->feat
->combo_int
) {
1263 ret
= tsens_register_irq(priv
, "combined",
1264 tsens_combined_irq_thread
);
1266 ret
= tsens_register_irq(priv
, "uplow", tsens_irq_thread
);
1270 if (priv
->feat
->crit_int
)
1271 ret
= tsens_register_irq(priv
, "critical",
1272 tsens_critical_irq_thread
);
1278 static int tsens_probe(struct platform_device
*pdev
)
1282 struct device_node
*np
;
1283 struct tsens_priv
*priv
;
1284 const struct tsens_plat_data
*data
;
1285 const struct of_device_id
*id
;
1288 if (pdev
->dev
.of_node
)
1291 dev
= pdev
->dev
.parent
;
1295 id
= of_match_node(tsens_table
, np
);
1301 num_sensors
= data
->num_sensors
;
1304 of_property_read_u32(np
, "#qcom,sensors", &num_sensors
);
1306 if (num_sensors
<= 0) {
1307 dev_err(dev
, "%s: invalid number of sensors\n", __func__
);
1311 priv
= devm_kzalloc(dev
,
1312 struct_size(priv
, sensor
, num_sensors
),
1318 priv
->num_sensors
= num_sensors
;
1319 priv
->ops
= data
->ops
;
1320 for (i
= 0; i
< priv
->num_sensors
; i
++) {
1322 priv
->sensor
[i
].hw_id
= data
->hw_ids
[i
];
1324 priv
->sensor
[i
].hw_id
= i
;
1326 priv
->feat
= data
->feat
;
1327 priv
->fields
= data
->fields
;
1329 platform_set_drvdata(pdev
, priv
);
1331 if (!priv
->ops
|| !priv
->ops
->init
|| !priv
->ops
->get_temp
)
1334 ret
= priv
->ops
->init(priv
);
1336 dev_err(dev
, "%s: init failed\n", __func__
);
1340 if (priv
->ops
->calibrate
) {
1341 ret
= priv
->ops
->calibrate(priv
);
1343 return dev_err_probe(dev
, ret
, "%s: calibration failed\n",
1347 ret
= tsens_register(priv
);
1349 tsens_debug_init(pdev
);
1354 static void tsens_remove(struct platform_device
*pdev
)
1356 struct tsens_priv
*priv
= platform_get_drvdata(pdev
);
1358 debugfs_remove_recursive(priv
->debug_root
);
1359 tsens_disable_irq(priv
);
1360 if (priv
->ops
->disable
)
1361 priv
->ops
->disable(priv
);
1364 static struct platform_driver tsens_driver
= {
1365 .probe
= tsens_probe
,
1366 .remove
= tsens_remove
,
1368 .name
= "qcom-tsens",
1369 .pm
= &tsens_pm_ops
,
1370 .of_match_table
= tsens_table
,
1373 module_platform_driver(tsens_driver
);
1375 MODULE_LICENSE("GPL v2");
1376 MODULE_DESCRIPTION("QCOM Temperature Sensor driver");
1377 MODULE_ALIAS("platform:qcom-tsens");