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