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