bt1-pvt.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2020 BAIKAL ELECTRONICS, JSC
  4. *
  5. * Baikal-T1 Process, Voltage, Temperature sensor driver
  6. */
  7. #ifndef __HWMON_BT1_PVT_H__
  8. #define __HWMON_BT1_PVT_H__
  9. #include <linux/completion.h>
  10. #include <linux/hwmon.h>
  11. #include <linux/kernel.h>
  12. #include <linux/ktime.h>
  13. #include <linux/mutex.h>
  14. #include <linux/seqlock.h>
  15. /* Baikal-T1 PVT registers and their bitfields */
  16. #define PVT_CTRL 0x00
  17. #define PVT_CTRL_EN BIT(0)
  18. #define PVT_CTRL_MODE_FLD 1
  19. #define PVT_CTRL_MODE_MASK GENMASK(3, PVT_CTRL_MODE_FLD)
  20. #define PVT_CTRL_MODE_TEMP 0x0
  21. #define PVT_CTRL_MODE_VOLT 0x1
  22. #define PVT_CTRL_MODE_LVT 0x2
  23. #define PVT_CTRL_MODE_HVT 0x4
  24. #define PVT_CTRL_MODE_SVT 0x6
  25. #define PVT_CTRL_TRIM_FLD 4
  26. #define PVT_CTRL_TRIM_MASK GENMASK(8, PVT_CTRL_TRIM_FLD)
  27. #define PVT_DATA 0x04
  28. #define PVT_DATA_VALID BIT(10)
  29. #define PVT_DATA_DATA_FLD 0
  30. #define PVT_DATA_DATA_MASK GENMASK(9, PVT_DATA_DATA_FLD)
  31. #define PVT_TTHRES 0x08
  32. #define PVT_VTHRES 0x0C
  33. #define PVT_LTHRES 0x10
  34. #define PVT_HTHRES 0x14
  35. #define PVT_STHRES 0x18
  36. #define PVT_THRES_LO_FLD 0
  37. #define PVT_THRES_LO_MASK GENMASK(9, PVT_THRES_LO_FLD)
  38. #define PVT_THRES_HI_FLD 10
  39. #define PVT_THRES_HI_MASK GENMASK(19, PVT_THRES_HI_FLD)
  40. #define PVT_TTIMEOUT 0x1C
  41. #define PVT_INTR_STAT 0x20
  42. #define PVT_INTR_MASK 0x24
  43. #define PVT_RAW_INTR_STAT 0x28
  44. #define PVT_INTR_DVALID BIT(0)
  45. #define PVT_INTR_TTHRES_LO BIT(1)
  46. #define PVT_INTR_TTHRES_HI BIT(2)
  47. #define PVT_INTR_VTHRES_LO BIT(3)
  48. #define PVT_INTR_VTHRES_HI BIT(4)
  49. #define PVT_INTR_LTHRES_LO BIT(5)
  50. #define PVT_INTR_LTHRES_HI BIT(6)
  51. #define PVT_INTR_HTHRES_LO BIT(7)
  52. #define PVT_INTR_HTHRES_HI BIT(8)
  53. #define PVT_INTR_STHRES_LO BIT(9)
  54. #define PVT_INTR_STHRES_HI BIT(10)
  55. #define PVT_INTR_ALL GENMASK(10, 0)
  56. #define PVT_CLR_INTR 0x2C
  57. /*
  58. * PVT sensors-related limits and default values
  59. * @PVT_TEMP_MIN: Minimal temperature in millidegrees of Celsius.
  60. * @PVT_TEMP_MAX: Maximal temperature in millidegrees of Celsius.
  61. * @PVT_TEMP_CHS: Number of temperature hwmon channels.
  62. * @PVT_VOLT_MIN: Minimal voltage in mV.
  63. * @PVT_VOLT_MAX: Maximal voltage in mV.
  64. * @PVT_VOLT_CHS: Number of voltage hwmon channels.
  65. * @PVT_DATA_MIN: Minimal PVT raw data value.
  66. * @PVT_DATA_MAX: Maximal PVT raw data value.
  67. * @PVT_TRIM_MIN: Minimal temperature sensor trim value.
  68. * @PVT_TRIM_MAX: Maximal temperature sensor trim value.
  69. * @PVT_TRIM_DEF: Default temperature sensor trim value (set a proper value
  70. * when one is determined for Baikal-T1 SoC).
  71. * @PVT_TRIM_TEMP: Maximum temperature encoded by the trim factor.
  72. * @PVT_TRIM_STEP: Temperature stride corresponding to the trim value.
  73. * @PVT_TOUT_MIN: Minimal timeout between samples in nanoseconds.
  74. * @PVT_TOUT_DEF: Default data measurements timeout. In case if alarms are
  75. * activated the PVT IRQ is enabled to be raised after each
  76. * conversion in order to have the thresholds checked and the
  77. * converted value cached. Too frequent conversions may cause
  78. * the system CPU overload. Lets set the 50ms delay between
  79. * them by default to prevent this.
  80. */
  81. #define PVT_TEMP_MIN -48380L
  82. #define PVT_TEMP_MAX 147438L
  83. #define PVT_TEMP_CHS 1
  84. #define PVT_VOLT_MIN 620L
  85. #define PVT_VOLT_MAX 1168L
  86. #define PVT_VOLT_CHS 4
  87. #define PVT_DATA_MIN 0
  88. #define PVT_DATA_MAX (PVT_DATA_DATA_MASK >> PVT_DATA_DATA_FLD)
  89. #define PVT_TRIM_MIN 0
  90. #define PVT_TRIM_MAX (PVT_CTRL_TRIM_MASK >> PVT_CTRL_TRIM_FLD)
  91. #define PVT_TRIM_TEMP 7130
  92. #define PVT_TRIM_STEP (PVT_TRIM_TEMP / PVT_TRIM_MAX)
  93. #define PVT_TRIM_DEF 0
  94. #define PVT_TOUT_MIN (NSEC_PER_SEC / 3000)
  95. #if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
  96. # define PVT_TOUT_DEF 60000
  97. #else
  98. # define PVT_TOUT_DEF 0
  99. #endif
  100. /*
  101. * enum pvt_sensor_type - Baikal-T1 PVT sensor types (correspond to each PVT
  102. * sampling mode)
  103. * @PVT_SENSOR*: helpers to traverse the sensors in loops.
  104. * @PVT_TEMP: PVT Temperature sensor.
  105. * @PVT_VOLT: PVT Voltage sensor.
  106. * @PVT_LVT: PVT Low-Voltage threshold sensor.
  107. * @PVT_HVT: PVT High-Voltage threshold sensor.
  108. * @PVT_SVT: PVT Standard-Voltage threshold sensor.
  109. */
  110. enum pvt_sensor_type {
  111. PVT_SENSOR_FIRST,
  112. PVT_TEMP = PVT_SENSOR_FIRST,
  113. PVT_VOLT,
  114. PVT_LVT,
  115. PVT_HVT,
  116. PVT_SVT,
  117. PVT_SENSOR_LAST = PVT_SVT,
  118. PVT_SENSORS_NUM
  119. };
  120. /*
  121. * enum pvt_clock_type - Baikal-T1 PVT clocks.
  122. * @PVT_CLOCK_APB: APB clock.
  123. * @PVT_CLOCK_REF: PVT reference clock.
  124. */
  125. enum pvt_clock_type {
  126. PVT_CLOCK_APB,
  127. PVT_CLOCK_REF,
  128. PVT_CLOCK_NUM
  129. };
  130. /*
  131. * struct pvt_sensor_info - Baikal-T1 PVT sensor informational structure
  132. * @channel: Sensor channel ID.
  133. * @label: hwmon sensor label.
  134. * @mode: PVT mode corresponding to the channel.
  135. * @thres_base: upper and lower threshold values of the sensor.
  136. * @thres_sts_lo: low threshold status bitfield.
  137. * @thres_sts_hi: high threshold status bitfield.
  138. * @type: Sensor type.
  139. * @attr_min_alarm: Min alarm attribute ID.
  140. * @attr_min_alarm: Max alarm attribute ID.
  141. */
  142. struct pvt_sensor_info {
  143. int channel;
  144. const char *label;
  145. u32 mode;
  146. unsigned long thres_base;
  147. u32 thres_sts_lo;
  148. u32 thres_sts_hi;
  149. enum hwmon_sensor_types type;
  150. u32 attr_min_alarm;
  151. u32 attr_max_alarm;
  152. };
  153. #define PVT_SENSOR_INFO(_ch, _label, _type, _mode, _thres) \
  154. { \
  155. .channel = _ch, \
  156. .label = _label, \
  157. .mode = PVT_CTRL_MODE_ ##_mode, \
  158. .thres_base = PVT_ ##_thres, \
  159. .thres_sts_lo = PVT_INTR_ ##_thres## _LO, \
  160. .thres_sts_hi = PVT_INTR_ ##_thres## _HI, \
  161. .type = _type, \
  162. .attr_min_alarm = _type## _min, \
  163. .attr_max_alarm = _type## _max, \
  164. }
  165. /*
  166. * struct pvt_cache - PVT sensors data cache
  167. * @data: data cache in raw format.
  168. * @thres_sts_lo: low threshold status saved on the previous data conversion.
  169. * @thres_sts_hi: high threshold status saved on the previous data conversion.
  170. * @data_seqlock: cached data seq-lock.
  171. * @conversion: data conversion completion.
  172. */
  173. struct pvt_cache {
  174. u32 data;
  175. #if defined(CONFIG_SENSORS_BT1_PVT_ALARMS)
  176. seqlock_t data_seqlock;
  177. u32 thres_sts_lo;
  178. u32 thres_sts_hi;
  179. #else
  180. struct completion conversion;
  181. #endif
  182. };
  183. /*
  184. * struct pvt_hwmon - Baikal-T1 PVT private data
  185. * @dev: device structure of the PVT platform device.
  186. * @hwmon: hwmon device structure.
  187. * @regs: pointer to the Baikal-T1 PVT registers region.
  188. * @irq: PVT events IRQ number.
  189. * @clks: Array of the PVT clocks descriptor (APB/ref clocks).
  190. * @ref_clk: Pointer to the reference clocks descriptor.
  191. * @iface_mtx: Generic interface mutex (used to lock the alarm registers
  192. * when the alarms enabled, or the data conversion interface
  193. * if alarms are disabled).
  194. * @sensor: current PVT sensor the data conversion is being performed for.
  195. * @cache: data cache descriptor.
  196. * @timeout: conversion timeout cache.
  197. */
  198. struct pvt_hwmon {
  199. struct device *dev;
  200. struct device *hwmon;
  201. void __iomem *regs;
  202. int irq;
  203. struct clk_bulk_data clks[PVT_CLOCK_NUM];
  204. struct mutex iface_mtx;
  205. enum pvt_sensor_type sensor;
  206. struct pvt_cache cache[PVT_SENSORS_NUM];
  207. ktime_t timeout;
  208. };
  209. /*
  210. * struct pvt_poly_term - a term descriptor of the PVT data translation
  211. * polynomial
  212. * @deg: degree of the term.
  213. * @coef: multiplication factor of the term.
  214. * @divider: distributed divider per each degree.
  215. * @divider_leftover: divider leftover, which couldn't be redistributed.
  216. */
  217. struct pvt_poly_term {
  218. unsigned int deg;
  219. long coef;
  220. long divider;
  221. long divider_leftover;
  222. };
  223. /*
  224. * struct pvt_poly - PVT data translation polynomial descriptor
  225. * @total_divider: total data divider.
  226. * @terms: polynomial terms up to a free one.
  227. */
  228. struct pvt_poly {
  229. long total_divider;
  230. struct pvt_poly_term terms[];
  231. };
  232. #endif /* __HWMON_BT1_PVT_H__ */