tsens2xxx.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (c) 2017-2020, 2021 The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2023, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #ifndef __QCOM_TSENS_H__
  7. #define __QCOM_TSENS_H__
  8. #include <linux/kernel.h>
  9. #include <linux/thermal.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/types.h>
  12. #include <linux/workqueue.h>
  13. #include <linux/io.h>
  14. #include <linux/delay.h>
  15. #include <linux/ipc_logging.h>
  16. #define DEBUG_SIZE 10
  17. #define TSENS_MAX_SENSORS 16
  18. #define TSENS_NUM_SENSORS_8937 11
  19. #define TSENS_NUM_SENSORS_405 10
  20. #define TSENS_CONTROLLER_ID(n) (n)
  21. #define TSENS_CTRL_ADDR(n) (n)
  22. #define TSENS_TM_SN_STATUS(n) ((n) + 0xa0)
  23. #define ONE_PT_CALIB 0x1
  24. #define ONE_PT_CALIB2 0x2
  25. #define TWO_PT_CALIB 0x3
  26. #define SLOPE_FACTOR 1000
  27. #define SLOPE_DEFAULT 3200
  28. #define IPC_LOGPAGES 10
  29. #define MIN_TEMP_DEF_OFFSET 0xFF
  30. #define TSENS_DRIVER_NAME "msm-tsens"
  31. enum tsens_trip_type {
  32. TSENS_TRIP_CONFIGURABLE_HI = 4,
  33. TSENS_TRIP_CONFIGURABLE_LOW
  34. };
  35. enum tsens_dbg_type {
  36. TSENS_DBG_POLL,
  37. TSENS_DBG_LOG_TEMP_READS,
  38. TSENS_DBG_LOG_INTERRUPT_TIMESTAMP,
  39. TSENS_DBG_LOG_BUS_ID_DATA,
  40. TSENS_DBG_MTC_DATA,
  41. TSENS_DBG_LOG_MAX
  42. };
  43. #define tsens_sec_to_msec_value 1000
  44. struct tsens_device;
  45. #ifdef CONFIG_DEBUG_FS
  46. #define TSENS_IPC(idx, dev, msg, args...) do { \
  47. if (dev) { \
  48. if ((idx == 0) && (dev)->ipc_log0) \
  49. ipc_log_string((dev)->ipc_log0, \
  50. "%s: " msg, __func__, args); \
  51. else if ((idx == 1) && (dev)->ipc_log1) \
  52. ipc_log_string((dev)->ipc_log1, \
  53. "%s: " msg, __func__, args); \
  54. else if ((idx == 2) && (dev)->ipc_log2) \
  55. ipc_log_string((dev)->ipc_log2, \
  56. "%s: " msg, __func__, args); \
  57. else \
  58. pr_debug("tsens: invalid logging index\n"); \
  59. } \
  60. } while (0)
  61. #define TSENS_DUMP(dev, msg, args...) do { \
  62. TSENS_IPC(2, dev, msg, args); \
  63. pr_info(msg, ##args); \
  64. } while (0)
  65. #define TSENS_ERR(dev, msg, args...) do { \
  66. pr_err(msg, ##args); \
  67. TSENS_IPC(1, dev, msg, args); \
  68. } while (0)
  69. #define TSENS_INFO(dev, msg, args...) do { \
  70. pr_info(msg, ##args); \
  71. TSENS_IPC(1, dev, msg, args); \
  72. } while (0)
  73. #define TSENS_DBG(dev, msg, args...) do { \
  74. pr_debug(msg, ##args); \
  75. if (dev) { \
  76. TSENS_IPC(0, dev, msg, args); \
  77. } \
  78. } while (0)
  79. #define TSENS_DBG1(dev, msg, args...) do { \
  80. pr_debug(msg, ##args); \
  81. if (dev) { \
  82. TSENS_IPC(1, dev, msg, args); \
  83. } \
  84. } while (0)
  85. #else
  86. #define TSENS_DBG1(dev, msg, x...) pr_debug(msg, ##x)
  87. #define TSENS_DBG(dev, msg, x...) pr_debug(msg, ##x)
  88. #define TSENS_INFO(dev, msg, x...) pr_info(msg, ##x)
  89. #define TSENS_ERR(dev, msg, x...) pr_err(msg, ##x)
  90. #define TSENS_DUMP(dev, msg, x...) pr_info(msg, ##x)
  91. #endif
  92. #if IS_ENABLED(CONFIG_THERMAL_TSENS_LEGACY)
  93. int tsens2xxx_dbg(struct tsens_device *data, u32 id, u32 dbg_type, int *temp);
  94. #else
  95. static inline int tsens2xxx_dbg(struct tsens_device *data, u32 id,
  96. u32 dbg_type, int *temp)
  97. { return -ENXIO; }
  98. #endif
  99. struct tsens_dbg {
  100. u32 idx;
  101. unsigned long long time_stmp[DEBUG_SIZE];
  102. unsigned long temp[DEBUG_SIZE];
  103. };
  104. struct tsens_dbg_context {
  105. struct tsens_device *tmdev;
  106. struct tsens_dbg sensor_dbg_info[TSENS_MAX_SENSORS];
  107. int tsens_critical_wd_cnt;
  108. u32 irq_idx;
  109. unsigned long long irq_time_stmp[DEBUG_SIZE];
  110. struct delayed_work tsens_critical_poll_test;
  111. };
  112. struct tsens_context {
  113. enum thermal_device_mode high_th_state;
  114. enum thermal_device_mode low_th_state;
  115. enum thermal_device_mode crit_th_state;
  116. int high_temp;
  117. int low_temp;
  118. int crit_temp;
  119. int high_adc_code;
  120. int low_adc_code;
  121. };
  122. struct tsens_sensor {
  123. struct tsens_device *tmdev;
  124. struct thermal_zone_device *tzd;
  125. u32 hw_id;
  126. u32 id;
  127. const char *sensor_name;
  128. struct tsens_context thr_state;
  129. int offset;
  130. int slope;
  131. int cached_temp;
  132. };
  133. /**
  134. * struct tsens_ops - operations as supported by the tsens device
  135. * @init: Function to initialize the tsens device
  136. * @get_temp: Function which returns the temp in millidegC
  137. */
  138. struct tsens_ops {
  139. int (*hw_init)(struct tsens_device *tmdev);
  140. int (*get_temp)(struct tsens_sensor *tm_sensor, int *temp);
  141. int (*set_trips)(struct tsens_sensor *tm_sensor, int low, int high);
  142. int (*interrupts_reg)(struct tsens_device *tmdev);
  143. int (*dbg)(struct tsens_device *tmdev, u32 id, u32 dbg_type,
  144. int *temp);
  145. int (*sensor_en)(struct tsens_device *tmdev, u32 sensor_id);
  146. int (*calibrate)(struct tsens_device *tmdev);
  147. int (*suspend)(struct tsens_device *tmdev);
  148. int (*resume)(struct tsens_device *tmdev);
  149. };
  150. struct tsens_irqs {
  151. const char *name;
  152. irqreturn_t (*handler)(int irq, void *data);
  153. };
  154. /**
  155. * struct tsens_data - tsens instance specific data
  156. * @num_sensors: Max number of sensors supported by platform
  157. * @ops: operations the tsens instance supports
  158. * @hw_ids: Subset of sensors ids supported by platform, if not the first n
  159. */
  160. struct tsens_data {
  161. const u32 num_sensors;
  162. const struct tsens_ops *ops;
  163. unsigned int *hw_ids;
  164. u32 temp_factor;
  165. bool cycle_monitor;
  166. u32 cycle_compltn_monitor_mask;
  167. bool wd_bark;
  168. u32 wd_bark_mask;
  169. bool valid_status_check;
  170. u32 ver_major;
  171. u32 ver_minor;
  172. };
  173. struct tsens_device {
  174. struct device *dev;
  175. struct platform_device *pdev;
  176. struct list_head list;
  177. struct regmap *map;
  178. struct regmap_field *status_field;
  179. void __iomem *tsens_srot_addr;
  180. void __iomem *tsens_tm_addr;
  181. void __iomem *tsens_calib_addr;
  182. const struct tsens_ops *ops;
  183. void *ipc_log0;
  184. void *ipc_log1;
  185. void *ipc_log2;
  186. phys_addr_t phys_addr_tm;
  187. struct tsens_dbg_context tsens_dbg;
  188. spinlock_t tsens_crit_lock;
  189. spinlock_t tsens_upp_low_lock;
  190. const struct tsens_data *ctrl_data;
  191. int trdy_fail_ctr;
  192. struct tsens_sensor zeroc;
  193. u8 zeroc_sensor_id;
  194. struct workqueue_struct *tsens_reinit_work;
  195. struct work_struct therm_fwk_notify;
  196. bool tsens_reinit_wa;
  197. int tsens_reinit_cnt;
  198. struct tsens_sensor sensor[0];
  199. };
  200. extern const struct tsens_data data_tsens2xxx, data_tsens23xx, data_tsens24xx,
  201. data_tsens26xx;
  202. extern struct list_head tsens_device_list;
  203. extern int tsens_2xxx_get_zeroc_status(
  204. struct tsens_sensor *sensor, int *status);
  205. #endif /* __QCOM_TSENS_H__ */