int3403_thermal.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * ACPI INT3403 thermal driver
  3. * Copyright (c) 2013, Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/types.h>
  18. #include <linux/acpi.h>
  19. #include <linux/thermal.h>
  20. #define INT3403_TYPE_SENSOR 0x03
  21. #define INT3403_PERF_CHANGED_EVENT 0x80
  22. #define INT3403_THERMAL_EVENT 0x90
  23. #define DECI_KELVIN_TO_MILLI_CELSIUS(t, off) (((t) - (off)) * 100)
  24. #define KELVIN_OFFSET 2732
  25. #define MILLI_CELSIUS_TO_DECI_KELVIN(t, off) (((t) / 100) + (off))
  26. #define ACPI_INT3403_CLASS "int3403"
  27. #define ACPI_INT3403_FILE_STATE "state"
  28. struct int3403_sensor {
  29. struct thermal_zone_device *tzone;
  30. unsigned long *thresholds;
  31. unsigned long crit_temp;
  32. int crit_trip_id;
  33. unsigned long psv_temp;
  34. int psv_trip_id;
  35. };
  36. static int sys_get_curr_temp(struct thermal_zone_device *tzone,
  37. unsigned long *temp)
  38. {
  39. struct acpi_device *device = tzone->devdata;
  40. unsigned long long tmp;
  41. acpi_status status;
  42. status = acpi_evaluate_integer(device->handle, "_TMP", NULL, &tmp);
  43. if (ACPI_FAILURE(status))
  44. return -EIO;
  45. *temp = DECI_KELVIN_TO_MILLI_CELSIUS(tmp, KELVIN_OFFSET);
  46. return 0;
  47. }
  48. static int sys_get_trip_hyst(struct thermal_zone_device *tzone,
  49. int trip, unsigned long *temp)
  50. {
  51. struct acpi_device *device = tzone->devdata;
  52. unsigned long long hyst;
  53. acpi_status status;
  54. status = acpi_evaluate_integer(device->handle, "GTSH", NULL, &hyst);
  55. if (ACPI_FAILURE(status))
  56. return -EIO;
  57. /*
  58. * Thermal hysteresis represents a temperature difference.
  59. * Kelvin and Celsius have same degree size. So the
  60. * conversion here between tenths of degree Kelvin unit
  61. * and Milli-Celsius unit is just to multiply 100.
  62. */
  63. *temp = hyst * 100;
  64. return 0;
  65. }
  66. static int sys_get_trip_temp(struct thermal_zone_device *tzone,
  67. int trip, unsigned long *temp)
  68. {
  69. struct acpi_device *device = tzone->devdata;
  70. struct int3403_sensor *obj = acpi_driver_data(device);
  71. if (trip == obj->crit_trip_id)
  72. *temp = obj->crit_temp;
  73. else if (trip == obj->psv_trip_id)
  74. *temp = obj->psv_temp;
  75. else {
  76. /*
  77. * get_trip_temp is a mandatory callback but
  78. * PATx method doesn't return any value, so return
  79. * cached value, which was last set from user space.
  80. */
  81. *temp = obj->thresholds[trip];
  82. }
  83. return 0;
  84. }
  85. static int sys_get_trip_type(struct thermal_zone_device *thermal,
  86. int trip, enum thermal_trip_type *type)
  87. {
  88. struct acpi_device *device = thermal->devdata;
  89. struct int3403_sensor *obj = acpi_driver_data(device);
  90. /* Mandatory callback, may not mean much here */
  91. if (trip == obj->crit_trip_id)
  92. *type = THERMAL_TRIP_CRITICAL;
  93. else
  94. *type = THERMAL_TRIP_PASSIVE;
  95. return 0;
  96. }
  97. int sys_set_trip_temp(struct thermal_zone_device *tzone, int trip,
  98. unsigned long temp)
  99. {
  100. struct acpi_device *device = tzone->devdata;
  101. acpi_status status;
  102. char name[10];
  103. int ret = 0;
  104. struct int3403_sensor *obj = acpi_driver_data(device);
  105. snprintf(name, sizeof(name), "PAT%d", trip);
  106. if (acpi_has_method(device->handle, name)) {
  107. status = acpi_execute_simple_method(device->handle, name,
  108. MILLI_CELSIUS_TO_DECI_KELVIN(temp,
  109. KELVIN_OFFSET));
  110. if (ACPI_FAILURE(status))
  111. ret = -EIO;
  112. else
  113. obj->thresholds[trip] = temp;
  114. } else {
  115. ret = -EIO;
  116. dev_err(&device->dev, "sys_set_trip_temp: method not found\n");
  117. }
  118. return ret;
  119. }
  120. static struct thermal_zone_device_ops tzone_ops = {
  121. .get_temp = sys_get_curr_temp,
  122. .get_trip_temp = sys_get_trip_temp,
  123. .get_trip_type = sys_get_trip_type,
  124. .set_trip_temp = sys_set_trip_temp,
  125. .get_trip_hyst = sys_get_trip_hyst,
  126. };
  127. static void acpi_thermal_notify(struct acpi_device *device, u32 event)
  128. {
  129. struct int3403_sensor *obj;
  130. if (!device)
  131. return;
  132. obj = acpi_driver_data(device);
  133. if (!obj)
  134. return;
  135. switch (event) {
  136. case INT3403_PERF_CHANGED_EVENT:
  137. break;
  138. case INT3403_THERMAL_EVENT:
  139. thermal_zone_device_update(obj->tzone);
  140. break;
  141. default:
  142. dev_err(&device->dev, "Unsupported event [0x%x]\n", event);
  143. break;
  144. }
  145. }
  146. static int sys_get_trip_crt(struct acpi_device *device, unsigned long *temp)
  147. {
  148. unsigned long long crt;
  149. acpi_status status;
  150. status = acpi_evaluate_integer(device->handle, "_CRT", NULL, &crt);
  151. if (ACPI_FAILURE(status))
  152. return -EIO;
  153. *temp = DECI_KELVIN_TO_MILLI_CELSIUS(crt, KELVIN_OFFSET);
  154. return 0;
  155. }
  156. static int sys_get_trip_psv(struct acpi_device *device, unsigned long *temp)
  157. {
  158. unsigned long long psv;
  159. acpi_status status;
  160. status = acpi_evaluate_integer(device->handle, "_PSV", NULL, &psv);
  161. if (ACPI_FAILURE(status))
  162. return -EIO;
  163. *temp = DECI_KELVIN_TO_MILLI_CELSIUS(psv, KELVIN_OFFSET);
  164. return 0;
  165. }
  166. static int acpi_int3403_add(struct acpi_device *device)
  167. {
  168. int result = 0;
  169. unsigned long long ptyp;
  170. acpi_status status;
  171. struct int3403_sensor *obj;
  172. unsigned long long trip_cnt;
  173. int trip_mask = 0;
  174. if (!device)
  175. return -EINVAL;
  176. status = acpi_evaluate_integer(device->handle, "PTYP", NULL, &ptyp);
  177. if (ACPI_FAILURE(status))
  178. return -EINVAL;
  179. if (ptyp != INT3403_TYPE_SENSOR)
  180. return -EINVAL;
  181. obj = devm_kzalloc(&device->dev, sizeof(*obj), GFP_KERNEL);
  182. if (!obj)
  183. return -ENOMEM;
  184. device->driver_data = obj;
  185. status = acpi_evaluate_integer(device->handle, "PATC", NULL,
  186. &trip_cnt);
  187. if (ACPI_FAILURE(status))
  188. trip_cnt = 0;
  189. if (trip_cnt) {
  190. /* We have to cache, thresholds can't be readback */
  191. obj->thresholds = devm_kzalloc(&device->dev,
  192. sizeof(*obj->thresholds) * trip_cnt,
  193. GFP_KERNEL);
  194. if (!obj->thresholds)
  195. return -ENOMEM;
  196. trip_mask = BIT(trip_cnt) - 1;
  197. }
  198. obj->psv_trip_id = -1;
  199. if (!sys_get_trip_psv(device, &obj->psv_temp))
  200. obj->psv_trip_id = trip_cnt++;
  201. obj->crit_trip_id = -1;
  202. if (!sys_get_trip_crt(device, &obj->crit_temp))
  203. obj->crit_trip_id = trip_cnt++;
  204. obj->tzone = thermal_zone_device_register(acpi_device_bid(device),
  205. trip_cnt, trip_mask, device, &tzone_ops,
  206. NULL, 0, 0);
  207. if (IS_ERR(obj->tzone)) {
  208. result = PTR_ERR(obj->tzone);
  209. return result;
  210. }
  211. strcpy(acpi_device_name(device), "INT3403");
  212. strcpy(acpi_device_class(device), ACPI_INT3403_CLASS);
  213. return 0;
  214. }
  215. static int acpi_int3403_remove(struct acpi_device *device)
  216. {
  217. struct int3403_sensor *obj;
  218. obj = acpi_driver_data(device);
  219. thermal_zone_device_unregister(obj->tzone);
  220. return 0;
  221. }
  222. ACPI_MODULE_NAME("int3403");
  223. static const struct acpi_device_id int3403_device_ids[] = {
  224. {"INT3403", 0},
  225. {"", 0},
  226. };
  227. MODULE_DEVICE_TABLE(acpi, int3403_device_ids);
  228. static struct acpi_driver acpi_int3403_driver = {
  229. .name = "INT3403",
  230. .class = ACPI_INT3403_CLASS,
  231. .ids = int3403_device_ids,
  232. .ops = {
  233. .add = acpi_int3403_add,
  234. .remove = acpi_int3403_remove,
  235. .notify = acpi_thermal_notify,
  236. },
  237. };
  238. module_acpi_driver(acpi_int3403_driver);
  239. MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
  240. MODULE_LICENSE("GPL v2");
  241. MODULE_DESCRIPTION("ACPI INT3403 thermal driver");