intel_pch_thermal.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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,
  106. unsigned long *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, unsigned long *temp);
  117. };
  118. /* dev ops for Wildcat Point */
  119. static 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,
  124. unsigned long *temp)
  125. {
  126. struct pch_thermal_device *ptd = tzd->devdata;
  127. return ptd->ops->get_temp(ptd, temp);
  128. }
  129. static int pch_get_trip_type(struct thermal_zone_device *tzd, int trip,
  130. enum thermal_trip_type *type)
  131. {
  132. struct pch_thermal_device *ptd = tzd->devdata;
  133. if (ptd->crt_trip_id == trip)
  134. *type = THERMAL_TRIP_CRITICAL;
  135. else if (ptd->hot_trip_id == trip)
  136. *type = THERMAL_TRIP_HOT;
  137. else
  138. return -EINVAL;
  139. return 0;
  140. }
  141. static int pch_get_trip_temp(struct thermal_zone_device *tzd, int trip,
  142. unsigned long *temp)
  143. {
  144. struct pch_thermal_device *ptd = tzd->devdata;
  145. if (ptd->crt_trip_id == trip)
  146. *temp = ptd->crt_temp;
  147. else if (ptd->hot_trip_id == trip)
  148. *temp = ptd->hot_temp;
  149. else
  150. return -EINVAL;
  151. return 0;
  152. }
  153. static struct thermal_zone_device_ops tzd_ops = {
  154. .get_temp = pch_thermal_get_temp,
  155. .get_trip_type = pch_get_trip_type,
  156. .get_trip_temp = pch_get_trip_temp,
  157. };
  158. static int intel_pch_thermal_probe(struct pci_dev *pdev,
  159. const struct pci_device_id *id)
  160. {
  161. struct pch_thermal_device *ptd;
  162. int err;
  163. int nr_trips;
  164. char *dev_name;
  165. ptd = devm_kzalloc(&pdev->dev, sizeof(*ptd), GFP_KERNEL);
  166. if (!ptd)
  167. return -ENOMEM;
  168. switch (pdev->device) {
  169. case PCH_THERMAL_DID_WPT:
  170. ptd->ops = &pch_dev_ops_wpt;
  171. dev_name = "pch_wildcat_point";
  172. break;
  173. default:
  174. dev_err(&pdev->dev, "unknown pch thermal device\n");
  175. return -ENODEV;
  176. }
  177. pci_set_drvdata(pdev, ptd);
  178. ptd->pdev = pdev;
  179. err = pci_enable_device(pdev);
  180. if (err) {
  181. dev_err(&pdev->dev, "failed to enable pci device\n");
  182. return err;
  183. }
  184. err = pci_request_regions(pdev, driver_name);
  185. if (err) {
  186. dev_err(&pdev->dev, "failed to request pci region\n");
  187. goto error_disable;
  188. }
  189. ptd->hw_base = pci_ioremap_bar(pdev, 0);
  190. if (!ptd->hw_base) {
  191. err = -ENOMEM;
  192. dev_err(&pdev->dev, "failed to map mem base\n");
  193. goto error_release;
  194. }
  195. err = ptd->ops->hw_init(ptd, &nr_trips);
  196. if (err)
  197. goto error_cleanup;
  198. ptd->tzd = thermal_zone_device_register(dev_name, nr_trips, 0, ptd,
  199. &tzd_ops, NULL, 0, 0);
  200. if (IS_ERR(ptd->tzd)) {
  201. dev_err(&pdev->dev, "Failed to register thermal zone %s\n",
  202. dev_name);
  203. err = PTR_ERR(ptd->tzd);
  204. goto error_cleanup;
  205. }
  206. return 0;
  207. error_cleanup:
  208. iounmap(ptd->hw_base);
  209. error_release:
  210. pci_release_regions(pdev);
  211. error_disable:
  212. pci_disable_device(pdev);
  213. dev_err(&pdev->dev, "pci device failed to probe\n");
  214. return err;
  215. }
  216. static void intel_pch_thermal_remove(struct pci_dev *pdev)
  217. {
  218. struct pch_thermal_device *ptd = pci_get_drvdata(pdev);
  219. thermal_zone_device_unregister(ptd->tzd);
  220. iounmap(ptd->hw_base);
  221. pci_set_drvdata(pdev, NULL);
  222. pci_release_region(pdev, 0);
  223. pci_disable_device(pdev);
  224. }
  225. static struct pci_device_id intel_pch_thermal_id[] = {
  226. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_WPT) },
  227. { 0, },
  228. };
  229. MODULE_DEVICE_TABLE(pci, intel_pch_thermal_id);
  230. static struct pci_driver intel_pch_thermal_driver = {
  231. .name = "intel_pch_thermal",
  232. .id_table = intel_pch_thermal_id,
  233. .probe = intel_pch_thermal_probe,
  234. .remove = intel_pch_thermal_remove,
  235. };
  236. module_pci_driver(intel_pch_thermal_driver);
  237. MODULE_LICENSE("GPL v2");
  238. MODULE_DESCRIPTION("Intel PCH Thermal driver");