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