intel_pch_thermal.c 7.3 KB

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