xref: /linux/drivers/hwmon/bt1-pvt.h (revision 3503d56cc7233ced602e38a4c13caa64f00ab2aa)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) 2020 BAIKAL ELECTRONICS, JSC
4  *
5  * Baikal-T1 Process, Voltage, Temperature sensor driver
6  */
7 #ifndef __HWMON_BT1_PVT_H__
8 #define __HWMON_BT1_PVT_H__
9 
10 #include <linux/completion.h>
11 #include <linux/hwmon.h>
12 #include <linux/kernel.h>
13 #include <linux/mutex.h>
14 #include <linux/seqlock.h>
15 
16 /* Baikal-T1 PVT registers and their bitfields */
17 #define PVT_CTRL			0x00
18 #define PVT_CTRL_EN			BIT(0)
19 #define PVT_CTRL_MODE_FLD		1
20 #define PVT_CTRL_MODE_MASK		GENMASK(3, PVT_CTRL_MODE_FLD)
21 #define PVT_CTRL_MODE_TEMP		0x0
22 #define PVT_CTRL_MODE_VOLT		0x1
23 #define PVT_CTRL_MODE_LVT		0x2
24 #define PVT_CTRL_MODE_HVT		0x4
25 #define PVT_CTRL_MODE_SVT		0x6
26 #define PVT_CTRL_TRIM_FLD		4
27 #define PVT_CTRL_TRIM_MASK		GENMASK(8, PVT_CTRL_TRIM_FLD)
28 #define PVT_DATA			0x04
29 #define PVT_DATA_VALID			BIT(10)
30 #define PVT_DATA_DATA_FLD		0
31 #define PVT_DATA_DATA_MASK		GENMASK(9, PVT_DATA_DATA_FLD)
32 #define PVT_TTHRES			0x08
33 #define PVT_VTHRES			0x0C
34 #define PVT_LTHRES			0x10
35 #define PVT_HTHRES			0x14
36 #define PVT_STHRES			0x18
37 #define PVT_THRES_LO_FLD		0
38 #define PVT_THRES_LO_MASK		GENMASK(9, PVT_THRES_LO_FLD)
39 #define PVT_THRES_HI_FLD		10
40 #define PVT_THRES_HI_MASK		GENMASK(19, PVT_THRES_HI_FLD)
41 #define PVT_TTIMEOUT			0x1C
42 #define PVT_INTR_STAT			0x20
43 #define PVT_INTR_MASK			0x24
44 #define PVT_RAW_INTR_STAT		0x28
45 #define PVT_INTR_DVALID			BIT(0)
46 #define PVT_INTR_TTHRES_LO		BIT(1)
47 #define PVT_INTR_TTHRES_HI		BIT(2)
48 #define PVT_INTR_VTHRES_LO		BIT(3)
49 #define PVT_INTR_VTHRES_HI		BIT(4)
50 #define PVT_INTR_LTHRES_LO		BIT(5)
51 #define PVT_INTR_LTHRES_HI		BIT(6)
52 #define PVT_INTR_HTHRES_LO		BIT(7)
53 #define PVT_INTR_HTHRES_HI		BIT(8)
54 #define PVT_INTR_STHRES_LO		BIT(9)
55 #define PVT_INTR_STHRES_HI		BIT(10)
56 #define PVT_INTR_ALL			GENMASK(10, 0)
57 #define PVT_CLR_INTR			0x2C
58 
59 /*
60  * PVT sensors-related limits and default values
61  * @PVT_TEMP_MIN: Minimal temperature in millidegrees of Celsius.
62  * @PVT_TEMP_MAX: Maximal temperature in millidegrees of Celsius.
63  * @PVT_TEMP_CHS: Number of temperature hwmon channels.
64  * @PVT_VOLT_MIN: Minimal voltage in mV.
65  * @PVT_VOLT_MAX: Maximal voltage in mV.
66  * @PVT_VOLT_CHS: Number of voltage hwmon channels.
67  * @PVT_DATA_MIN: Minimal PVT raw data value.
68  * @PVT_DATA_MAX: Maximal PVT raw data value.
69  * @PVT_TRIM_MIN: Minimal temperature sensor trim value.
70  * @PVT_TRIM_MAX: Maximal temperature sensor trim value.
71  * @PVT_TRIM_DEF: Default temperature sensor trim value (set a proper value
72  *		  when one is determined for Baikal-T1 SoC).
73  * @PVT_TRIM_TEMP: Maximum temperature encoded by the trim factor.
74  * @PVT_TRIM_STEP: Temperature stride corresponding to the trim value.
75  * @PVT_TOUT_MIN: Minimal timeout between samples in nanoseconds.
76  * @PVT_TOUT_DEF: Default data measurements timeout. In case if alarms are
77  *		  activated the PVT IRQ is enabled to be raised after each
78  *		  conversion in order to have the thresholds checked and the
79  *		  converted value cached. Too frequent conversions may cause
80  *		  the system CPU overload. Lets set the 50ms delay between
81  *		  them by default to prevent this.
82  */
83 #define PVT_TEMP_MIN		-48380L
84 #define PVT_TEMP_MAX		147438L
85 #define PVT_TEMP_CHS		1
86 #define PVT_VOLT_MIN		620L
87 #define PVT_VOLT_MAX		1168L
88 #define PVT_VOLT_CHS		4
89 #define PVT_DATA_MIN		0
90 #define PVT_DATA_MAX		(PVT_DATA_DATA_MASK >> PVT_DATA_DATA_FLD)
91 #define PVT_TRIM_MIN		0
92 #define PVT_TRIM_MAX		(PVT_CTRL_TRIM_MASK >> PVT_CTRL_TRIM_FLD)
93 #define PVT_TRIM_TEMP		7130
94 #define PVT_TRIM_STEP		(PVT_TRIM_TEMP / PVT_TRIM_MAX)
95 #define PVT_TRIM_DEF		0
96 #define PVT_TOUT_MIN		(NSEC_PER_SEC / 3000)
97 #if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
98 # define PVT_TOUT_DEF		60000
99 #else
100 # define PVT_TOUT_DEF		0
101 #endif
102 
103 /*
104  * enum pvt_sensor_type - Baikal-T1 PVT sensor types (correspond to each PVT
105  *			  sampling mode)
106  * @PVT_SENSOR*: helpers to traverse the sensors in loops.
107  * @PVT_TEMP: PVT Temperature sensor.
108  * @PVT_VOLT: PVT Voltage sensor.
109  * @PVT_LVT: PVT Low-Voltage threshold sensor.
110  * @PVT_HVT: PVT High-Voltage threshold sensor.
111  * @PVT_SVT: PVT Standard-Voltage threshold sensor.
112  */
113 enum pvt_sensor_type {
114 	PVT_SENSOR_FIRST,
115 	PVT_TEMP = PVT_SENSOR_FIRST,
116 	PVT_VOLT,
117 	PVT_LVT,
118 	PVT_HVT,
119 	PVT_SVT,
120 	PVT_SENSOR_LAST = PVT_SVT,
121 	PVT_SENSORS_NUM
122 };
123 
124 /*
125  * enum pvt_clock_type - Baikal-T1 PVT clocks.
126  * @PVT_CLOCK_APB: APB clock.
127  * @PVT_CLOCK_REF: PVT reference clock.
128  */
129 enum pvt_clock_type {
130 	PVT_CLOCK_APB,
131 	PVT_CLOCK_REF,
132 	PVT_CLOCK_NUM
133 };
134 
135 /*
136  * struct pvt_sensor_info - Baikal-T1 PVT sensor informational structure
137  * @channel: Sensor channel ID.
138  * @label: hwmon sensor label.
139  * @mode: PVT mode corresponding to the channel.
140  * @thres_base: upper and lower threshold values of the sensor.
141  * @thres_sts_lo: low threshold status bitfield.
142  * @thres_sts_hi: high threshold status bitfield.
143  * @type: Sensor type.
144  * @attr_min_alarm: Min alarm attribute ID.
145  * @attr_min_alarm: Max alarm attribute ID.
146  */
147 struct pvt_sensor_info {
148 	int channel;
149 	const char *label;
150 	u32 mode;
151 	unsigned long thres_base;
152 	u32 thres_sts_lo;
153 	u32 thres_sts_hi;
154 	enum hwmon_sensor_types type;
155 	u32 attr_min_alarm;
156 	u32 attr_max_alarm;
157 };
158 
159 #define PVT_SENSOR_INFO(_ch, _label, _type, _mode, _thres)	\
160 	{							\
161 		.channel = _ch,					\
162 		.label = _label,				\
163 		.mode = PVT_CTRL_MODE_ ##_mode,			\
164 		.thres_base = PVT_ ##_thres,			\
165 		.thres_sts_lo = PVT_INTR_ ##_thres## _LO,	\
166 		.thres_sts_hi = PVT_INTR_ ##_thres## _HI,	\
167 		.type = _type,					\
168 		.attr_min_alarm = _type## _min,			\
169 		.attr_max_alarm = _type## _max,			\
170 	}
171 
172 /*
173  * struct pvt_cache - PVT sensors data cache
174  * @data: data cache in raw format.
175  * @thres_sts_lo: low threshold status saved on the previous data conversion.
176  * @thres_sts_hi: high threshold status saved on the previous data conversion.
177  * @data_seqlock: cached data seq-lock.
178  * @conversion: data conversion completion.
179  */
180 struct pvt_cache {
181 	u32 data;
182 #if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
183 	seqlock_t data_seqlock;
184 	u32 thres_sts_lo;
185 	u32 thres_sts_hi;
186 #else
187 	struct completion conversion;
188 #endif
189 };
190 
191 /*
192  * struct pvt_hwmon - Baikal-T1 PVT private data
193  * @dev: device structure of the PVT platform device.
194  * @hwmon: hwmon device structure.
195  * @regs: pointer to the Baikal-T1 PVT registers region.
196  * @irq: PVT events IRQ number.
197  * @clks: Array of the PVT clocks descriptor (APB/ref clocks).
198  * @ref_clk: Pointer to the reference clocks descriptor.
199  * @iface_mtx: Generic interface mutex (used to lock the alarm registers
200  *	       when the alarms enabled, or the data conversion interface
201  *	       if alarms are disabled).
202  * @sensor: current PVT sensor the data conversion is being performed for.
203  * @cache: data cache descriptor.
204  */
205 struct pvt_hwmon {
206 	struct device *dev;
207 	struct device *hwmon;
208 
209 	void __iomem *regs;
210 	int irq;
211 
212 	struct clk_bulk_data clks[PVT_CLOCK_NUM];
213 
214 	struct mutex iface_mtx;
215 	enum pvt_sensor_type sensor;
216 	struct pvt_cache cache[PVT_SENSORS_NUM];
217 };
218 
219 /*
220  * struct pvt_poly_term - a term descriptor of the PVT data translation
221  *			  polynomial
222  * @deg: degree of the term.
223  * @coef: multiplication factor of the term.
224  * @divider: distributed divider per each degree.
225  * @divider_leftover: divider leftover, which couldn't be redistributed.
226  */
227 struct pvt_poly_term {
228 	unsigned int deg;
229 	long coef;
230 	long divider;
231 	long divider_leftover;
232 };
233 
234 /*
235  * struct pvt_poly - PVT data translation polynomial descriptor
236  * @total_divider: total data divider.
237  * @terms: polynomial terms up to a free one.
238  */
239 struct pvt_poly {
240 	long total_divider;
241 	struct pvt_poly_term terms[];
242 };
243 
244 #endif /* __HWMON_BT1_PVT_H__ */
245