1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (c) 2015, The Linux Foundation. All rights reserved.
6 #ifndef __QCOM_TSENS_H__
7 #define __QCOM_TSENS_H__
9 #define NO_PT_CALIB 0x0
10 #define ONE_PT_CALIB 0x1
11 #define ONE_PT_CALIB2 0x2
12 #define TWO_PT_CALIB 0x3
13 #define ONE_PT_CALIB2_NO_OFFSET 0x6
14 #define TWO_PT_CALIB_NO_OFFSET 0x7
15 #define CAL_DEGC_PT1 30
16 #define CAL_DEGC_PT2 120
17 #define SLOPE_FACTOR 1000
18 #define SLOPE_DEFAULT 3200
19 #define TIMEOUT_US 100
20 #define THRESHOLD_MAX_ADC_CODE 0x3ff
21 #define THRESHOLD_MIN_ADC_CODE 0x0
23 #define MAX_SENSORS 16
25 #include <linux/interrupt.h>
26 #include <linux/thermal.h>
27 #include <linux/regmap.h>
28 #include <linux/slab.h>
32 /* IP version numbers in ascending order */
47 * struct tsens_sensor - data for each sensor connected to the tsens device
48 * @priv: tsens device instance that this sensor is connected to
49 * @tzd: pointer to the thermal zone that this sensor is in
50 * @offset: offset of temperature adjustment curve
51 * @hw_id: HW ID can be used in case of platform-specific IDs
52 * @slope: slope of temperature adjustment curve
53 * @status: 8960-specific variable to track 8960 and 8660 status register offset
56 struct tsens_priv
*priv
;
57 struct thermal_zone_device
*tzd
;
67 * struct tsens_ops - operations as supported by the tsens device
68 * @init: Function to initialize the tsens device
69 * @calibrate: Function to calibrate the tsens device
70 * @get_temp: Function which returns the temp in millidegC
71 * @enable: Function to enable (clocks/power) tsens device
72 * @disable: Function to disable the tsens device
73 * @suspend: Function to suspend the tsens device
74 * @resume: Function to resume the tsens device
77 /* mandatory callbacks */
78 int (*init
)(struct tsens_priv
*priv
);
79 int (*calibrate
)(struct tsens_priv
*priv
);
80 int (*get_temp
)(const struct tsens_sensor
*s
, int *temp
);
81 /* optional callbacks */
82 int (*enable
)(struct tsens_priv
*priv
, int i
);
83 void (*disable
)(struct tsens_priv
*priv
);
84 int (*suspend
)(struct tsens_priv
*priv
);
85 int (*resume
)(struct tsens_priv
*priv
);
88 #define REG_FIELD_FOR_EACH_SENSOR11(_name, _offset, _startbit, _stopbit) \
89 [_name##_##0] = REG_FIELD(_offset, _startbit, _stopbit), \
90 [_name##_##1] = REG_FIELD(_offset + 4, _startbit, _stopbit), \
91 [_name##_##2] = REG_FIELD(_offset + 8, _startbit, _stopbit), \
92 [_name##_##3] = REG_FIELD(_offset + 12, _startbit, _stopbit), \
93 [_name##_##4] = REG_FIELD(_offset + 16, _startbit, _stopbit), \
94 [_name##_##5] = REG_FIELD(_offset + 20, _startbit, _stopbit), \
95 [_name##_##6] = REG_FIELD(_offset + 24, _startbit, _stopbit), \
96 [_name##_##7] = REG_FIELD(_offset + 28, _startbit, _stopbit), \
97 [_name##_##8] = REG_FIELD(_offset + 32, _startbit, _stopbit), \
98 [_name##_##9] = REG_FIELD(_offset + 36, _startbit, _stopbit), \
99 [_name##_##10] = REG_FIELD(_offset + 40, _startbit, _stopbit)
101 #define REG_FIELD_FOR_EACH_SENSOR16(_name, _offset, _startbit, _stopbit) \
102 [_name##_##0] = REG_FIELD(_offset, _startbit, _stopbit), \
103 [_name##_##1] = REG_FIELD(_offset + 4, _startbit, _stopbit), \
104 [_name##_##2] = REG_FIELD(_offset + 8, _startbit, _stopbit), \
105 [_name##_##3] = REG_FIELD(_offset + 12, _startbit, _stopbit), \
106 [_name##_##4] = REG_FIELD(_offset + 16, _startbit, _stopbit), \
107 [_name##_##5] = REG_FIELD(_offset + 20, _startbit, _stopbit), \
108 [_name##_##6] = REG_FIELD(_offset + 24, _startbit, _stopbit), \
109 [_name##_##7] = REG_FIELD(_offset + 28, _startbit, _stopbit), \
110 [_name##_##8] = REG_FIELD(_offset + 32, _startbit, _stopbit), \
111 [_name##_##9] = REG_FIELD(_offset + 36, _startbit, _stopbit), \
112 [_name##_##10] = REG_FIELD(_offset + 40, _startbit, _stopbit), \
113 [_name##_##11] = REG_FIELD(_offset + 44, _startbit, _stopbit), \
114 [_name##_##12] = REG_FIELD(_offset + 48, _startbit, _stopbit), \
115 [_name##_##13] = REG_FIELD(_offset + 52, _startbit, _stopbit), \
116 [_name##_##14] = REG_FIELD(_offset + 56, _startbit, _stopbit), \
117 [_name##_##15] = REG_FIELD(_offset + 60, _startbit, _stopbit)
119 #define REG_FIELD_SPLIT_BITS_0_15(_name, _offset) \
120 [_name##_##0] = REG_FIELD(_offset, 0, 0), \
121 [_name##_##1] = REG_FIELD(_offset, 1, 1), \
122 [_name##_##2] = REG_FIELD(_offset, 2, 2), \
123 [_name##_##3] = REG_FIELD(_offset, 3, 3), \
124 [_name##_##4] = REG_FIELD(_offset, 4, 4), \
125 [_name##_##5] = REG_FIELD(_offset, 5, 5), \
126 [_name##_##6] = REG_FIELD(_offset, 6, 6), \
127 [_name##_##7] = REG_FIELD(_offset, 7, 7), \
128 [_name##_##8] = REG_FIELD(_offset, 8, 8), \
129 [_name##_##9] = REG_FIELD(_offset, 9, 9), \
130 [_name##_##10] = REG_FIELD(_offset, 10, 10), \
131 [_name##_##11] = REG_FIELD(_offset, 11, 11), \
132 [_name##_##12] = REG_FIELD(_offset, 12, 12), \
133 [_name##_##13] = REG_FIELD(_offset, 13, 13), \
134 [_name##_##14] = REG_FIELD(_offset, 14, 14), \
135 [_name##_##15] = REG_FIELD(_offset, 15, 15)
137 #define REG_FIELD_SPLIT_BITS_16_31(_name, _offset) \
138 [_name##_##0] = REG_FIELD(_offset, 16, 16), \
139 [_name##_##1] = REG_FIELD(_offset, 17, 17), \
140 [_name##_##2] = REG_FIELD(_offset, 18, 18), \
141 [_name##_##3] = REG_FIELD(_offset, 19, 19), \
142 [_name##_##4] = REG_FIELD(_offset, 20, 20), \
143 [_name##_##5] = REG_FIELD(_offset, 21, 21), \
144 [_name##_##6] = REG_FIELD(_offset, 22, 22), \
145 [_name##_##7] = REG_FIELD(_offset, 23, 23), \
146 [_name##_##8] = REG_FIELD(_offset, 24, 24), \
147 [_name##_##9] = REG_FIELD(_offset, 25, 25), \
148 [_name##_##10] = REG_FIELD(_offset, 26, 26), \
149 [_name##_##11] = REG_FIELD(_offset, 27, 27), \
150 [_name##_##12] = REG_FIELD(_offset, 28, 28), \
151 [_name##_##13] = REG_FIELD(_offset, 29, 29), \
152 [_name##_##14] = REG_FIELD(_offset, 30, 30), \
153 [_name##_##15] = REG_FIELD(_offset, 31, 31)
156 * reg_field IDs to use as an index into an array
157 * If you change the order of the entries, check the devm_regmap_field_alloc()
158 * calls in init_common()
161 /* ----- SROT ------ */
172 /* ----- TM ------ */
175 /* INTERRUPT ENABLE */
176 INT_EN
, /* v2+ has separate enables for crit, upper and lower irq */
178 LAST_TEMP_0
, /* Last temperature reading */
194 VALID_0
, /* VALID reading or not */
210 LOWER_STATUS_0
, /* LOWER threshold violated */
226 LOW_INT_STATUS_0
, /* LOWER interrupt status */
242 LOW_INT_CLEAR_0
, /* LOWER interrupt clear */
258 LOW_INT_MASK_0
, /* LOWER interrupt mask */
274 LOW_THRESH_0
, /* LOWER threshold values */
290 UPPER_STATUS_0
, /* UPPER threshold violated */
306 UP_INT_STATUS_0
, /* UPPER interrupt status */
322 UP_INT_CLEAR_0
, /* UPPER interrupt clear */
338 UP_INT_MASK_0
, /* UPPER interrupt mask */
354 UP_THRESH_0
, /* UPPER threshold values */
370 CRITICAL_STATUS_0
, /* CRITICAL threshold violated */
386 CRIT_INT_STATUS_0
, /* CRITICAL interrupt status */
402 CRIT_INT_CLEAR_0
, /* CRITICAL interrupt clear */
418 CRIT_INT_MASK_0
, /* CRITICAL interrupt mask */
434 CRIT_THRESH_0
, /* CRITICAL threshold values */
457 /* CYCLE COMPLETION MONITOR */
462 MIN_STATUS_0
, /* MIN threshold violated */
478 MAX_STATUS_0
, /* MAX threshold violated */
500 * struct tsens_features - Features supported by the IP
501 * @ver_major: Major number of IP version
502 * @crit_int: does the IP support critical interrupts?
503 * @combo_int: does the IP use one IRQ for up, low and critical thresholds?
504 * @adc: do the sensors only output adc code (instead of temperature)?
505 * @srot_split: does the IP neatly splits the register space into SROT and TM,
506 * with SROT only being available to secure boot firmware?
507 * @has_watchdog: does this IP support watchdog functionality?
508 * @max_sensors: maximum sensors supported by this version of the IP
509 * @trip_min_temp: minimum trip temperature supported by this version of the IP
510 * @trip_max_temp: maximum trip temperature supported by this version of the IP
512 struct tsens_features
{
513 unsigned int ver_major
;
514 unsigned int crit_int
:1;
515 unsigned int combo_int
:1;
517 unsigned int srot_split
:1;
518 unsigned int has_watchdog
:1;
519 unsigned int max_sensors
;
525 * struct tsens_plat_data - tsens compile-time platform data
526 * @num_sensors: Number of sensors supported by platform
527 * @ops: operations the tsens instance supports
528 * @hw_ids: Subset of sensors ids supported by platform, if not the first n
529 * @feat: features of the IP
530 * @fields: bitfield locations
532 struct tsens_plat_data
{
533 const u32 num_sensors
;
534 const struct tsens_ops
*ops
;
535 unsigned int *hw_ids
;
536 struct tsens_features
*feat
;
537 const struct reg_field
*fields
;
541 * struct tsens_context - Registers to be saved/restored across a context loss
542 * @threshold: Threshold register value
543 * @control: Control register value
545 struct tsens_context
{
551 * struct tsens_priv - private data for each instance of the tsens IP
552 * @dev: pointer to struct device
553 * @num_sensors: number of sensors enabled on this device
554 * @tm_map: pointer to TM register address space
555 * @srot_map: pointer to SROT register address space
556 * @tm_offset: deal with old device trees that don't address TM and SROT
557 * address space separately
558 * @ul_lock: lock while processing upper/lower threshold interrupts
559 * @crit_lock: lock while processing critical threshold interrupts
560 * @rf: array of regmap_fields used to store value of the field
561 * @ctx: registers to be saved and restored during suspend/resume
562 * @feat: features of the IP
563 * @fields: bitfield locations
564 * @ops: pointer to list of callbacks supported by this device
565 * @debug_root: pointer to debugfs dentry for all tsens
566 * @debug: pointer to debugfs dentry for tsens controller
567 * @sensor: list of sensors attached to this device
572 struct regmap
*tm_map
;
573 struct regmap
*srot_map
;
576 /* lock for upper/lower threshold interrupts */
579 struct regmap_field
*rf
[MAX_REGFIELDS
];
580 struct tsens_context ctx
;
581 struct tsens_features
*feat
;
582 const struct reg_field
*fields
;
583 const struct tsens_ops
*ops
;
585 struct dentry
*debug_root
;
586 struct dentry
*debug
;
588 struct tsens_sensor sensor
[] __counted_by(num_sensors
);
592 * struct tsens_single_value - internal representation of a single field inside nvmem calibration data
593 * @idx: index into the u32 data array
594 * @shift: the shift of the first bit in the value
595 * @blob: index of the data blob to use for this cell
597 struct tsens_single_value
{
604 * struct tsens_legacy_calibration_format - description of calibration data used when parsing the legacy nvmem blob
605 * @base_len: the length of the base fields inside calibration data
606 * @base_shift: the shift to be applied to base data
607 * @sp_len: the length of the sN_pM fields inside calibration data
608 * @mode: descriptor of the calibration mode field
609 * @invalid: descriptor of the calibration mode invalid field
610 * @base: descriptors of the base0 and base1 fields
611 * @sp: descriptors of the sN_pM fields
613 struct tsens_legacy_calibration_format
{
614 unsigned int base_len
;
615 unsigned int base_shift
;
618 struct tsens_single_value mode
;
619 /* on all platforms except 8974 invalid is the third bit of what downstream calls 'mode' */
620 struct tsens_single_value invalid
;
621 struct tsens_single_value base
[2];
622 struct tsens_single_value sp
[][2];
625 char *qfprom_read(struct device
*dev
, const char *cname
);
626 int tsens_read_calibration_legacy(struct tsens_priv
*priv
,
627 const struct tsens_legacy_calibration_format
*format
,
629 u32
*cdata
, u32
*csel
);
630 int tsens_read_calibration(struct tsens_priv
*priv
, int shift
, u32
*p1
, u32
*p2
, bool backup
);
631 int tsens_calibrate_nvmem(struct tsens_priv
*priv
, int shift
);
632 int tsens_calibrate_common(struct tsens_priv
*priv
);
633 void compute_intercept_slope(struct tsens_priv
*priv
, u32
*pt1
, u32
*pt2
, u32 mode
);
634 int init_common(struct tsens_priv
*priv
);
635 int get_temp_tsens_valid(const struct tsens_sensor
*s
, int *temp
);
636 int get_temp_common(const struct tsens_sensor
*s
, int *temp
);
637 #ifdef CONFIG_SUSPEND
638 int tsens_resume_common(struct tsens_priv
*priv
);
640 #define tsens_resume_common NULL
644 extern struct tsens_plat_data data_8960
;
646 /* TSENS v0.1 targets */
647 extern struct tsens_plat_data data_8226
, data_8909
, data_8916
, data_8939
, data_8974
, data_9607
;
649 /* TSENS v1 targets */
650 extern struct tsens_plat_data data_tsens_v1
, data_8937
, data_8976
, data_8956
;
652 /* TSENS v2 targets */
653 extern struct tsens_plat_data data_8996
, data_ipq8074
, data_tsens_v2
;
655 #endif /* __QCOM_TSENS_H__ */