intel_pch_thermal.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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/acpi.h>
  23. #include <linux/thermal.h>
  24. #include <linux/pm.h>
  25. /* Intel PCH thermal Device IDs */
  26. #define PCH_THERMAL_DID_HSW_1 0x9C24 /* Haswell PCH */
  27. #define PCH_THERMAL_DID_HSW_2 0x8C24 /* Haswell PCH */
  28. #define PCH_THERMAL_DID_WPT 0x9CA4 /* Wildcat Point */
  29. #define PCH_THERMAL_DID_SKL 0x9D31 /* Skylake PCH */
  30. #define PCH_THERMAL_DID_SKL_H 0xA131 /* Skylake PCH 100 series */
  31. /* Wildcat Point-LP PCH Thermal registers */
  32. #define WPT_TEMP 0x0000 /* Temperature */
  33. #define WPT_TSC 0x04 /* Thermal Sensor Control */
  34. #define WPT_TSS 0x06 /* Thermal Sensor Status */
  35. #define WPT_TSEL 0x08 /* Thermal Sensor Enable and Lock */
  36. #define WPT_TSREL 0x0A /* Thermal Sensor Report Enable and Lock */
  37. #define WPT_TSMIC 0x0C /* Thermal Sensor SMI Control */
  38. #define WPT_CTT 0x0010 /* Catastrophic Trip Point */
  39. #define WPT_TAHV 0x0014 /* Thermal Alert High Value */
  40. #define WPT_TALV 0x0018 /* Thermal Alert Low Value */
  41. #define WPT_TL 0x00000040 /* Throttle Value */
  42. #define WPT_PHL 0x0060 /* PCH Hot Level */
  43. #define WPT_PHLC 0x62 /* PHL Control */
  44. #define WPT_TAS 0x80 /* Thermal Alert Status */
  45. #define WPT_TSPIEN 0x82 /* PCI Interrupt Event Enables */
  46. #define WPT_TSGPEN 0x84 /* General Purpose Event Enables */
  47. /* Wildcat Point-LP PCH Thermal Register bit definitions */
  48. #define WPT_TEMP_TSR 0x01ff /* Temp TS Reading */
  49. #define WPT_TSC_CPDE 0x01 /* Catastrophic Power-Down Enable */
  50. #define WPT_TSS_TSDSS 0x10 /* Thermal Sensor Dynamic Shutdown Status */
  51. #define WPT_TSS_GPES 0x08 /* GPE status */
  52. #define WPT_TSEL_ETS 0x01 /* Enable TS */
  53. #define WPT_TSEL_PLDB 0x80 /* TSEL Policy Lock-Down Bit */
  54. #define WPT_TL_TOL 0x000001FF /* T0 Level */
  55. #define WPT_TL_T1L 0x1ff00000 /* T1 Level */
  56. #define WPT_TL_TTEN 0x20000000 /* TT Enable */
  57. static char driver_name[] = "Intel PCH thermal driver";
  58. struct pch_thermal_device {
  59. void __iomem *hw_base;
  60. const struct pch_dev_ops *ops;
  61. struct pci_dev *pdev;
  62. struct thermal_zone_device *tzd;
  63. int crt_trip_id;
  64. unsigned long crt_temp;
  65. int hot_trip_id;
  66. unsigned long hot_temp;
  67. int psv_trip_id;
  68. unsigned long psv_temp;
  69. bool bios_enabled;
  70. };
  71. #ifdef CONFIG_ACPI
  72. /*
  73. * On some platforms, there is a companion ACPI device, which adds
  74. * passive trip temperature using _PSV method. There is no specific
  75. * passive temperature setting in MMIO interface of this PCI device.
  76. */
  77. static void pch_wpt_add_acpi_psv_trip(struct pch_thermal_device *ptd,
  78. int *nr_trips)
  79. {
  80. struct acpi_device *adev;
  81. ptd->psv_trip_id = -1;
  82. adev = ACPI_COMPANION(&ptd->pdev->dev);
  83. if (adev) {
  84. unsigned long long r;
  85. acpi_status status;
  86. status = acpi_evaluate_integer(adev->handle, "_PSV", NULL,
  87. &r);
  88. if (ACPI_SUCCESS(status)) {
  89. unsigned long trip_temp;
  90. trip_temp = DECI_KELVIN_TO_MILLICELSIUS(r);
  91. if (trip_temp) {
  92. ptd->psv_temp = trip_temp;
  93. ptd->psv_trip_id = *nr_trips;
  94. ++(*nr_trips);
  95. }
  96. }
  97. }
  98. }
  99. #else
  100. static void pch_wpt_add_acpi_psv_trip(struct pch_thermal_device *ptd,
  101. int *nr_trips)
  102. {
  103. ptd->psv_trip_id = -1;
  104. }
  105. #endif
  106. static int pch_wpt_init(struct pch_thermal_device *ptd, int *nr_trips)
  107. {
  108. u8 tsel;
  109. u16 trip_temp;
  110. *nr_trips = 0;
  111. /* Check if BIOS has already enabled thermal sensor */
  112. if (WPT_TSEL_ETS & readb(ptd->hw_base + WPT_TSEL)) {
  113. ptd->bios_enabled = true;
  114. goto read_trips;
  115. }
  116. tsel = readb(ptd->hw_base + WPT_TSEL);
  117. /*
  118. * When TSEL's Policy Lock-Down bit is 1, TSEL become RO.
  119. * If so, thermal sensor cannot enable. Bail out.
  120. */
  121. if (tsel & WPT_TSEL_PLDB) {
  122. dev_err(&ptd->pdev->dev, "Sensor can't be enabled\n");
  123. return -ENODEV;
  124. }
  125. writeb(tsel|WPT_TSEL_ETS, ptd->hw_base + WPT_TSEL);
  126. if (!(WPT_TSEL_ETS & readb(ptd->hw_base + WPT_TSEL))) {
  127. dev_err(&ptd->pdev->dev, "Sensor can't be enabled\n");
  128. return -ENODEV;
  129. }
  130. read_trips:
  131. ptd->crt_trip_id = -1;
  132. trip_temp = readw(ptd->hw_base + WPT_CTT);
  133. trip_temp &= 0x1FF;
  134. if (trip_temp) {
  135. /* Resolution of 1/2 degree C and an offset of -50C */
  136. ptd->crt_temp = trip_temp * 1000 / 2 - 50000;
  137. ptd->crt_trip_id = 0;
  138. ++(*nr_trips);
  139. }
  140. ptd->hot_trip_id = -1;
  141. trip_temp = readw(ptd->hw_base + WPT_PHL);
  142. trip_temp &= 0x1FF;
  143. if (trip_temp) {
  144. /* Resolution of 1/2 degree C and an offset of -50C */
  145. ptd->hot_temp = trip_temp * 1000 / 2 - 50000;
  146. ptd->hot_trip_id = *nr_trips;
  147. ++(*nr_trips);
  148. }
  149. pch_wpt_add_acpi_psv_trip(ptd, nr_trips);
  150. return 0;
  151. }
  152. static int pch_wpt_get_temp(struct pch_thermal_device *ptd, int *temp)
  153. {
  154. u16 wpt_temp;
  155. wpt_temp = WPT_TEMP_TSR & readw(ptd->hw_base + WPT_TEMP);
  156. /* Resolution of 1/2 degree C and an offset of -50C */
  157. *temp = (wpt_temp * 1000 / 2 - 50000);
  158. return 0;
  159. }
  160. static int pch_wpt_suspend(struct pch_thermal_device *ptd)
  161. {
  162. u8 tsel;
  163. if (ptd->bios_enabled)
  164. return 0;
  165. tsel = readb(ptd->hw_base + WPT_TSEL);
  166. writeb(tsel & 0xFE, ptd->hw_base + WPT_TSEL);
  167. return 0;
  168. }
  169. static int pch_wpt_resume(struct pch_thermal_device *ptd)
  170. {
  171. u8 tsel;
  172. if (ptd->bios_enabled)
  173. return 0;
  174. tsel = readb(ptd->hw_base + WPT_TSEL);
  175. writeb(tsel | WPT_TSEL_ETS, ptd->hw_base + WPT_TSEL);
  176. return 0;
  177. }
  178. struct pch_dev_ops {
  179. int (*hw_init)(struct pch_thermal_device *ptd, int *nr_trips);
  180. int (*get_temp)(struct pch_thermal_device *ptd, int *temp);
  181. int (*suspend)(struct pch_thermal_device *ptd);
  182. int (*resume)(struct pch_thermal_device *ptd);
  183. };
  184. /* dev ops for Wildcat Point */
  185. static const struct pch_dev_ops pch_dev_ops_wpt = {
  186. .hw_init = pch_wpt_init,
  187. .get_temp = pch_wpt_get_temp,
  188. .suspend = pch_wpt_suspend,
  189. .resume = pch_wpt_resume,
  190. };
  191. static int pch_thermal_get_temp(struct thermal_zone_device *tzd, int *temp)
  192. {
  193. struct pch_thermal_device *ptd = tzd->devdata;
  194. return ptd->ops->get_temp(ptd, temp);
  195. }
  196. static int pch_get_trip_type(struct thermal_zone_device *tzd, int trip,
  197. enum thermal_trip_type *type)
  198. {
  199. struct pch_thermal_device *ptd = tzd->devdata;
  200. if (ptd->crt_trip_id == trip)
  201. *type = THERMAL_TRIP_CRITICAL;
  202. else if (ptd->hot_trip_id == trip)
  203. *type = THERMAL_TRIP_HOT;
  204. else if (ptd->psv_trip_id == trip)
  205. *type = THERMAL_TRIP_PASSIVE;
  206. else
  207. return -EINVAL;
  208. return 0;
  209. }
  210. static int pch_get_trip_temp(struct thermal_zone_device *tzd, int trip, int *temp)
  211. {
  212. struct pch_thermal_device *ptd = tzd->devdata;
  213. if (ptd->crt_trip_id == trip)
  214. *temp = ptd->crt_temp;
  215. else if (ptd->hot_trip_id == trip)
  216. *temp = ptd->hot_temp;
  217. else if (ptd->psv_trip_id == trip)
  218. *temp = ptd->psv_temp;
  219. else
  220. return -EINVAL;
  221. return 0;
  222. }
  223. static struct thermal_zone_device_ops tzd_ops = {
  224. .get_temp = pch_thermal_get_temp,
  225. .get_trip_type = pch_get_trip_type,
  226. .get_trip_temp = pch_get_trip_temp,
  227. };
  228. enum board_ids {
  229. board_hsw,
  230. board_wpt,
  231. board_skl,
  232. };
  233. static const struct board_info {
  234. const char *name;
  235. const struct pch_dev_ops *ops;
  236. } board_info[] = {
  237. [board_hsw] = {
  238. .name = "pch_haswell",
  239. .ops = &pch_dev_ops_wpt,
  240. },
  241. [board_wpt] = {
  242. .name = "pch_wildcat_point",
  243. .ops = &pch_dev_ops_wpt,
  244. },
  245. [board_skl] = {
  246. .name = "pch_skylake",
  247. .ops = &pch_dev_ops_wpt,
  248. },
  249. };
  250. static int intel_pch_thermal_probe(struct pci_dev *pdev,
  251. const struct pci_device_id *id)
  252. {
  253. enum board_ids board_id = id->driver_data;
  254. const struct board_info *bi = &board_info[board_id];
  255. struct pch_thermal_device *ptd;
  256. int err;
  257. int nr_trips;
  258. ptd = devm_kzalloc(&pdev->dev, sizeof(*ptd), GFP_KERNEL);
  259. if (!ptd)
  260. return -ENOMEM;
  261. ptd->ops = bi->ops;
  262. pci_set_drvdata(pdev, ptd);
  263. ptd->pdev = pdev;
  264. err = pci_enable_device(pdev);
  265. if (err) {
  266. dev_err(&pdev->dev, "failed to enable pci device\n");
  267. return err;
  268. }
  269. err = pci_request_regions(pdev, driver_name);
  270. if (err) {
  271. dev_err(&pdev->dev, "failed to request pci region\n");
  272. goto error_disable;
  273. }
  274. ptd->hw_base = pci_ioremap_bar(pdev, 0);
  275. if (!ptd->hw_base) {
  276. err = -ENOMEM;
  277. dev_err(&pdev->dev, "failed to map mem base\n");
  278. goto error_release;
  279. }
  280. err = ptd->ops->hw_init(ptd, &nr_trips);
  281. if (err)
  282. goto error_cleanup;
  283. ptd->tzd = thermal_zone_device_register(bi->name, nr_trips, 0, ptd,
  284. &tzd_ops, NULL, 0, 0);
  285. if (IS_ERR(ptd->tzd)) {
  286. dev_err(&pdev->dev, "Failed to register thermal zone %s\n",
  287. bi->name);
  288. err = PTR_ERR(ptd->tzd);
  289. goto error_cleanup;
  290. }
  291. return 0;
  292. error_cleanup:
  293. iounmap(ptd->hw_base);
  294. error_release:
  295. pci_release_regions(pdev);
  296. error_disable:
  297. pci_disable_device(pdev);
  298. dev_err(&pdev->dev, "pci device failed to probe\n");
  299. return err;
  300. }
  301. static void intel_pch_thermal_remove(struct pci_dev *pdev)
  302. {
  303. struct pch_thermal_device *ptd = pci_get_drvdata(pdev);
  304. thermal_zone_device_unregister(ptd->tzd);
  305. iounmap(ptd->hw_base);
  306. pci_set_drvdata(pdev, NULL);
  307. pci_release_region(pdev, 0);
  308. pci_disable_device(pdev);
  309. }
  310. static int intel_pch_thermal_suspend(struct device *device)
  311. {
  312. struct pci_dev *pdev = to_pci_dev(device);
  313. struct pch_thermal_device *ptd = pci_get_drvdata(pdev);
  314. return ptd->ops->suspend(ptd);
  315. }
  316. static int intel_pch_thermal_resume(struct device *device)
  317. {
  318. struct pci_dev *pdev = to_pci_dev(device);
  319. struct pch_thermal_device *ptd = pci_get_drvdata(pdev);
  320. return ptd->ops->resume(ptd);
  321. }
  322. static const struct pci_device_id intel_pch_thermal_id[] = {
  323. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_HSW_1),
  324. .driver_data = board_hsw, },
  325. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_HSW_2),
  326. .driver_data = board_hsw, },
  327. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_WPT),
  328. .driver_data = board_wpt, },
  329. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_SKL),
  330. .driver_data = board_skl, },
  331. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_SKL_H),
  332. .driver_data = board_skl, },
  333. { 0, },
  334. };
  335. MODULE_DEVICE_TABLE(pci, intel_pch_thermal_id);
  336. static const struct dev_pm_ops intel_pch_pm_ops = {
  337. .suspend = intel_pch_thermal_suspend,
  338. .resume = intel_pch_thermal_resume,
  339. };
  340. static struct pci_driver intel_pch_thermal_driver = {
  341. .name = "intel_pch_thermal",
  342. .id_table = intel_pch_thermal_id,
  343. .probe = intel_pch_thermal_probe,
  344. .remove = intel_pch_thermal_remove,
  345. .driver.pm = &intel_pch_pm_ops,
  346. };
  347. module_pci_driver(intel_pch_thermal_driver);
  348. MODULE_LICENSE("GPL v2");
  349. MODULE_DESCRIPTION("Intel PCH Thermal driver");