1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * OMAP4 Bandgap temperature sensor driver
4  *
5  * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
6  * Contact:
7  *   Eduardo Valentin <eduardo.valentin@ti.com>
8  */
9 #ifndef __TI_BANDGAP_H
10 #define __TI_BANDGAP_H
11 
12 #include <linux/spinlock.h>
13 #include <linux/types.h>
14 #include <linux/err.h>
15 
16 struct gpio_desc;
17 
18 /**
19  * DOC: bandgap driver data structure
20  * ==================================
21  *
22  *   +----------+----------------+
23  *   | struct temp_sensor_regval |
24  *   +---------------------------+
25  *              * (Array of)
26  *              |
27  *              |
28  *   +-------------------+   +-----------------+
29  *   | struct ti_bandgap |-->| struct device * |
30  *   +----------+--------+   +-----------------+
31  *              |
32  *              |
33  *              V
34  *   +------------------------+
35  *   | struct ti_bandgap_data |
36  *   +------------------------+
37  *              |
38  *              |
39  *              * (Array of)
40  * +------------+------------------------------------------------------+
41  * | +----------+------------+   +-------------------------+           |
42  * | | struct ti_temp_sensor |-->| struct temp_sensor_data |           |
43  * | +-----------------------+   +------------+------------+           |
44  * |            |                                                      |
45  * |            +                                                      |
46  * |            V                                                      |
47  * | +----------+-------------------+                                  |
48  * | | struct temp_sensor_registers |                                  |
49  * | +------------------------------+                                  |
50  * |                                                                   |
51  * +-------------------------------------------------------------------+
52  *
53  * Above is a simple diagram describing how the data structure below
54  * are organized. For each bandgap device there should be a ti_bandgap_data
55  * containing the device instance configuration, as well as, an array of
56  * sensors, representing every sensor instance present in this bandgap.
57  */
58 
59 /**
60  * struct temp_sensor_registers - descriptor to access registers and bitfields
61  * @temp_sensor_ctrl: TEMP_SENSOR_CTRL register offset
62  * @bgap_tempsoff_mask: mask to temp_sensor_ctrl.tempsoff
63  * @bgap_soc_mask: mask to temp_sensor_ctrl.soc
64  * @bgap_eocz_mask: mask to temp_sensor_ctrl.eocz
65  * @bgap_dtemp_mask: mask to temp_sensor_ctrl.dtemp
66  * @bgap_mask_ctrl: BANDGAP_MASK_CTRL register offset
67  * @mask_hot_mask: mask to bandgap_mask_ctrl.mask_hot
68  * @mask_cold_mask: mask to bandgap_mask_ctrl.mask_cold
69  * @mask_counter_delay_mask: mask to bandgap_mask_ctrl.mask_counter_delay
70  * @mask_freeze_mask: mask to bandgap_mask_ctrl.mask_free
71  * @bgap_mode_ctrl: BANDGAP_MODE_CTRL register offset
72  * @mode_ctrl_mask: mask to bandgap_mode_ctrl.mode_ctrl
73  * @bgap_counter: BANDGAP_COUNTER register offset
74  * @counter_mask: mask to bandgap_counter.counter
75  * @bgap_threshold: BANDGAP_THRESHOLD register offset (TALERT thresholds)
76  * @threshold_thot_mask: mask to bandgap_threhold.thot
77  * @threshold_tcold_mask: mask to bandgap_threhold.tcold
78  * @tshut_threshold: TSHUT_THRESHOLD register offset (TSHUT thresholds)
79  * @tshut_hot_mask: mask to tshut_threhold.thot
80  * @tshut_cold_mask: mask to tshut_threhold.thot
81  * @bgap_status: BANDGAP_STATUS register offset
82  * @status_hot_mask: mask to bandgap_status.hot
83  * @status_cold_mask: mask to bandgap_status.cold
84  * @ctrl_dtemp_1: CTRL_DTEMP1 register offset
85  * @ctrl_dtemp_2: CTRL_DTEMP2 register offset
86  * @bgap_efuse: BANDGAP_EFUSE register offset
87  *
88  * The register offsets and bitfields might change across
89  * OMAP and variants versions. Hence this struct serves as a
90  * descriptor map on how to access the registers and the bitfields.
91  *
92  * This descriptor contains registers of all versions of bandgap chips.
93  * Not all versions will use all registers, depending on the available
94  * features. Please read TRMs for descriptive explanation on each bitfield.
95  */
96 
97 struct temp_sensor_registers {
98 	u32	temp_sensor_ctrl;
99 	u32	bgap_tempsoff_mask;
100 	u32	bgap_soc_mask;
101 	u32	bgap_eocz_mask;
102 	u32	bgap_dtemp_mask;
103 
104 	u32	bgap_mask_ctrl;
105 	u32	mask_hot_mask;
106 	u32	mask_cold_mask;
107 	u32	mask_counter_delay_mask;
108 	u32	mask_freeze_mask;
109 
110 	u32	bgap_mode_ctrl;
111 	u32	mode_ctrl_mask;
112 
113 	u32	bgap_counter;
114 	u32	counter_mask;
115 
116 	u32	bgap_threshold;
117 	u32	threshold_thot_mask;
118 	u32	threshold_tcold_mask;
119 
120 	u32	tshut_threshold;
121 	u32	tshut_hot_mask;
122 	u32	tshut_cold_mask;
123 
124 	u32	bgap_status;
125 	u32	status_hot_mask;
126 	u32	status_cold_mask;
127 
128 	u32	ctrl_dtemp_1;
129 	u32	ctrl_dtemp_2;
130 	u32	bgap_efuse;
131 };
132 
133 /**
134  * struct temp_sensor_data - The thresholds and limits for temperature sensors.
135  * @tshut_hot: temperature to trigger a thermal reset (initial value)
136  * @tshut_cold: temp to get the plat out of reset due to thermal (init val)
137  * @t_hot: temperature to trigger a thermal alert (high initial value)
138  * @t_cold: temperature to trigger a thermal alert (low initial value)
139  * @min_freq: sensor minimum clock rate
140  * @max_freq: sensor maximum clock rate
141  *
142  * This data structure will hold the required thresholds and temperature limits
143  * for a specific temperature sensor, like shutdown temperature, alert
144  * temperature, clock / rate used, ADC conversion limits and update intervals
145  */
146 struct temp_sensor_data {
147 	u32	tshut_hot;
148 	u32	tshut_cold;
149 	u32	t_hot;
150 	u32	t_cold;
151 	u32	min_freq;
152 	u32	max_freq;
153 };
154 
155 struct ti_bandgap_data;
156 
157 /**
158  * struct temp_sensor_regval - temperature sensor register values and priv data
159  * @bg_mode_ctrl: temp sensor control register value
160  * @bg_ctrl: bandgap ctrl register value
161  * @bg_counter: bandgap counter value
162  * @bg_threshold: bandgap threshold register value
163  * @tshut_threshold: bandgap tshut register value
164  * @data: private data
165  *
166  * Data structure to save and restore bandgap register set context. Only
167  * required registers are shadowed, when needed.
168  */
169 struct temp_sensor_regval {
170 	u32			bg_mode_ctrl;
171 	u32			bg_ctrl;
172 	u32			bg_counter;
173 	u32			bg_threshold;
174 	u32			tshut_threshold;
175 	void			*data;
176 };
177 
178 /**
179  * struct ti_bandgap - bandgap device structure
180  * @dev: struct device pointer
181  * @base: io memory base address
182  * @conf: struct with bandgap configuration set (# sensors, conv_table, etc)
183  * @regval: temperature sensor register values
184  * @fclock: pointer to functional clock of temperature sensor
185  * @div_clk: pointer to divider clock of temperature sensor fclk
186  * @lock: spinlock for ti_bandgap structure
187  * @irq: MPU IRQ number for thermal alert
188  * @tshut_gpio: GPIO where Tshut signal is routed
189  * @clk_rate: Holds current clock rate
190  *
191  * The bandgap device structure representing the bandgap device instance.
192  * It holds most of the dynamic stuff. Configurations and sensor specific
193  * entries are inside the @conf structure.
194  */
195 struct ti_bandgap {
196 	struct device			*dev;
197 	void __iomem			*base;
198 	const struct ti_bandgap_data	*conf;
199 	struct temp_sensor_regval	*regval;
200 	struct clk			*fclock;
201 	struct clk			*div_clk;
202 	spinlock_t			lock; /* shields this struct */
203 	int				irq;
204 	struct gpio_desc		*tshut_gpiod;
205 	u32				clk_rate;
206 };
207 
208 /**
209  * struct ti_temp_sensor - bandgap temperature sensor configuration data
210  * @ts_data: pointer to struct with thresholds, limits of temperature sensor
211  * @registers: pointer to the list of register offsets and bitfields
212  * @domain: the name of the domain where the sensor is located
213  * @slope_pcb: sensor gradient slope info for hotspot extrapolation equation
214  *             with no external influence
215  * @constant_pcb: sensor gradient const info for hotspot extrapolation equation
216  *             with no external influence
217  * @register_cooling: function to describe how this sensor is going to be cooled
218  * @unregister_cooling: function to release cooling data
219  *
220  * Data structure to describe a temperature sensor handled by a bandgap device.
221  * It should provide configuration details on this sensor, such as how to
222  * access the registers affecting this sensor, shadow register buffer, how to
223  * assess the gradient from hotspot, how to cooldown the domain when sensor
224  * reports too hot temperature.
225  */
226 struct ti_temp_sensor {
227 	struct temp_sensor_data		*ts_data;
228 	struct temp_sensor_registers	*registers;
229 	char				*domain;
230 	/* for hotspot extrapolation */
231 	const int			slope_pcb;
232 	const int			constant_pcb;
233 	int (*register_cooling)(struct ti_bandgap *bgp, int id);
234 	int (*unregister_cooling)(struct ti_bandgap *bgp, int id);
235 };
236 
237 /**
238  * DOC: ti bandgap feature types
239  *
240  * TI_BANDGAP_FEATURE_TSHUT - used when the thermal shutdown signal output
241  *      of a bandgap device instance is routed to the processor. This means
242  *      the system must react and perform the shutdown by itself (handle an
243  *      IRQ, for instance).
244  *
245  * TI_BANDGAP_FEATURE_TSHUT_CONFIG - used when the bandgap device has control
246  *      over the thermal shutdown configuration. This means that the thermal
247  *      shutdown thresholds are programmable, for instance.
248  *
249  * TI_BANDGAP_FEATURE_TALERT - used when the bandgap device instance outputs
250  *      a signal representing violation of programmable alert thresholds.
251  *
252  * TI_BANDGAP_FEATURE_MODE_CONFIG - used when it is possible to choose which
253  *      mode, continuous or one shot, the bandgap device instance will operate.
254  *
255  * TI_BANDGAP_FEATURE_COUNTER - used when the bandgap device instance allows
256  *      programming the update interval of its internal state machine.
257  *
258  * TI_BANDGAP_FEATURE_POWER_SWITCH - used when the bandgap device allows
259  *      itself to be switched on/off.
260  *
261  * TI_BANDGAP_FEATURE_CLK_CTRL - used when the clocks feeding the bandgap
262  *      device are gateable or not.
263  *
264  * TI_BANDGAP_FEATURE_FREEZE_BIT - used when the bandgap device features
265  *      a history buffer that its update can be freezed/unfreezed.
266  *
267  * TI_BANDGAP_FEATURE_COUNTER_DELAY - used when the bandgap device features
268  *	a delay programming based on distinct values.
269  *
270  * TI_BANDGAP_FEATURE_HISTORY_BUFFER - used when the bandgap device features
271  *	a history buffer of temperatures.
272  *
273  * TI_BANDGAP_FEATURE_ERRATA_814 - used to workaorund when the bandgap device
274  *	has Errata 814
275  * TI_BANDGAP_FEATURE_UNRELIABLE - used when the sensor readings are too
276  *	inaccurate.
277  * TI_BANDGAP_HAS(b, f) - macro to check if a bandgap device is capable of a
278  *      specific feature (above) or not. Return non-zero, if yes.
279  */
280 #define TI_BANDGAP_FEATURE_TSHUT		BIT(0)
281 #define TI_BANDGAP_FEATURE_TSHUT_CONFIG		BIT(1)
282 #define TI_BANDGAP_FEATURE_TALERT		BIT(2)
283 #define TI_BANDGAP_FEATURE_MODE_CONFIG		BIT(3)
284 #define TI_BANDGAP_FEATURE_COUNTER		BIT(4)
285 #define TI_BANDGAP_FEATURE_POWER_SWITCH		BIT(5)
286 #define TI_BANDGAP_FEATURE_CLK_CTRL		BIT(6)
287 #define TI_BANDGAP_FEATURE_FREEZE_BIT		BIT(7)
288 #define TI_BANDGAP_FEATURE_COUNTER_DELAY	BIT(8)
289 #define TI_BANDGAP_FEATURE_HISTORY_BUFFER	BIT(9)
290 #define TI_BANDGAP_FEATURE_ERRATA_814		BIT(10)
291 #define TI_BANDGAP_FEATURE_UNRELIABLE		BIT(11)
292 #define TI_BANDGAP_HAS(b, f)			\
293 			((b)->conf->features & TI_BANDGAP_FEATURE_ ## f)
294 
295 /**
296  * struct ti_bandgap_data - ti bandgap data configuration structure
297  * @features: a bitwise flag set to describe the device features
298  * @conv_table: Pointer to ADC to temperature conversion table
299  * @adc_start_val: ADC conversion table starting value
300  * @adc_end_val: ADC conversion table ending value
301  * @fclock_name: clock name of the functional clock
302  * @div_ck_name: clock name of the clock divisor
303  * @sensor_count: count of temperature sensor within this bandgap device
304  * @report_temperature: callback to report thermal alert to thermal API
305  * @expose_sensor: callback to export sensor to thermal API
306  * @remove_sensor: callback to destroy sensor from thermal API
307  * @sensors: array of sensors present in this bandgap instance
308  *
309  * This is a data structure which should hold most of the static configuration
310  * of a bandgap device instance. It should describe which features this instance
311  * is capable of, the clock names to feed this device, the amount of sensors and
312  * their configuration representation, and how to export and unexport them to
313  * a thermal API.
314  */
315 struct ti_bandgap_data {
316 	unsigned int			features;
317 	const int			*conv_table;
318 	u32				adc_start_val;
319 	u32				adc_end_val;
320 	char				*fclock_name;
321 	char				*div_ck_name;
322 	int				sensor_count;
323 	int (*report_temperature)(struct ti_bandgap *bgp, int id);
324 	int (*expose_sensor)(struct ti_bandgap *bgp, int id, char *domain);
325 	int (*remove_sensor)(struct ti_bandgap *bgp, int id);
326 
327 	/* this needs to be at the end */
328 	struct ti_temp_sensor		sensors[];
329 };
330 
331 int ti_bandgap_read_thot(struct ti_bandgap *bgp, int id, int *thot);
332 int ti_bandgap_write_thot(struct ti_bandgap *bgp, int id, int val);
333 int ti_bandgap_read_tcold(struct ti_bandgap *bgp, int id, int *tcold);
334 int ti_bandgap_write_tcold(struct ti_bandgap *bgp, int id, int val);
335 int ti_bandgap_read_update_interval(struct ti_bandgap *bgp, int id,
336 				    int *interval);
337 int ti_bandgap_write_update_interval(struct ti_bandgap *bgp, int id,
338 				     u32 interval);
339 int ti_bandgap_read_temperature(struct ti_bandgap *bgp, int id,
340 				  int *temperature);
341 int ti_bandgap_set_sensor_data(struct ti_bandgap *bgp, int id, void *data);
342 void *ti_bandgap_get_sensor_data(struct ti_bandgap *bgp, int id);
343 int ti_bandgap_get_trend(struct ti_bandgap *bgp, int id, int *trend);
344 
345 #ifdef CONFIG_OMAP3_THERMAL
346 extern const struct ti_bandgap_data omap34xx_data;
347 extern const struct ti_bandgap_data omap36xx_data;
348 #else
349 #define omap34xx_data					NULL
350 #define omap36xx_data					NULL
351 #endif
352 
353 #ifdef CONFIG_OMAP4_THERMAL
354 extern const struct ti_bandgap_data omap4430_data;
355 extern const struct ti_bandgap_data omap4460_data;
356 extern const struct ti_bandgap_data omap4470_data;
357 #else
358 #define omap4430_data					NULL
359 #define omap4460_data					NULL
360 #define omap4470_data					NULL
361 #endif
362 
363 #ifdef CONFIG_OMAP5_THERMAL
364 extern const struct ti_bandgap_data omap5430_data;
365 #else
366 #define omap5430_data					NULL
367 #endif
368 
369 #ifdef CONFIG_DRA752_THERMAL
370 extern const struct ti_bandgap_data dra752_data;
371 #else
372 #define dra752_data					NULL
373 #endif
374 #endif
375