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 ONE_PT_CALIB 0x1
10 #define ONE_PT_CALIB2 0x2
11 #define TWO_PT_CALIB 0x3
12 #define CAL_DEGC_PT1 30
13 #define CAL_DEGC_PT2 120
14 #define SLOPE_FACTOR 1000
15 #define SLOPE_DEFAULT 3200
16 #define THRESHOLD_MAX_ADC_CODE 0x3ff
17 #define THRESHOLD_MIN_ADC_CODE 0x0
19 #include <linux/interrupt.h>
20 #include <linux/thermal.h>
21 #include <linux/regmap.h>
22 #include <linux/slab.h>
26 /* IP version numbers in ascending order */
40 * struct tsens_sensor - data for each sensor connected to the tsens device
41 * @priv: tsens device instance that this sensor is connected to
42 * @tzd: pointer to the thermal zone that this sensor is in
43 * @offset: offset of temperature adjustment curve
44 * @hw_id: HW ID can be used in case of platform-specific IDs
45 * @slope: slope of temperature adjustment curve
46 * @status: 8960-specific variable to track 8960 and 8660 status register offset
49 struct tsens_priv
*priv
;
50 struct thermal_zone_device
*tzd
;
58 * struct tsens_ops - operations as supported by the tsens device
59 * @init: Function to initialize the tsens device
60 * @calibrate: Function to calibrate the tsens device
61 * @get_temp: Function which returns the temp in millidegC
62 * @enable: Function to enable (clocks/power) tsens device
63 * @disable: Function to disable the tsens device
64 * @suspend: Function to suspend the tsens device
65 * @resume: Function to resume the tsens device
66 * @get_trend: Function to get the thermal/temp trend
69 /* mandatory callbacks */
70 int (*init
)(struct tsens_priv
*priv
);
71 int (*calibrate
)(struct tsens_priv
*priv
);
72 int (*get_temp
)(const struct tsens_sensor
*s
, int *temp
);
73 /* optional callbacks */
74 int (*enable
)(struct tsens_priv
*priv
, int i
);
75 void (*disable
)(struct tsens_priv
*priv
);
76 int (*suspend
)(struct tsens_priv
*priv
);
77 int (*resume
)(struct tsens_priv
*priv
);
78 int (*get_trend
)(struct tsens_sensor
*s
, enum thermal_trend
*trend
);
81 #define REG_FIELD_FOR_EACH_SENSOR11(_name, _offset, _startbit, _stopbit) \
82 [_name##_##0] = REG_FIELD(_offset, _startbit, _stopbit), \
83 [_name##_##1] = REG_FIELD(_offset + 4, _startbit, _stopbit), \
84 [_name##_##2] = REG_FIELD(_offset + 8, _startbit, _stopbit), \
85 [_name##_##3] = REG_FIELD(_offset + 12, _startbit, _stopbit), \
86 [_name##_##4] = REG_FIELD(_offset + 16, _startbit, _stopbit), \
87 [_name##_##5] = REG_FIELD(_offset + 20, _startbit, _stopbit), \
88 [_name##_##6] = REG_FIELD(_offset + 24, _startbit, _stopbit), \
89 [_name##_##7] = REG_FIELD(_offset + 28, _startbit, _stopbit), \
90 [_name##_##8] = REG_FIELD(_offset + 32, _startbit, _stopbit), \
91 [_name##_##9] = REG_FIELD(_offset + 36, _startbit, _stopbit), \
92 [_name##_##10] = REG_FIELD(_offset + 40, _startbit, _stopbit)
94 #define REG_FIELD_FOR_EACH_SENSOR16(_name, _offset, _startbit, _stopbit) \
95 [_name##_##0] = REG_FIELD(_offset, _startbit, _stopbit), \
96 [_name##_##1] = REG_FIELD(_offset + 4, _startbit, _stopbit), \
97 [_name##_##2] = REG_FIELD(_offset + 8, _startbit, _stopbit), \
98 [_name##_##3] = REG_FIELD(_offset + 12, _startbit, _stopbit), \
99 [_name##_##4] = REG_FIELD(_offset + 16, _startbit, _stopbit), \
100 [_name##_##5] = REG_FIELD(_offset + 20, _startbit, _stopbit), \
101 [_name##_##6] = REG_FIELD(_offset + 24, _startbit, _stopbit), \
102 [_name##_##7] = REG_FIELD(_offset + 28, _startbit, _stopbit), \
103 [_name##_##8] = REG_FIELD(_offset + 32, _startbit, _stopbit), \
104 [_name##_##9] = REG_FIELD(_offset + 36, _startbit, _stopbit), \
105 [_name##_##10] = REG_FIELD(_offset + 40, _startbit, _stopbit), \
106 [_name##_##11] = REG_FIELD(_offset + 44, _startbit, _stopbit), \
107 [_name##_##12] = REG_FIELD(_offset + 48, _startbit, _stopbit), \
108 [_name##_##13] = REG_FIELD(_offset + 52, _startbit, _stopbit), \
109 [_name##_##14] = REG_FIELD(_offset + 56, _startbit, _stopbit), \
110 [_name##_##15] = REG_FIELD(_offset + 60, _startbit, _stopbit)
112 #define REG_FIELD_SPLIT_BITS_0_15(_name, _offset) \
113 [_name##_##0] = REG_FIELD(_offset, 0, 0), \
114 [_name##_##1] = REG_FIELD(_offset, 1, 1), \
115 [_name##_##2] = REG_FIELD(_offset, 2, 2), \
116 [_name##_##3] = REG_FIELD(_offset, 3, 3), \
117 [_name##_##4] = REG_FIELD(_offset, 4, 4), \
118 [_name##_##5] = REG_FIELD(_offset, 5, 5), \
119 [_name##_##6] = REG_FIELD(_offset, 6, 6), \
120 [_name##_##7] = REG_FIELD(_offset, 7, 7), \
121 [_name##_##8] = REG_FIELD(_offset, 8, 8), \
122 [_name##_##9] = REG_FIELD(_offset, 9, 9), \
123 [_name##_##10] = REG_FIELD(_offset, 10, 10), \
124 [_name##_##11] = REG_FIELD(_offset, 11, 11), \
125 [_name##_##12] = REG_FIELD(_offset, 12, 12), \
126 [_name##_##13] = REG_FIELD(_offset, 13, 13), \
127 [_name##_##14] = REG_FIELD(_offset, 14, 14), \
128 [_name##_##15] = REG_FIELD(_offset, 15, 15)
130 #define REG_FIELD_SPLIT_BITS_16_31(_name, _offset) \
131 [_name##_##0] = REG_FIELD(_offset, 16, 16), \
132 [_name##_##1] = REG_FIELD(_offset, 17, 17), \
133 [_name##_##2] = REG_FIELD(_offset, 18, 18), \
134 [_name##_##3] = REG_FIELD(_offset, 19, 19), \
135 [_name##_##4] = REG_FIELD(_offset, 20, 20), \
136 [_name##_##5] = REG_FIELD(_offset, 21, 21), \
137 [_name##_##6] = REG_FIELD(_offset, 22, 22), \
138 [_name##_##7] = REG_FIELD(_offset, 23, 23), \
139 [_name##_##8] = REG_FIELD(_offset, 24, 24), \
140 [_name##_##9] = REG_FIELD(_offset, 25, 25), \
141 [_name##_##10] = REG_FIELD(_offset, 26, 26), \
142 [_name##_##11] = REG_FIELD(_offset, 27, 27), \
143 [_name##_##12] = REG_FIELD(_offset, 28, 28), \
144 [_name##_##13] = REG_FIELD(_offset, 29, 29), \
145 [_name##_##14] = REG_FIELD(_offset, 30, 30), \
146 [_name##_##15] = REG_FIELD(_offset, 31, 31)
149 * reg_field IDs to use as an index into an array
150 * If you change the order of the entries, check the devm_regmap_field_alloc()
151 * calls in init_common()
154 /* ----- SROT ------ */
165 /* ----- TM ------ */
168 /* INTERRUPT ENABLE */
169 INT_EN
, /* v2+ has separate enables for crit, upper and lower irq */
171 LAST_TEMP_0
, /* Last temperature reading */
187 VALID_0
, /* VALID reading or not */
203 LOWER_STATUS_0
, /* LOWER threshold violated */
219 LOW_INT_STATUS_0
, /* LOWER interrupt status */
235 LOW_INT_CLEAR_0
, /* LOWER interrupt clear */
251 LOW_INT_MASK_0
, /* LOWER interrupt mask */
267 LOW_THRESH_0
, /* LOWER threshold values */
283 UPPER_STATUS_0
, /* UPPER threshold violated */
299 UP_INT_STATUS_0
, /* UPPER interrupt status */
315 UP_INT_CLEAR_0
, /* UPPER interrupt clear */
331 UP_INT_MASK_0
, /* UPPER interrupt mask */
347 UP_THRESH_0
, /* UPPER threshold values */
363 CRITICAL_STATUS_0
, /* CRITICAL threshold violated */
379 CRIT_INT_STATUS_0
, /* CRITICAL interrupt status */
395 CRIT_INT_CLEAR_0
, /* CRITICAL interrupt clear */
411 CRIT_INT_MASK_0
, /* CRITICAL interrupt mask */
427 CRIT_THRESH_0
, /* CRITICAL threshold values */
450 /* CYCLE COMPLETION MONITOR */
455 MIN_STATUS_0
, /* MIN threshold violated */
471 MAX_STATUS_0
, /* MAX threshold violated */
493 * struct tsens_features - Features supported by the IP
494 * @ver_major: Major number of IP version
495 * @crit_int: does the IP support critical interrupts?
496 * @adc: do the sensors only output adc code (instead of temperature)?
497 * @srot_split: does the IP neatly splits the register space into SROT and TM,
498 * with SROT only being available to secure boot firmware?
499 * @has_watchdog: does this IP support watchdog functionality?
500 * @max_sensors: maximum sensors supported by this version of the IP
502 struct tsens_features
{
503 unsigned int ver_major
;
504 unsigned int crit_int
:1;
506 unsigned int srot_split
:1;
507 unsigned int has_watchdog
:1;
508 unsigned int max_sensors
;
512 * struct tsens_plat_data - tsens compile-time platform data
513 * @num_sensors: Number of sensors supported by platform
514 * @ops: operations the tsens instance supports
515 * @hw_ids: Subset of sensors ids supported by platform, if not the first n
516 * @feat: features of the IP
517 * @fields: bitfield locations
519 struct tsens_plat_data
{
520 const u32 num_sensors
;
521 const struct tsens_ops
*ops
;
522 unsigned int *hw_ids
;
523 struct tsens_features
*feat
;
524 const struct reg_field
*fields
;
528 * struct tsens_context - Registers to be saved/restored across a context loss
529 * @threshold: Threshold register value
530 * @control: Control register value
532 struct tsens_context
{
538 * struct tsens_priv - private data for each instance of the tsens IP
539 * @dev: pointer to struct device
540 * @num_sensors: number of sensors enabled on this device
541 * @tm_map: pointer to TM register address space
542 * @srot_map: pointer to SROT register address space
543 * @tm_offset: deal with old device trees that don't address TM and SROT
544 * address space separately
545 * @ul_lock: lock while processing upper/lower threshold interrupts
546 * @crit_lock: lock while processing critical threshold interrupts
547 * @rf: array of regmap_fields used to store value of the field
548 * @ctx: registers to be saved and restored during suspend/resume
549 * @feat: features of the IP
550 * @fields: bitfield locations
551 * @ops: pointer to list of callbacks supported by this device
552 * @debug_root: pointer to debugfs dentry for all tsens
553 * @debug: pointer to debugfs dentry for tsens controller
554 * @sensor: list of sensors attached to this device
559 struct regmap
*tm_map
;
560 struct regmap
*srot_map
;
563 /* lock for upper/lower threshold interrupts */
566 struct regmap_field
*rf
[MAX_REGFIELDS
];
567 struct tsens_context ctx
;
568 struct tsens_features
*feat
;
569 const struct reg_field
*fields
;
570 const struct tsens_ops
*ops
;
572 struct dentry
*debug_root
;
573 struct dentry
*debug
;
575 struct tsens_sensor sensor
[];
578 char *qfprom_read(struct device
*dev
, const char *cname
);
579 void compute_intercept_slope(struct tsens_priv
*priv
, u32
*pt1
, u32
*pt2
, u32 mode
);
580 int init_common(struct tsens_priv
*priv
);
581 int get_temp_tsens_valid(const struct tsens_sensor
*s
, int *temp
);
582 int get_temp_common(const struct tsens_sensor
*s
, int *temp
);
583 int tsens_enable_irq(struct tsens_priv
*priv
);
584 void tsens_disable_irq(struct tsens_priv
*priv
);
585 int tsens_set_trips(void *_sensor
, int low
, int high
);
586 irqreturn_t
tsens_irq_thread(int irq
, void *data
);
587 irqreturn_t
tsens_critical_irq_thread(int irq
, void *data
);
590 extern struct tsens_plat_data data_8960
;
592 /* TSENS v0.1 targets */
593 extern struct tsens_plat_data data_8916
, data_8974
;
595 /* TSENS v1 targets */
596 extern struct tsens_plat_data data_tsens_v1
, data_8976
;
598 /* TSENS v2 targets */
599 extern struct tsens_plat_data data_8996
, data_tsens_v2
;
601 #endif /* __QCOM_TSENS_H__ */