brcmstb_thermal.c 10.0 KB

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