xref: /OK3568_Linux_fs/kernel/drivers/macintosh/windfarm.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-only */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Windfarm PowerMac thermal control.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * (c) Copyright 2005 Benjamin Herrenschmidt, IBM Corp.
6*4882a593Smuzhiyun  *                    <benh@kernel.crashing.org>
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #ifndef __WINDFARM_H__
10*4882a593Smuzhiyun #define __WINDFARM_H__
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #include <linux/kref.h>
13*4882a593Smuzhiyun #include <linux/list.h>
14*4882a593Smuzhiyun #include <linux/module.h>
15*4882a593Smuzhiyun #include <linux/notifier.h>
16*4882a593Smuzhiyun #include <linux/device.h>
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun /* Display a 16.16 fixed point value */
19*4882a593Smuzhiyun #define FIX32TOPRINT(f)	(((s32)(f)) >> 16),(((((s32)(f)) & 0xffff) * 1000) >> 16)
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun /*
22*4882a593Smuzhiyun  * Control objects
23*4882a593Smuzhiyun  */
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun struct wf_control;
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun struct wf_control_ops {
28*4882a593Smuzhiyun 	int			(*set_value)(struct wf_control *ct, s32 val);
29*4882a593Smuzhiyun 	int			(*get_value)(struct wf_control *ct, s32 *val);
30*4882a593Smuzhiyun 	s32			(*get_min)(struct wf_control *ct);
31*4882a593Smuzhiyun 	s32			(*get_max)(struct wf_control *ct);
32*4882a593Smuzhiyun 	void			(*release)(struct wf_control *ct);
33*4882a593Smuzhiyun 	struct module		*owner;
34*4882a593Smuzhiyun };
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun struct wf_control {
37*4882a593Smuzhiyun 	struct list_head		link;
38*4882a593Smuzhiyun 	const struct wf_control_ops	*ops;
39*4882a593Smuzhiyun 	const char			*name;
40*4882a593Smuzhiyun 	int				type;
41*4882a593Smuzhiyun 	struct kref			ref;
42*4882a593Smuzhiyun 	struct device_attribute		attr;
43*4882a593Smuzhiyun 	void				*priv;
44*4882a593Smuzhiyun };
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun #define WF_CONTROL_TYPE_GENERIC		0
47*4882a593Smuzhiyun #define WF_CONTROL_RPM_FAN		1
48*4882a593Smuzhiyun #define WF_CONTROL_PWM_FAN		2
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun /* Note about lifetime rules: wf_register_control() will initialize
52*4882a593Smuzhiyun  * the kref and wf_unregister_control will decrement it, thus the
53*4882a593Smuzhiyun  * object creating/disposing a given control shouldn't assume it
54*4882a593Smuzhiyun  * still exists after wf_unregister_control has been called.
55*4882a593Smuzhiyun  */
56*4882a593Smuzhiyun extern int wf_register_control(struct wf_control *ct);
57*4882a593Smuzhiyun extern void wf_unregister_control(struct wf_control *ct);
58*4882a593Smuzhiyun extern int wf_get_control(struct wf_control *ct);
59*4882a593Smuzhiyun extern void wf_put_control(struct wf_control *ct);
60*4882a593Smuzhiyun 
wf_control_set_max(struct wf_control * ct)61*4882a593Smuzhiyun static inline int wf_control_set_max(struct wf_control *ct)
62*4882a593Smuzhiyun {
63*4882a593Smuzhiyun 	s32 vmax = ct->ops->get_max(ct);
64*4882a593Smuzhiyun 	return ct->ops->set_value(ct, vmax);
65*4882a593Smuzhiyun }
66*4882a593Smuzhiyun 
wf_control_set_min(struct wf_control * ct)67*4882a593Smuzhiyun static inline int wf_control_set_min(struct wf_control *ct)
68*4882a593Smuzhiyun {
69*4882a593Smuzhiyun 	s32 vmin = ct->ops->get_min(ct);
70*4882a593Smuzhiyun 	return ct->ops->set_value(ct, vmin);
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun 
wf_control_set(struct wf_control * ct,s32 val)73*4882a593Smuzhiyun static inline int wf_control_set(struct wf_control *ct, s32 val)
74*4882a593Smuzhiyun {
75*4882a593Smuzhiyun 	return ct->ops->set_value(ct, val);
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun 
wf_control_get(struct wf_control * ct,s32 * val)78*4882a593Smuzhiyun static inline int wf_control_get(struct wf_control *ct, s32 *val)
79*4882a593Smuzhiyun {
80*4882a593Smuzhiyun 	return ct->ops->get_value(ct, val);
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun 
wf_control_get_min(struct wf_control * ct)83*4882a593Smuzhiyun static inline s32 wf_control_get_min(struct wf_control *ct)
84*4882a593Smuzhiyun {
85*4882a593Smuzhiyun 	return ct->ops->get_min(ct);
86*4882a593Smuzhiyun }
87*4882a593Smuzhiyun 
wf_control_get_max(struct wf_control * ct)88*4882a593Smuzhiyun static inline s32 wf_control_get_max(struct wf_control *ct)
89*4882a593Smuzhiyun {
90*4882a593Smuzhiyun 	return ct->ops->get_max(ct);
91*4882a593Smuzhiyun }
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun /*
94*4882a593Smuzhiyun  * Sensor objects
95*4882a593Smuzhiyun  */
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun struct wf_sensor;
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun struct wf_sensor_ops {
100*4882a593Smuzhiyun 	int			(*get_value)(struct wf_sensor *sr, s32 *val);
101*4882a593Smuzhiyun 	void			(*release)(struct wf_sensor *sr);
102*4882a593Smuzhiyun 	struct module		*owner;
103*4882a593Smuzhiyun };
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun struct wf_sensor {
106*4882a593Smuzhiyun 	struct list_head		link;
107*4882a593Smuzhiyun 	const struct wf_sensor_ops	*ops;
108*4882a593Smuzhiyun 	const char			*name;
109*4882a593Smuzhiyun 	struct kref			ref;
110*4882a593Smuzhiyun 	struct device_attribute		attr;
111*4882a593Smuzhiyun 	void				*priv;
112*4882a593Smuzhiyun };
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun /* Same lifetime rules as controls */
115*4882a593Smuzhiyun extern int wf_register_sensor(struct wf_sensor *sr);
116*4882a593Smuzhiyun extern void wf_unregister_sensor(struct wf_sensor *sr);
117*4882a593Smuzhiyun extern int wf_get_sensor(struct wf_sensor *sr);
118*4882a593Smuzhiyun extern void wf_put_sensor(struct wf_sensor *sr);
119*4882a593Smuzhiyun 
wf_sensor_get(struct wf_sensor * sr,s32 * val)120*4882a593Smuzhiyun static inline int wf_sensor_get(struct wf_sensor *sr, s32 *val)
121*4882a593Smuzhiyun {
122*4882a593Smuzhiyun 	return sr->ops->get_value(sr, val);
123*4882a593Smuzhiyun }
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun /* For use by clients. Note that we are a bit racy here since
126*4882a593Smuzhiyun  * notifier_block doesn't have a module owner field. I may fix
127*4882a593Smuzhiyun  * it one day ...
128*4882a593Smuzhiyun  *
129*4882a593Smuzhiyun  * LOCKING NOTE !
130*4882a593Smuzhiyun  *
131*4882a593Smuzhiyun  * All "events" except WF_EVENT_TICK are called with an internal mutex
132*4882a593Smuzhiyun  * held which will deadlock if you call basically any core routine.
133*4882a593Smuzhiyun  * So don't ! Just take note of the event and do your actual operations
134*4882a593Smuzhiyun  * from the ticker.
135*4882a593Smuzhiyun  *
136*4882a593Smuzhiyun  */
137*4882a593Smuzhiyun extern int wf_register_client(struct notifier_block *nb);
138*4882a593Smuzhiyun extern int wf_unregister_client(struct notifier_block *nb);
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun /* Overtemp conditions. Those are refcounted */
141*4882a593Smuzhiyun extern void wf_set_overtemp(void);
142*4882a593Smuzhiyun extern void wf_clear_overtemp(void);
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun #define WF_EVENT_NEW_CONTROL	0 /* param is wf_control * */
145*4882a593Smuzhiyun #define WF_EVENT_NEW_SENSOR	1 /* param is wf_sensor * */
146*4882a593Smuzhiyun #define WF_EVENT_OVERTEMP	2 /* no param */
147*4882a593Smuzhiyun #define WF_EVENT_NORMALTEMP	3 /* overtemp condition cleared */
148*4882a593Smuzhiyun #define WF_EVENT_TICK		4 /* 1 second tick */
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun /* Note: If that driver gets more broad use, we could replace the
151*4882a593Smuzhiyun  * simplistic overtemp bits with "environmental conditions". That
152*4882a593Smuzhiyun  * could then be used to also notify of things like fan failure,
153*4882a593Smuzhiyun  * case open, battery conditions, ...
154*4882a593Smuzhiyun  */
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun #endif /* __WINDFARM_H__ */
157