brcmstb_thermal.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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/platform_device.h>
  18. #include <linux/of_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 = tz->devdata;
  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. dev_err(priv->dev, "reading not valid\n");
  136. return -EIO;
  137. }
  138. val = (val & AVS_TMON_STATUS_data_msk) >> AVS_TMON_STATUS_data_shift;
  139. t = avs_tmon_code_to_temp(priv, val);
  140. if (t < 0)
  141. *temp = 0;
  142. else
  143. *temp = t;
  144. return 0;
  145. }
  146. static void avs_tmon_trip_enable(struct brcmstb_thermal_priv *priv,
  147. enum avs_tmon_trip_type type, int en)
  148. {
  149. struct avs_tmon_trip *trip = &avs_tmon_trips[type];
  150. u32 val = __raw_readl(priv->tmon_base + trip->enable_offs);
  151. dev_dbg(priv->dev, "%sable trip, type %d\n", en ? "en" : "dis", type);
  152. if (en)
  153. val |= trip->enable_mask;
  154. else
  155. val &= ~trip->enable_mask;
  156. __raw_writel(val, priv->tmon_base + trip->enable_offs);
  157. }
  158. static int avs_tmon_get_trip_temp(struct brcmstb_thermal_priv *priv,
  159. enum avs_tmon_trip_type type)
  160. {
  161. struct avs_tmon_trip *trip = &avs_tmon_trips[type];
  162. u32 val = __raw_readl(priv->tmon_base + trip->reg_offs);
  163. val &= trip->reg_msk;
  164. val >>= trip->reg_shift;
  165. return avs_tmon_code_to_temp(priv, val);
  166. }
  167. static void avs_tmon_set_trip_temp(struct brcmstb_thermal_priv *priv,
  168. enum avs_tmon_trip_type type,
  169. int temp)
  170. {
  171. struct avs_tmon_trip *trip = &avs_tmon_trips[type];
  172. u32 val, orig;
  173. dev_dbg(priv->dev, "set temp %d to %d\n", type, temp);
  174. /* round toward low temp for the low interrupt */
  175. val = avs_tmon_temp_to_code(priv, temp,
  176. type == TMON_TRIP_TYPE_LOW);
  177. val <<= trip->reg_shift;
  178. val &= trip->reg_msk;
  179. orig = __raw_readl(priv->tmon_base + trip->reg_offs);
  180. orig &= ~trip->reg_msk;
  181. orig |= val;
  182. __raw_writel(orig, priv->tmon_base + trip->reg_offs);
  183. }
  184. static int avs_tmon_get_intr_temp(struct brcmstb_thermal_priv *priv)
  185. {
  186. u32 val;
  187. val = __raw_readl(priv->tmon_base + AVS_TMON_TEMP_INT_CODE);
  188. return avs_tmon_code_to_temp(priv, val);
  189. }
  190. static irqreturn_t brcmstb_tmon_irq_thread(int irq, void *data)
  191. {
  192. struct brcmstb_thermal_priv *priv = data;
  193. int low, high, intr;
  194. low = avs_tmon_get_trip_temp(priv, TMON_TRIP_TYPE_LOW);
  195. high = avs_tmon_get_trip_temp(priv, TMON_TRIP_TYPE_HIGH);
  196. intr = avs_tmon_get_intr_temp(priv);
  197. dev_dbg(priv->dev, "low/intr/high: %d/%d/%d\n",
  198. low, intr, high);
  199. /* Disable high-temp until next threshold shift */
  200. if (intr >= high)
  201. avs_tmon_trip_enable(priv, TMON_TRIP_TYPE_HIGH, 0);
  202. /* Disable low-temp until next threshold shift */
  203. if (intr <= low)
  204. avs_tmon_trip_enable(priv, TMON_TRIP_TYPE_LOW, 0);
  205. /*
  206. * Notify using the interrupt temperature, in case the temperature
  207. * changes before it can next be read out
  208. */
  209. thermal_zone_device_update(priv->thermal, intr);
  210. return IRQ_HANDLED;
  211. }
  212. static int brcmstb_set_trips(struct thermal_zone_device *tz, int low, int high)
  213. {
  214. struct brcmstb_thermal_priv *priv = tz->devdata;
  215. dev_dbg(priv->dev, "set trips %d <--> %d\n", low, high);
  216. /*
  217. * Disable low-temp if "low" is too small. As per thermal framework
  218. * API, we use -INT_MAX rather than INT_MIN.
  219. */
  220. if (low <= -INT_MAX) {
  221. avs_tmon_trip_enable(priv, TMON_TRIP_TYPE_LOW, 0);
  222. } else {
  223. avs_tmon_set_trip_temp(priv, TMON_TRIP_TYPE_LOW, low);
  224. avs_tmon_trip_enable(priv, TMON_TRIP_TYPE_LOW, 1);
  225. }
  226. /* Disable high-temp if "high" is too big. */
  227. if (high == INT_MAX) {
  228. avs_tmon_trip_enable(priv, TMON_TRIP_TYPE_HIGH, 0);
  229. } else {
  230. avs_tmon_set_trip_temp(priv, TMON_TRIP_TYPE_HIGH, high);
  231. avs_tmon_trip_enable(priv, TMON_TRIP_TYPE_HIGH, 1);
  232. }
  233. return 0;
  234. }
  235. static const struct thermal_zone_device_ops brcmstb_16nm_of_ops = {
  236. .get_temp = brcmstb_get_temp,
  237. };
  238. static const struct brcmstb_thermal_params brcmstb_16nm_params = {
  239. .offset = 457829,
  240. .mult = 557,
  241. .of_ops = &brcmstb_16nm_of_ops,
  242. };
  243. static const struct thermal_zone_device_ops brcmstb_28nm_of_ops = {
  244. .get_temp = brcmstb_get_temp,
  245. .set_trips = brcmstb_set_trips,
  246. };
  247. static const struct brcmstb_thermal_params brcmstb_28nm_params = {
  248. .offset = 410040,
  249. .mult = 487,
  250. .of_ops = &brcmstb_28nm_of_ops,
  251. };
  252. static const struct of_device_id brcmstb_thermal_id_table[] = {
  253. { .compatible = "brcm,avs-tmon-bcm7216", .data = &brcmstb_16nm_params },
  254. { .compatible = "brcm,avs-tmon", .data = &brcmstb_28nm_params },
  255. {},
  256. };
  257. MODULE_DEVICE_TABLE(of, brcmstb_thermal_id_table);
  258. static int brcmstb_thermal_probe(struct platform_device *pdev)
  259. {
  260. const struct thermal_zone_device_ops *of_ops;
  261. struct thermal_zone_device *thermal;
  262. struct brcmstb_thermal_priv *priv;
  263. struct resource *res;
  264. int irq, ret;
  265. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  266. if (!priv)
  267. return -ENOMEM;
  268. priv->temp_params = of_device_get_match_data(&pdev->dev);
  269. if (!priv->temp_params)
  270. return -EINVAL;
  271. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  272. priv->tmon_base = devm_ioremap_resource(&pdev->dev, res);
  273. if (IS_ERR(priv->tmon_base))
  274. return PTR_ERR(priv->tmon_base);
  275. priv->dev = &pdev->dev;
  276. platform_set_drvdata(pdev, priv);
  277. of_ops = priv->temp_params->of_ops;
  278. thermal = devm_thermal_of_zone_register(&pdev->dev, 0, priv,
  279. of_ops);
  280. if (IS_ERR(thermal)) {
  281. ret = PTR_ERR(thermal);
  282. dev_err(&pdev->dev, "could not register sensor: %d\n", ret);
  283. return ret;
  284. }
  285. priv->thermal = thermal;
  286. irq = platform_get_irq_optional(pdev, 0);
  287. if (irq >= 0) {
  288. ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  289. brcmstb_tmon_irq_thread,
  290. IRQF_ONESHOT,
  291. DRV_NAME, priv);
  292. if (ret < 0) {
  293. dev_err(&pdev->dev, "could not request IRQ: %d\n", ret);
  294. return ret;
  295. }
  296. }
  297. dev_info(&pdev->dev, "registered AVS TMON of-sensor driver\n");
  298. return 0;
  299. }
  300. static struct platform_driver brcmstb_thermal_driver = {
  301. .probe = brcmstb_thermal_probe,
  302. .driver = {
  303. .name = DRV_NAME,
  304. .of_match_table = brcmstb_thermal_id_table,
  305. },
  306. };
  307. module_platform_driver(brcmstb_thermal_driver);
  308. MODULE_LICENSE("GPL v2");
  309. MODULE_AUTHOR("Brian Norris");
  310. MODULE_DESCRIPTION("Broadcom STB AVS TMON thermal driver");