intel_pch_thermal.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /* intel_pch_thermal.c - Intel PCH Thermal driver
  2. *
  3. * Copyright (c) 2015, Intel Corporation.
  4. *
  5. * Authors:
  6. * Tushar Dave <tushar.n.dave@intel.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms and conditions of the GNU General Public License,
  10. * version 2, as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. */
  18. #include <linux/module.h>
  19. #include <linux/types.h>
  20. #include <linux/init.h>
  21. #include <linux/pci.h>
  22. #include <linux/thermal.h>
  23. /* Intel PCH thermal Device IDs */
  24. #define PCH_THERMAL_DID_WPT 0x9CA4 /* Wildcat Point */
  25. #define PCH_THERMAL_DID_SKL 0x9D31 /* Skylake PCH */
  26. /* Wildcat Point-LP PCH Thermal registers */
  27. #define WPT_TEMP 0x0000 /* Temperature */
  28. #define WPT_TSC 0x04 /* Thermal Sensor Control */
  29. #define WPT_TSS 0x06 /* Thermal Sensor Status */
  30. #define WPT_TSEL 0x08 /* Thermal Sensor Enable and Lock */
  31. #define WPT_TSREL 0x0A /* Thermal Sensor Report Enable and Lock */
  32. #define WPT_TSMIC 0x0C /* Thermal Sensor SMI Control */
  33. #define WPT_CTT 0x0010 /* Catastrophic Trip Point */
  34. #define WPT_TAHV 0x0014 /* Thermal Alert High Value */
  35. #define WPT_TALV 0x0018 /* Thermal Alert Low Value */
  36. #define WPT_TL 0x00000040 /* Throttle Value */
  37. #define WPT_PHL 0x0060 /* PCH Hot Level */
  38. #define WPT_PHLC 0x62 /* PHL Control */
  39. #define WPT_TAS 0x80 /* Thermal Alert Status */
  40. #define WPT_TSPIEN 0x82 /* PCI Interrupt Event Enables */
  41. #define WPT_TSGPEN 0x84 /* General Purpose Event Enables */
  42. /* Wildcat Point-LP PCH Thermal Register bit definitions */
  43. #define WPT_TEMP_TSR 0x00ff /* Temp TS Reading */
  44. #define WPT_TSC_CPDE 0x01 /* Catastrophic Power-Down Enable */
  45. #define WPT_TSS_TSDSS 0x10 /* Thermal Sensor Dynamic Shutdown Status */
  46. #define WPT_TSS_GPES 0x08 /* GPE status */
  47. #define WPT_TSEL_ETS 0x01 /* Enable TS */
  48. #define WPT_TSEL_PLDB 0x80 /* TSEL Policy Lock-Down Bit */
  49. #define WPT_TL_TOL 0x000001FF /* T0 Level */
  50. #define WPT_TL_T1L 0x1ff00000 /* T1 Level */
  51. #define WPT_TL_TTEN 0x20000000 /* TT Enable */
  52. static char driver_name[] = "Intel PCH thermal driver";
  53. struct pch_thermal_device {
  54. void __iomem *hw_base;
  55. const struct pch_dev_ops *ops;
  56. struct pci_dev *pdev;
  57. struct thermal_zone_device *tzd;
  58. int crt_trip_id;
  59. unsigned long crt_temp;
  60. int hot_trip_id;
  61. unsigned long hot_temp;
  62. };
  63. static int pch_wpt_init(struct pch_thermal_device *ptd, int *nr_trips)
  64. {
  65. u8 tsel;
  66. u16 trip_temp;
  67. *nr_trips = 0;
  68. /* Check if BIOS has already enabled thermal sensor */
  69. if (WPT_TSS_TSDSS & readb(ptd->hw_base + WPT_TSS))
  70. goto read_trips;
  71. tsel = readb(ptd->hw_base + WPT_TSEL);
  72. /*
  73. * When TSEL's Policy Lock-Down bit is 1, TSEL become RO.
  74. * If so, thermal sensor cannot enable. Bail out.
  75. */
  76. if (tsel & WPT_TSEL_PLDB) {
  77. dev_err(&ptd->pdev->dev, "Sensor can't be enabled\n");
  78. return -ENODEV;
  79. }
  80. writeb(tsel|WPT_TSEL_ETS, ptd->hw_base + WPT_TSEL);
  81. if (!(WPT_TSS_TSDSS & readb(ptd->hw_base + WPT_TSS))) {
  82. dev_err(&ptd->pdev->dev, "Sensor can't be enabled\n");
  83. return -ENODEV;
  84. }
  85. read_trips:
  86. ptd->crt_trip_id = -1;
  87. trip_temp = readw(ptd->hw_base + WPT_CTT);
  88. trip_temp &= 0x1FF;
  89. if (trip_temp) {
  90. /* Resolution of 1/2 degree C and an offset of -50C */
  91. ptd->crt_temp = trip_temp * 1000 / 2 - 50000;
  92. ptd->crt_trip_id = 0;
  93. ++(*nr_trips);
  94. }
  95. ptd->hot_trip_id = -1;
  96. trip_temp = readw(ptd->hw_base + WPT_PHL);
  97. trip_temp &= 0x1FF;
  98. if (trip_temp) {
  99. /* Resolution of 1/2 degree C and an offset of -50C */
  100. ptd->hot_temp = trip_temp * 1000 / 2 - 50000;
  101. ptd->hot_trip_id = *nr_trips;
  102. ++(*nr_trips);
  103. }
  104. return 0;
  105. }
  106. static int pch_wpt_get_temp(struct pch_thermal_device *ptd, int *temp)
  107. {
  108. u8 wpt_temp;
  109. wpt_temp = WPT_TEMP_TSR & readl(ptd->hw_base + WPT_TEMP);
  110. /* Resolution of 1/2 degree C and an offset of -50C */
  111. *temp = (wpt_temp * 1000 / 2 - 50000);
  112. return 0;
  113. }
  114. struct pch_dev_ops {
  115. int (*hw_init)(struct pch_thermal_device *ptd, int *nr_trips);
  116. int (*get_temp)(struct pch_thermal_device *ptd, int *temp);
  117. };
  118. /* dev ops for Wildcat Point */
  119. static const struct pch_dev_ops pch_dev_ops_wpt = {
  120. .hw_init = pch_wpt_init,
  121. .get_temp = pch_wpt_get_temp,
  122. };
  123. static int pch_thermal_get_temp(struct thermal_zone_device *tzd, int *temp)
  124. {
  125. struct pch_thermal_device *ptd = tzd->devdata;
  126. return ptd->ops->get_temp(ptd, temp);
  127. }
  128. static int pch_get_trip_type(struct thermal_zone_device *tzd, int trip,
  129. enum thermal_trip_type *type)
  130. {
  131. struct pch_thermal_device *ptd = tzd->devdata;
  132. if (ptd->crt_trip_id == trip)
  133. *type = THERMAL_TRIP_CRITICAL;
  134. else if (ptd->hot_trip_id == trip)
  135. *type = THERMAL_TRIP_HOT;
  136. else
  137. return -EINVAL;
  138. return 0;
  139. }
  140. static int pch_get_trip_temp(struct thermal_zone_device *tzd, int trip, int *temp)
  141. {
  142. struct pch_thermal_device *ptd = tzd->devdata;
  143. if (ptd->crt_trip_id == trip)
  144. *temp = ptd->crt_temp;
  145. else if (ptd->hot_trip_id == trip)
  146. *temp = ptd->hot_temp;
  147. else
  148. return -EINVAL;
  149. return 0;
  150. }
  151. static struct thermal_zone_device_ops tzd_ops = {
  152. .get_temp = pch_thermal_get_temp,
  153. .get_trip_type = pch_get_trip_type,
  154. .get_trip_temp = pch_get_trip_temp,
  155. };
  156. static int intel_pch_thermal_probe(struct pci_dev *pdev,
  157. const struct pci_device_id *id)
  158. {
  159. struct pch_thermal_device *ptd;
  160. int err;
  161. int nr_trips;
  162. char *dev_name;
  163. ptd = devm_kzalloc(&pdev->dev, sizeof(*ptd), GFP_KERNEL);
  164. if (!ptd)
  165. return -ENOMEM;
  166. switch (pdev->device) {
  167. case PCH_THERMAL_DID_WPT:
  168. ptd->ops = &pch_dev_ops_wpt;
  169. dev_name = "pch_wildcat_point";
  170. break;
  171. case PCH_THERMAL_DID_SKL:
  172. ptd->ops = &pch_dev_ops_wpt;
  173. dev_name = "pch_skylake";
  174. break;
  175. default:
  176. dev_err(&pdev->dev, "unknown pch thermal device\n");
  177. return -ENODEV;
  178. }
  179. pci_set_drvdata(pdev, ptd);
  180. ptd->pdev = pdev;
  181. err = pci_enable_device(pdev);
  182. if (err) {
  183. dev_err(&pdev->dev, "failed to enable pci device\n");
  184. return err;
  185. }
  186. err = pci_request_regions(pdev, driver_name);
  187. if (err) {
  188. dev_err(&pdev->dev, "failed to request pci region\n");
  189. goto error_disable;
  190. }
  191. ptd->hw_base = pci_ioremap_bar(pdev, 0);
  192. if (!ptd->hw_base) {
  193. err = -ENOMEM;
  194. dev_err(&pdev->dev, "failed to map mem base\n");
  195. goto error_release;
  196. }
  197. err = ptd->ops->hw_init(ptd, &nr_trips);
  198. if (err)
  199. goto error_cleanup;
  200. ptd->tzd = thermal_zone_device_register(dev_name, nr_trips, 0, ptd,
  201. &tzd_ops, NULL, 0, 0);
  202. if (IS_ERR(ptd->tzd)) {
  203. dev_err(&pdev->dev, "Failed to register thermal zone %s\n",
  204. dev_name);
  205. err = PTR_ERR(ptd->tzd);
  206. goto error_cleanup;
  207. }
  208. return 0;
  209. error_cleanup:
  210. iounmap(ptd->hw_base);
  211. error_release:
  212. pci_release_regions(pdev);
  213. error_disable:
  214. pci_disable_device(pdev);
  215. dev_err(&pdev->dev, "pci device failed to probe\n");
  216. return err;
  217. }
  218. static void intel_pch_thermal_remove(struct pci_dev *pdev)
  219. {
  220. struct pch_thermal_device *ptd = pci_get_drvdata(pdev);
  221. thermal_zone_device_unregister(ptd->tzd);
  222. iounmap(ptd->hw_base);
  223. pci_set_drvdata(pdev, NULL);
  224. pci_release_region(pdev, 0);
  225. pci_disable_device(pdev);
  226. }
  227. static struct pci_device_id intel_pch_thermal_id[] = {
  228. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_WPT) },
  229. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_SKL) },
  230. { 0, },
  231. };
  232. MODULE_DEVICE_TABLE(pci, intel_pch_thermal_id);
  233. static struct pci_driver intel_pch_thermal_driver = {
  234. .name = "intel_pch_thermal",
  235. .id_table = intel_pch_thermal_id,
  236. .probe = intel_pch_thermal_probe,
  237. .remove = intel_pch_thermal_remove,
  238. };
  239. module_pci_driver(intel_pch_thermal_driver);
  240. MODULE_LICENSE("GPL v2");
  241. MODULE_DESCRIPTION("Intel PCH Thermal driver");