intel_pch_thermal.c 11 KB

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