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