brcmstb_thermal.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Broadcom STB AVS TMON thermal sensor driver
  4. *
  5. * Copyright (c) 2015-2017 Broadcom
  6. */
  7. #define DRV_NAME "brcmstb_thermal"
  8. #define pr_fmt(fmt) DRV_NAME ": " fmt
  9. #include <linux/bitops.h>
  10. #include <linux/device.h>
  11. #include <linux/err.h>
  12. #include <linux/io.h>
  13. #include <linux/irqreturn.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/thermal.h>
  20. #define AVS_TMON_STATUS 0x00
  21. #define AVS_TMON_STATUS_valid_msk BIT(11)
  22. #define AVS_TMON_STATUS_data_msk GENMASK(10, 1)
  23. #define AVS_TMON_STATUS_data_shift 1
  24. #define AVS_TMON_EN_OVERTEMP_RESET 0x04
  25. #define AVS_TMON_EN_OVERTEMP_RESET_msk BIT(0)
  26. #define AVS_TMON_RESET_THRESH 0x08
  27. #define AVS_TMON_RESET_THRESH_msk GENMASK(10, 1)
  28. #define AVS_TMON_RESET_THRESH_shift 1
  29. #define AVS_TMON_INT_IDLE_TIME 0x10
  30. #define AVS_TMON_EN_TEMP_INT_SRCS 0x14
  31. #define AVS_TMON_EN_TEMP_INT_SRCS_high BIT(1)
  32. #define AVS_TMON_EN_TEMP_INT_SRCS_low BIT(0)
  33. #define AVS_TMON_INT_THRESH 0x18
  34. #define AVS_TMON_INT_THRESH_high_msk GENMASK(26, 17)
  35. #define AVS_TMON_INT_THRESH_high_shift 17
  36. #define AVS_TMON_INT_THRESH_low_msk GENMASK(10, 1)
  37. #define AVS_TMON_INT_THRESH_low_shift 1
  38. #define AVS_TMON_TEMP_INT_CODE 0x1c
  39. #define AVS_TMON_TP_TEST_ENABLE 0x20
  40. /* Default coefficients */
  41. #define AVS_TMON_TEMP_SLOPE 487
  42. #define AVS_TMON_TEMP_OFFSET 410040
  43. /* HW related temperature constants */
  44. #define AVS_TMON_TEMP_MAX 0x3ff
  45. #define AVS_TMON_TEMP_MIN -88161
  46. #define AVS_TMON_TEMP_MASK AVS_TMON_TEMP_MAX
  47. enum avs_tmon_trip_type {
  48. TMON_TRIP_TYPE_LOW = 0,
  49. TMON_TRIP_TYPE_HIGH,
  50. TMON_TRIP_TYPE_RESET,
  51. TMON_TRIP_TYPE_MAX,
  52. };
  53. struct avs_tmon_trip {
  54. /* HW bit to enable the trip */
  55. u32 enable_offs;
  56. u32 enable_mask;
  57. /* HW field to read the trip temperature */
  58. u32 reg_offs;
  59. u32 reg_msk;
  60. int reg_shift;
  61. };
  62. static struct avs_tmon_trip avs_tmon_trips[] = {
  63. /* Trips when temperature is below threshold */
  64. [TMON_TRIP_TYPE_LOW] = {
  65. .enable_offs = AVS_TMON_EN_TEMP_INT_SRCS,
  66. .enable_mask = AVS_TMON_EN_TEMP_INT_SRCS_low,
  67. .reg_offs = AVS_TMON_INT_THRESH,
  68. .reg_msk = AVS_TMON_INT_THRESH_low_msk,
  69. .reg_shift = AVS_TMON_INT_THRESH_low_shift,
  70. },
  71. /* Trips when temperature is above threshold */
  72. [TMON_TRIP_TYPE_HIGH] = {
  73. .enable_offs = AVS_TMON_EN_TEMP_INT_SRCS,
  74. .enable_mask = AVS_TMON_EN_TEMP_INT_SRCS_high,
  75. .reg_offs = AVS_TMON_INT_THRESH,
  76. .reg_msk = AVS_TMON_INT_THRESH_high_msk,
  77. .reg_shift = AVS_TMON_INT_THRESH_high_shift,
  78. },
  79. /* Automatically resets chip when above threshold */
  80. [TMON_TRIP_TYPE_RESET] = {
  81. .enable_offs = AVS_TMON_EN_OVERTEMP_RESET,
  82. .enable_mask = AVS_TMON_EN_OVERTEMP_RESET_msk,
  83. .reg_offs = AVS_TMON_RESET_THRESH,
  84. .reg_msk = AVS_TMON_RESET_THRESH_msk,
  85. .reg_shift = AVS_TMON_RESET_THRESH_shift,
  86. },
  87. };
  88. struct brcmstb_thermal_params {
  89. unsigned int offset;
  90. unsigned int mult;
  91. const struct thermal_zone_device_ops *of_ops;
  92. };
  93. struct brcmstb_thermal_priv {
  94. void __iomem *tmon_base;
  95. struct device *dev;
  96. struct thermal_zone_device *thermal;
  97. /* Process specific thermal parameters used for calculations */
  98. const struct brcmstb_thermal_params *temp_params;
  99. };
  100. /* Convert a HW code to a temperature reading (millidegree celsius) */
  101. static inline int avs_tmon_code_to_temp(struct brcmstb_thermal_priv *priv,
  102. u32 code)
  103. {
  104. int offset = priv->temp_params->offset;
  105. int mult = priv->temp_params->mult;
  106. return (offset - (int)((code & AVS_TMON_TEMP_MASK) * mult));
  107. }
  108. /*
  109. * Convert a temperature value (millidegree celsius) to a HW code
  110. *
  111. * @temp: temperature to convert
  112. * @low: if true, round toward the low side
  113. */
  114. static inline u32 avs_tmon_temp_to_code(struct brcmstb_thermal_priv *priv,
  115. int temp, bool low)
  116. {
  117. int offset = priv->temp_params->offset;
  118. int mult = priv->temp_params->mult;
  119. if (temp < AVS_TMON_TEMP_MIN)
  120. return AVS_TMON_TEMP_MAX; /* Maximum code value */
  121. if (temp >= offset)
  122. return 0; /* Minimum code value */
  123. if (low)
  124. return (u32)(DIV_ROUND_UP(offset - temp, mult));
  125. else
  126. return (u32)((offset - temp) / mult);
  127. }
  128. static int brcmstb_get_temp(struct thermal_zone_device *tz, int *temp)
  129. {
  130. struct brcmstb_thermal_priv *priv = thermal_zone_device_priv(tz);
  131. u32 val;
  132. long t;
  133. val = __raw_readl(priv->tmon_base + AVS_TMON_STATUS);
  134. if (!(val & AVS_TMON_STATUS_valid_msk))
  135. return -EIO;
  136. val = (val & AVS_TMON_STATUS_data_msk) >> AVS_TMON_STATUS_data_shift;
  137. t = avs_tmon_code_to_temp(priv, val);
  138. if (t < 0)
  139. *temp = 0;
  140. else
  141. *temp = t;
  142. return 0;
  143. }
  144. static void avs_tmon_trip_enable(struct brcmstb_thermal_priv *priv,
  145. enum avs_tmon_trip_type type, int en)
  146. {
  147. struct avs_tmon_trip *trip = &avs_tmon_trips[type];
  148. u32 val = __raw_readl(priv->tmon_base + trip->enable_offs);
  149. dev_dbg(priv->dev, "%sable trip, type %d\n", en ? "en" : "dis", type);
  150. if (en)
  151. val |= trip->enable_mask;
  152. else
  153. val &= ~trip->enable_mask;
  154. __raw_writel(val, priv->tmon_base + trip->enable_offs);
  155. }
  156. static int avs_tmon_get_trip_temp(struct brcmstb_thermal_priv *priv,
  157. enum avs_tmon_trip_type type)
  158. {
  159. struct avs_tmon_trip *trip = &avs_tmon_trips[type];
  160. u32 val = __raw_readl(priv->tmon_base + trip->reg_offs);
  161. val &= trip->reg_msk;
  162. val >>= trip->reg_shift;
  163. return avs_tmon_code_to_temp(priv, val);
  164. }
  165. static void avs_tmon_set_trip_temp(struct brcmstb_thermal_priv *priv,
  166. enum avs_tmon_trip_type type,
  167. int temp)
  168. {
  169. struct avs_tmon_trip *trip = &avs_tmon_trips[type];
  170. u32 val, orig;
  171. dev_dbg(priv->dev, "set temp %d to %d\n", type, temp);
  172. /* round toward low temp for the low interrupt */
  173. val = avs_tmon_temp_to_code(priv, temp,
  174. type == TMON_TRIP_TYPE_LOW);
  175. val <<= trip->reg_shift;
  176. val &= trip->reg_msk;
  177. orig = __raw_readl(priv->tmon_base + trip->reg_offs);
  178. orig &= ~trip->reg_msk;
  179. orig |= val;
  180. __raw_writel(orig, priv->tmon_base + trip->reg_offs);
  181. }
  182. static int avs_tmon_get_intr_temp(struct brcmstb_thermal_priv *priv)
  183. {
  184. u32 val;
  185. val = __raw_readl(priv->tmon_base + AVS_TMON_TEMP_INT_CODE);
  186. return avs_tmon_code_to_temp(priv, val);
  187. }
  188. static irqreturn_t brcmstb_tmon_irq_thread(int irq, void *data)
  189. {
  190. struct brcmstb_thermal_priv *priv = data;
  191. int low, high, intr;
  192. low = avs_tmon_get_trip_temp(priv, TMON_TRIP_TYPE_LOW);
  193. high = avs_tmon_get_trip_temp(priv, TMON_TRIP_TYPE_HIGH);
  194. intr = avs_tmon_get_intr_temp(priv);
  195. dev_dbg(priv->dev, "low/intr/high: %d/%d/%d\n",
  196. low, intr, high);
  197. /* Disable high-temp until next threshold shift */
  198. if (intr >= high)
  199. avs_tmon_trip_enable(priv, TMON_TRIP_TYPE_HIGH, 0);
  200. /* Disable low-temp until next threshold shift */
  201. if (intr <= low)
  202. avs_tmon_trip_enable(priv, TMON_TRIP_TYPE_LOW, 0);
  203. /*
  204. * Notify using the interrupt temperature, in case the temperature
  205. * changes before it can next be read out
  206. */
  207. thermal_zone_device_update(priv->thermal, intr);
  208. return IRQ_HANDLED;
  209. }
  210. static int brcmstb_set_trips(struct thermal_zone_device *tz, int low, int high)
  211. {
  212. struct brcmstb_thermal_priv *priv = thermal_zone_device_priv(tz);
  213. dev_dbg(priv->dev, "set trips %d <--> %d\n", low, high);
  214. /*
  215. * Disable low-temp if "low" is too small. As per thermal framework
  216. * API, we use -INT_MAX rather than INT_MIN.
  217. */
  218. if (low <= -INT_MAX) {
  219. avs_tmon_trip_enable(priv, TMON_TRIP_TYPE_LOW, 0);
  220. } else {
  221. avs_tmon_set_trip_temp(priv, TMON_TRIP_TYPE_LOW, low);
  222. avs_tmon_trip_enable(priv, TMON_TRIP_TYPE_LOW, 1);
  223. }
  224. /* Disable high-temp if "high" is too big. */
  225. if (high == INT_MAX) {
  226. avs_tmon_trip_enable(priv, TMON_TRIP_TYPE_HIGH, 0);
  227. } else {
  228. avs_tmon_set_trip_temp(priv, TMON_TRIP_TYPE_HIGH, high);
  229. avs_tmon_trip_enable(priv, TMON_TRIP_TYPE_HIGH, 1);
  230. }
  231. return 0;
  232. }
  233. static const struct thermal_zone_device_ops brcmstb_of_ops = {
  234. .get_temp = brcmstb_get_temp,
  235. };
  236. static const struct brcmstb_thermal_params brcmstb_8nm_params = {
  237. .offset = 418670,
  238. .mult = 509,
  239. .of_ops = &brcmstb_of_ops,
  240. };
  241. static const struct brcmstb_thermal_params brcmstb_16nm_params = {
  242. .offset = 457829,
  243. .mult = 557,
  244. .of_ops = &brcmstb_of_ops,
  245. };
  246. static const struct thermal_zone_device_ops brcmstb_28nm_of_ops = {
  247. .get_temp = brcmstb_get_temp,
  248. .set_trips = brcmstb_set_trips,
  249. };
  250. static const struct brcmstb_thermal_params brcmstb_28nm_params = {
  251. .offset = 410040,
  252. .mult = 487,
  253. .of_ops = &brcmstb_28nm_of_ops,
  254. };
  255. static const struct of_device_id brcmstb_thermal_id_table[] = {
  256. { .compatible = "brcm,avs-tmon-bcm74110", .data = &brcmstb_8nm_params },
  257. { .compatible = "brcm,avs-tmon-bcm7216", .data = &brcmstb_16nm_params },
  258. { .compatible = "brcm,avs-tmon", .data = &brcmstb_28nm_params },
  259. {},
  260. };
  261. MODULE_DEVICE_TABLE(of, brcmstb_thermal_id_table);
  262. static int brcmstb_thermal_probe(struct platform_device *pdev)
  263. {
  264. const struct thermal_zone_device_ops *of_ops;
  265. struct thermal_zone_device *thermal;
  266. struct brcmstb_thermal_priv *priv;
  267. int irq, ret;
  268. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  269. if (!priv)
  270. return -ENOMEM;
  271. priv->temp_params = of_device_get_match_data(&pdev->dev);
  272. if (!priv->temp_params)
  273. return -EINVAL;
  274. priv->tmon_base = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
  275. if (IS_ERR(priv->tmon_base))
  276. return PTR_ERR(priv->tmon_base);
  277. priv->dev = &pdev->dev;
  278. of_ops = priv->temp_params->of_ops;
  279. thermal = devm_thermal_of_zone_register(&pdev->dev, 0, priv,
  280. of_ops);
  281. if (IS_ERR(thermal))
  282. return dev_err_probe(&pdev->dev, PTR_ERR(thermal),
  283. "could not register sensor\n");
  284. priv->thermal = thermal;
  285. irq = platform_get_irq_optional(pdev, 0);
  286. if (irq >= 0) {
  287. ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  288. brcmstb_tmon_irq_thread,
  289. IRQF_ONESHOT,
  290. DRV_NAME, priv);
  291. if (ret < 0)
  292. return dev_err_probe(&pdev->dev, ret,
  293. "could not request IRQ\n");
  294. }
  295. dev_info(&pdev->dev, "registered AVS TMON of-sensor driver\n");
  296. return 0;
  297. }
  298. static struct platform_driver brcmstb_thermal_driver = {
  299. .probe = brcmstb_thermal_probe,
  300. .driver = {
  301. .name = DRV_NAME,
  302. .of_match_table = brcmstb_thermal_id_table,
  303. },
  304. };
  305. module_platform_driver(brcmstb_thermal_driver);
  306. MODULE_LICENSE("GPL v2");
  307. MODULE_AUTHOR("Brian Norris");
  308. MODULE_DESCRIPTION("Broadcom STB AVS TMON thermal driver");