windfarm.h 4.1 KB

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