1*2c162f9bSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
275722d39SBenjamin Herrenschmidt /*
375722d39SBenjamin Herrenschmidt * Windfarm PowerMac thermal control.
475722d39SBenjamin Herrenschmidt *
575722d39SBenjamin Herrenschmidt * (c) Copyright 2005 Benjamin Herrenschmidt, IBM Corp.
675722d39SBenjamin Herrenschmidt * <benh@kernel.crashing.org>
775722d39SBenjamin Herrenschmidt */
875722d39SBenjamin Herrenschmidt
975722d39SBenjamin Herrenschmidt #ifndef __WINDFARM_H__
1075722d39SBenjamin Herrenschmidt #define __WINDFARM_H__
1175722d39SBenjamin Herrenschmidt
1275722d39SBenjamin Herrenschmidt #include <linux/kref.h>
1375722d39SBenjamin Herrenschmidt #include <linux/list.h>
1475722d39SBenjamin Herrenschmidt #include <linux/module.h>
1575722d39SBenjamin Herrenschmidt #include <linux/notifier.h>
16ac171c46SBenjamin Herrenschmidt #include <linux/device.h>
1775722d39SBenjamin Herrenschmidt
1875722d39SBenjamin Herrenschmidt /* Display a 16.16 fixed point value */
196cd32099SBenjamin Herrenschmidt #define FIX32TOPRINT(f) (((s32)(f)) >> 16),(((((s32)(f)) & 0xffff) * 1000) >> 16)
2075722d39SBenjamin Herrenschmidt
2175722d39SBenjamin Herrenschmidt /*
2275722d39SBenjamin Herrenschmidt * Control objects
2375722d39SBenjamin Herrenschmidt */
2475722d39SBenjamin Herrenschmidt
2575722d39SBenjamin Herrenschmidt struct wf_control;
2675722d39SBenjamin Herrenschmidt
2775722d39SBenjamin Herrenschmidt struct wf_control_ops {
2875722d39SBenjamin Herrenschmidt int (*set_value)(struct wf_control *ct, s32 val);
2975722d39SBenjamin Herrenschmidt int (*get_value)(struct wf_control *ct, s32 *val);
3075722d39SBenjamin Herrenschmidt s32 (*get_min)(struct wf_control *ct);
3175722d39SBenjamin Herrenschmidt s32 (*get_max)(struct wf_control *ct);
3275722d39SBenjamin Herrenschmidt void (*release)(struct wf_control *ct);
3375722d39SBenjamin Herrenschmidt struct module *owner;
3475722d39SBenjamin Herrenschmidt };
3575722d39SBenjamin Herrenschmidt
3675722d39SBenjamin Herrenschmidt struct wf_control {
3775722d39SBenjamin Herrenschmidt struct list_head link;
38e074d08eSBenjamin Herrenschmidt const struct wf_control_ops *ops;
39e074d08eSBenjamin Herrenschmidt const char *name;
4075722d39SBenjamin Herrenschmidt int type;
4175722d39SBenjamin Herrenschmidt struct kref ref;
42ac171c46SBenjamin Herrenschmidt struct device_attribute attr;
436cd32099SBenjamin Herrenschmidt void *priv;
4475722d39SBenjamin Herrenschmidt };
4575722d39SBenjamin Herrenschmidt
4675722d39SBenjamin Herrenschmidt #define WF_CONTROL_TYPE_GENERIC 0
4775722d39SBenjamin Herrenschmidt #define WF_CONTROL_RPM_FAN 1
4875722d39SBenjamin Herrenschmidt #define WF_CONTROL_PWM_FAN 2
4975722d39SBenjamin Herrenschmidt
5075722d39SBenjamin Herrenschmidt
5175722d39SBenjamin Herrenschmidt /* Note about lifetime rules: wf_register_control() will initialize
5275722d39SBenjamin Herrenschmidt * the kref and wf_unregister_control will decrement it, thus the
5375722d39SBenjamin Herrenschmidt * object creating/disposing a given control shouldn't assume it
5475722d39SBenjamin Herrenschmidt * still exists after wf_unregister_control has been called.
5575722d39SBenjamin Herrenschmidt */
5675722d39SBenjamin Herrenschmidt extern int wf_register_control(struct wf_control *ct);
5775722d39SBenjamin Herrenschmidt extern void wf_unregister_control(struct wf_control *ct);
5875722d39SBenjamin Herrenschmidt extern int wf_get_control(struct wf_control *ct);
5975722d39SBenjamin Herrenschmidt extern void wf_put_control(struct wf_control *ct);
6075722d39SBenjamin Herrenschmidt
wf_control_set_max(struct wf_control * ct)6175722d39SBenjamin Herrenschmidt static inline int wf_control_set_max(struct wf_control *ct)
6275722d39SBenjamin Herrenschmidt {
6375722d39SBenjamin Herrenschmidt s32 vmax = ct->ops->get_max(ct);
6475722d39SBenjamin Herrenschmidt return ct->ops->set_value(ct, vmax);
6575722d39SBenjamin Herrenschmidt }
6675722d39SBenjamin Herrenschmidt
wf_control_set_min(struct wf_control * ct)6775722d39SBenjamin Herrenschmidt static inline int wf_control_set_min(struct wf_control *ct)
6875722d39SBenjamin Herrenschmidt {
6975722d39SBenjamin Herrenschmidt s32 vmin = ct->ops->get_min(ct);
7075722d39SBenjamin Herrenschmidt return ct->ops->set_value(ct, vmin);
7175722d39SBenjamin Herrenschmidt }
7275722d39SBenjamin Herrenschmidt
wf_control_set(struct wf_control * ct,s32 val)7333e6820bSBenjamin Herrenschmidt static inline int wf_control_set(struct wf_control *ct, s32 val)
7433e6820bSBenjamin Herrenschmidt {
7533e6820bSBenjamin Herrenschmidt return ct->ops->set_value(ct, val);
7633e6820bSBenjamin Herrenschmidt }
7733e6820bSBenjamin Herrenschmidt
wf_control_get(struct wf_control * ct,s32 * val)7833e6820bSBenjamin Herrenschmidt static inline int wf_control_get(struct wf_control *ct, s32 *val)
7933e6820bSBenjamin Herrenschmidt {
8033e6820bSBenjamin Herrenschmidt return ct->ops->get_value(ct, val);
8133e6820bSBenjamin Herrenschmidt }
8233e6820bSBenjamin Herrenschmidt
wf_control_get_min(struct wf_control * ct)8333e6820bSBenjamin Herrenschmidt static inline s32 wf_control_get_min(struct wf_control *ct)
8433e6820bSBenjamin Herrenschmidt {
8533e6820bSBenjamin Herrenschmidt return ct->ops->get_min(ct);
8633e6820bSBenjamin Herrenschmidt }
8733e6820bSBenjamin Herrenschmidt
wf_control_get_max(struct wf_control * ct)8833e6820bSBenjamin Herrenschmidt static inline s32 wf_control_get_max(struct wf_control *ct)
8933e6820bSBenjamin Herrenschmidt {
9033e6820bSBenjamin Herrenschmidt return ct->ops->get_max(ct);
9133e6820bSBenjamin Herrenschmidt }
9233e6820bSBenjamin Herrenschmidt
9375722d39SBenjamin Herrenschmidt /*
9475722d39SBenjamin Herrenschmidt * Sensor objects
9575722d39SBenjamin Herrenschmidt */
9675722d39SBenjamin Herrenschmidt
9775722d39SBenjamin Herrenschmidt struct wf_sensor;
9875722d39SBenjamin Herrenschmidt
9975722d39SBenjamin Herrenschmidt struct wf_sensor_ops {
10075722d39SBenjamin Herrenschmidt int (*get_value)(struct wf_sensor *sr, s32 *val);
10175722d39SBenjamin Herrenschmidt void (*release)(struct wf_sensor *sr);
10275722d39SBenjamin Herrenschmidt struct module *owner;
10375722d39SBenjamin Herrenschmidt };
10475722d39SBenjamin Herrenschmidt
10575722d39SBenjamin Herrenschmidt struct wf_sensor {
10675722d39SBenjamin Herrenschmidt struct list_head link;
107e074d08eSBenjamin Herrenschmidt const struct wf_sensor_ops *ops;
108e074d08eSBenjamin Herrenschmidt const char *name;
10975722d39SBenjamin Herrenschmidt struct kref ref;
110ac171c46SBenjamin Herrenschmidt struct device_attribute attr;
111e074d08eSBenjamin Herrenschmidt void *priv;
11275722d39SBenjamin Herrenschmidt };
11375722d39SBenjamin Herrenschmidt
11475722d39SBenjamin Herrenschmidt /* Same lifetime rules as controls */
11575722d39SBenjamin Herrenschmidt extern int wf_register_sensor(struct wf_sensor *sr);
11675722d39SBenjamin Herrenschmidt extern void wf_unregister_sensor(struct wf_sensor *sr);
11775722d39SBenjamin Herrenschmidt extern int wf_get_sensor(struct wf_sensor *sr);
11875722d39SBenjamin Herrenschmidt extern void wf_put_sensor(struct wf_sensor *sr);
11975722d39SBenjamin Herrenschmidt
wf_sensor_get(struct wf_sensor * sr,s32 * val)12033e6820bSBenjamin Herrenschmidt static inline int wf_sensor_get(struct wf_sensor *sr, s32 *val)
12133e6820bSBenjamin Herrenschmidt {
12233e6820bSBenjamin Herrenschmidt return sr->ops->get_value(sr, val);
12333e6820bSBenjamin Herrenschmidt }
12433e6820bSBenjamin Herrenschmidt
12575722d39SBenjamin Herrenschmidt /* For use by clients. Note that we are a bit racy here since
12675722d39SBenjamin Herrenschmidt * notifier_block doesn't have a module owner field. I may fix
12775722d39SBenjamin Herrenschmidt * it one day ...
12875722d39SBenjamin Herrenschmidt *
12975722d39SBenjamin Herrenschmidt * LOCKING NOTE !
13075722d39SBenjamin Herrenschmidt *
13175722d39SBenjamin Herrenschmidt * All "events" except WF_EVENT_TICK are called with an internal mutex
13275722d39SBenjamin Herrenschmidt * held which will deadlock if you call basically any core routine.
13375722d39SBenjamin Herrenschmidt * So don't ! Just take note of the event and do your actual operations
13475722d39SBenjamin Herrenschmidt * from the ticker.
13575722d39SBenjamin Herrenschmidt *
13675722d39SBenjamin Herrenschmidt */
13775722d39SBenjamin Herrenschmidt extern int wf_register_client(struct notifier_block *nb);
13875722d39SBenjamin Herrenschmidt extern int wf_unregister_client(struct notifier_block *nb);
13975722d39SBenjamin Herrenschmidt
14075722d39SBenjamin Herrenschmidt /* Overtemp conditions. Those are refcounted */
14175722d39SBenjamin Herrenschmidt extern void wf_set_overtemp(void);
14275722d39SBenjamin Herrenschmidt extern void wf_clear_overtemp(void);
14375722d39SBenjamin Herrenschmidt
14475722d39SBenjamin Herrenschmidt #define WF_EVENT_NEW_CONTROL 0 /* param is wf_control * */
14575722d39SBenjamin Herrenschmidt #define WF_EVENT_NEW_SENSOR 1 /* param is wf_sensor * */
14675722d39SBenjamin Herrenschmidt #define WF_EVENT_OVERTEMP 2 /* no param */
14775722d39SBenjamin Herrenschmidt #define WF_EVENT_NORMALTEMP 3 /* overtemp condition cleared */
14875722d39SBenjamin Herrenschmidt #define WF_EVENT_TICK 4 /* 1 second tick */
14975722d39SBenjamin Herrenschmidt
15075722d39SBenjamin Herrenschmidt /* Note: If that driver gets more broad use, we could replace the
15175722d39SBenjamin Herrenschmidt * simplistic overtemp bits with "environmental conditions". That
15275722d39SBenjamin Herrenschmidt * could then be used to also notify of things like fan failure,
15375722d39SBenjamin Herrenschmidt * case open, battery conditions, ...
15475722d39SBenjamin Herrenschmidt */
15575722d39SBenjamin Herrenschmidt
15675722d39SBenjamin Herrenschmidt #endif /* __WINDFARM_H__ */
157