1 /* SPDX-License-Identifier: GPL-2.0-only */
2 #ifndef _WINDFARM_PID_H
3 #define _WINDFARM_PID_H
4 
5 /*
6  * Windfarm PowerMac thermal control. Generic PID helpers
7  *
8  * (c) Copyright 2005 Benjamin Herrenschmidt, IBM Corp.
9  *                    <benh@kernel.crashing.org>
10  *
11  * This is a pair of generic PID helpers that can be used by
12  * control loops. One is the basic PID implementation, the
13  * other one is more specifically tailored to the loops used
14  * for CPU control with 2 input sample types (temp and power)
15  */
16 
17 /*
18  * *** Simple PID ***
19  */
20 
21 #define WF_PID_MAX_HISTORY	32
22 
23 /* This parameter array is passed to the PID algorithm. Currently,
24  * we don't support changing parameters on the fly as it's not needed
25  * but could be implemented (with necessary adjustment of the history
26  * buffer
27  */
28 struct wf_pid_param {
29 	int	interval;	/* Interval between samples in seconds */
30 	int	history_len;	/* Size of history buffer */
31 	int	additive;	/* 1: target relative to previous value */
32 	s32	gd, gp, gr;	/* PID gains */
33 	s32	itarget;	/* PID input target */
34 	s32	min,max;	/* min and max target values */
35 };
36 
37 struct wf_pid_state {
38 	int	first;				/* first run of the loop */
39 	int	index; 				/* index of current sample */
40 	s32	target;				/* current target value */
41 	s32	samples[WF_PID_MAX_HISTORY];	/* samples history buffer */
42 	s32	errors[WF_PID_MAX_HISTORY];	/* error history buffer */
43 
44 	struct wf_pid_param param;
45 };
46 
47 extern void wf_pid_init(struct wf_pid_state *st, struct wf_pid_param *param);
48 extern s32 wf_pid_run(struct wf_pid_state *st, s32 sample);
49 
50 
51 /*
52  * *** CPU PID ***
53  */
54 
55 #define WF_CPU_PID_MAX_HISTORY	32
56 
57 /* This parameter array is passed to the CPU PID algorithm. Currently,
58  * we don't support changing parameters on the fly as it's not needed
59  * but could be implemented (with necessary adjustment of the history
60  * buffer
61  */
62 struct wf_cpu_pid_param {
63 	int	interval;	/* Interval between samples in seconds */
64 	int	history_len;	/* Size of history buffer */
65 	s32	gd, gp, gr;	/* PID gains */
66 	s32	pmaxadj;	/* PID max power adjust */
67 	s32	ttarget;	/* PID input target */
68 	s32	tmax;		/* PID input max */
69 	s32	min,max;	/* min and max target values */
70 };
71 
72 struct wf_cpu_pid_state {
73 	int	first;				/* first run of the loop */
74 	int	index; 				/* index of current power */
75 	int	tindex; 			/* index of current temp */
76 	s32	target;				/* current target value */
77 	s32	last_delta;			/* last Tactual - Ttarget */
78 	s32	powers[WF_PID_MAX_HISTORY];	/* power history buffer */
79 	s32	errors[WF_PID_MAX_HISTORY];	/* error history buffer */
80 	s32	temps[2];			/* temp. history buffer */
81 
82 	struct wf_cpu_pid_param param;
83 };
84 
85 extern void wf_cpu_pid_init(struct wf_cpu_pid_state *st,
86 			    struct wf_cpu_pid_param *param);
87 extern s32 wf_cpu_pid_run(struct wf_cpu_pid_state *st, s32 power, s32 temp);
88 
89 #endif /* _WINDFARM_PID_H */
90