brcmstb_thermal.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. /* Convert a HW code to a temperature reading (millidegree celsius) */
  103. static inline int avs_tmon_code_to_temp(struct thermal_zone_device *tz,
  104. u32 code)
  105. {
  106. return (AVS_TMON_TEMP_OFFSET -
  107. (int)((code & AVS_TMON_TEMP_MAX) * AVS_TMON_TEMP_SLOPE));
  108. }
  109. /*
  110. * Convert a temperature value (millidegree celsius) to a HW code
  111. *
  112. * @temp: temperature to convert
  113. * @low: if true, round toward the low side
  114. */
  115. static inline u32 avs_tmon_temp_to_code(struct thermal_zone_device *tz,
  116. int temp, bool low)
  117. {
  118. if (temp < AVS_TMON_TEMP_MIN)
  119. return AVS_TMON_TEMP_MAX; /* Maximum code value */
  120. if (temp >= AVS_TMON_TEMP_OFFSET)
  121. return 0; /* Minimum code value */
  122. if (low)
  123. return (u32)(DIV_ROUND_UP(AVS_TMON_TEMP_OFFSET - temp,
  124. AVS_TMON_TEMP_SLOPE));
  125. else
  126. return (u32)((AVS_TMON_TEMP_OFFSET - temp) /
  127. AVS_TMON_TEMP_SLOPE);
  128. }
  129. static int brcmstb_get_temp(void *data, int *temp)
  130. {
  131. struct brcmstb_thermal_priv *priv = data;
  132. u32 val;
  133. long t;
  134. val = __raw_readl(priv->tmon_base + AVS_TMON_STATUS);
  135. if (!(val & AVS_TMON_STATUS_valid_msk)) {
  136. dev_err(priv->dev, "reading not valid\n");
  137. return -EIO;
  138. }
  139. val = (val & AVS_TMON_STATUS_data_msk) >> AVS_TMON_STATUS_data_shift;
  140. t = avs_tmon_code_to_temp(priv->thermal, val);
  141. if (t < 0)
  142. *temp = 0;
  143. else
  144. *temp = t;
  145. return 0;
  146. }
  147. static void avs_tmon_trip_enable(struct brcmstb_thermal_priv *priv,
  148. enum avs_tmon_trip_type type, int en)
  149. {
  150. struct avs_tmon_trip *trip = &avs_tmon_trips[type];
  151. u32 val = __raw_readl(priv->tmon_base + trip->enable_offs);
  152. dev_dbg(priv->dev, "%sable trip, type %d\n", en ? "en" : "dis", type);
  153. if (en)
  154. val |= trip->enable_mask;
  155. else
  156. val &= ~trip->enable_mask;
  157. __raw_writel(val, priv->tmon_base + trip->enable_offs);
  158. }
  159. static int avs_tmon_get_trip_temp(struct brcmstb_thermal_priv *priv,
  160. enum avs_tmon_trip_type type)
  161. {
  162. struct avs_tmon_trip *trip = &avs_tmon_trips[type];
  163. u32 val = __raw_readl(priv->tmon_base + trip->reg_offs);
  164. val &= trip->reg_msk;
  165. val >>= trip->reg_shift;
  166. return avs_tmon_code_to_temp(priv->thermal, val);
  167. }
  168. static void avs_tmon_set_trip_temp(struct brcmstb_thermal_priv *priv,
  169. enum avs_tmon_trip_type type,
  170. int temp)
  171. {
  172. struct avs_tmon_trip *trip = &avs_tmon_trips[type];
  173. u32 val, orig;
  174. dev_dbg(priv->dev, "set temp %d to %d\n", type, temp);
  175. /* round toward low temp for the low interrupt */
  176. val = avs_tmon_temp_to_code(priv->thermal, temp,
  177. type == TMON_TRIP_TYPE_LOW);
  178. val <<= trip->reg_shift;
  179. val &= trip->reg_msk;
  180. orig = __raw_readl(priv->tmon_base + trip->reg_offs);
  181. orig &= ~trip->reg_msk;
  182. orig |= val;
  183. __raw_writel(orig, priv->tmon_base + trip->reg_offs);
  184. }
  185. static int avs_tmon_get_intr_temp(struct brcmstb_thermal_priv *priv)
  186. {
  187. u32 val;
  188. val = __raw_readl(priv->tmon_base + AVS_TMON_TEMP_INT_CODE);
  189. return avs_tmon_code_to_temp(priv->thermal, val);
  190. }
  191. static irqreturn_t brcmstb_tmon_irq_thread(int irq, void *data)
  192. {
  193. struct brcmstb_thermal_priv *priv = data;
  194. int low, high, intr;
  195. low = avs_tmon_get_trip_temp(priv, TMON_TRIP_TYPE_LOW);
  196. high = avs_tmon_get_trip_temp(priv, TMON_TRIP_TYPE_HIGH);
  197. intr = avs_tmon_get_intr_temp(priv);
  198. dev_dbg(priv->dev, "low/intr/high: %d/%d/%d\n",
  199. low, intr, high);
  200. /* Disable high-temp until next threshold shift */
  201. if (intr >= high)
  202. avs_tmon_trip_enable(priv, TMON_TRIP_TYPE_HIGH, 0);
  203. /* Disable low-temp until next threshold shift */
  204. if (intr <= low)
  205. avs_tmon_trip_enable(priv, TMON_TRIP_TYPE_LOW, 0);
  206. /*
  207. * Notify using the interrupt temperature, in case the temperature
  208. * changes before it can next be read out
  209. */
  210. thermal_zone_device_update(priv->thermal, intr);
  211. return IRQ_HANDLED;
  212. }
  213. static int brcmstb_set_trips(void *data, int low, int high)
  214. {
  215. struct brcmstb_thermal_priv *priv = data;
  216. dev_dbg(priv->dev, "set trips %d <--> %d\n", low, high);
  217. /*
  218. * Disable low-temp if "low" is too small. As per thermal framework
  219. * API, we use -INT_MAX rather than INT_MIN.
  220. */
  221. if (low <= -INT_MAX) {
  222. avs_tmon_trip_enable(priv, TMON_TRIP_TYPE_LOW, 0);
  223. } else {
  224. avs_tmon_set_trip_temp(priv, TMON_TRIP_TYPE_LOW, low);
  225. avs_tmon_trip_enable(priv, TMON_TRIP_TYPE_LOW, 1);
  226. }
  227. /* Disable high-temp if "high" is too big. */
  228. if (high == INT_MAX) {
  229. avs_tmon_trip_enable(priv, TMON_TRIP_TYPE_HIGH, 0);
  230. } else {
  231. avs_tmon_set_trip_temp(priv, TMON_TRIP_TYPE_HIGH, high);
  232. avs_tmon_trip_enable(priv, TMON_TRIP_TYPE_HIGH, 1);
  233. }
  234. return 0;
  235. }
  236. static struct thermal_zone_of_device_ops of_ops = {
  237. .get_temp = brcmstb_get_temp,
  238. .set_trips = brcmstb_set_trips,
  239. };
  240. static const struct of_device_id brcmstb_thermal_id_table[] = {
  241. { .compatible = "brcm,avs-tmon" },
  242. {},
  243. };
  244. MODULE_DEVICE_TABLE(of, brcmstb_thermal_id_table);
  245. static int brcmstb_thermal_probe(struct platform_device *pdev)
  246. {
  247. struct thermal_zone_device *thermal;
  248. struct brcmstb_thermal_priv *priv;
  249. struct resource *res;
  250. int irq, ret;
  251. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  252. if (!priv)
  253. return -ENOMEM;
  254. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  255. priv->tmon_base = devm_ioremap_resource(&pdev->dev, res);
  256. if (IS_ERR(priv->tmon_base))
  257. return PTR_ERR(priv->tmon_base);
  258. priv->dev = &pdev->dev;
  259. platform_set_drvdata(pdev, priv);
  260. thermal = thermal_zone_of_sensor_register(&pdev->dev, 0, priv, &of_ops);
  261. if (IS_ERR(thermal)) {
  262. ret = PTR_ERR(thermal);
  263. dev_err(&pdev->dev, "could not register sensor: %d\n", ret);
  264. return ret;
  265. }
  266. priv->thermal = thermal;
  267. irq = platform_get_irq(pdev, 0);
  268. if (irq < 0) {
  269. dev_err(&pdev->dev, "could not get IRQ\n");
  270. ret = irq;
  271. goto err;
  272. }
  273. ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  274. brcmstb_tmon_irq_thread, IRQF_ONESHOT,
  275. DRV_NAME, priv);
  276. if (ret < 0) {
  277. dev_err(&pdev->dev, "could not request IRQ: %d\n", ret);
  278. goto err;
  279. }
  280. dev_info(&pdev->dev, "registered AVS TMON of-sensor driver\n");
  281. return 0;
  282. err:
  283. thermal_zone_of_sensor_unregister(&pdev->dev, thermal);
  284. return ret;
  285. }
  286. static int brcmstb_thermal_exit(struct platform_device *pdev)
  287. {
  288. struct brcmstb_thermal_priv *priv = platform_get_drvdata(pdev);
  289. struct thermal_zone_device *thermal = priv->thermal;
  290. if (thermal)
  291. thermal_zone_of_sensor_unregister(&pdev->dev, priv->thermal);
  292. return 0;
  293. }
  294. static struct platform_driver brcmstb_thermal_driver = {
  295. .probe = brcmstb_thermal_probe,
  296. .remove = brcmstb_thermal_exit,
  297. .driver = {
  298. .name = DRV_NAME,
  299. .of_match_table = brcmstb_thermal_id_table,
  300. },
  301. };
  302. module_platform_driver(brcmstb_thermal_driver);
  303. MODULE_LICENSE("GPL v2");
  304. MODULE_AUTHOR("Brian Norris");
  305. MODULE_DESCRIPTION("Broadcom STB AVS TMON thermal driver");