int3403_thermal.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. };
  32. static int sys_get_curr_temp(struct thermal_zone_device *tzone,
  33. unsigned long *temp)
  34. {
  35. struct acpi_device *device = tzone->devdata;
  36. unsigned long long tmp;
  37. acpi_status status;
  38. status = acpi_evaluate_integer(device->handle, "_TMP", NULL, &tmp);
  39. if (ACPI_FAILURE(status))
  40. return -EIO;
  41. *temp = DECI_KELVIN_TO_MILLI_CELSIUS(tmp, KELVIN_OFFSET);
  42. return 0;
  43. }
  44. static int sys_get_trip_hyst(struct thermal_zone_device *tzone,
  45. int trip, unsigned long *temp)
  46. {
  47. struct acpi_device *device = tzone->devdata;
  48. unsigned long long hyst;
  49. acpi_status status;
  50. status = acpi_evaluate_integer(device->handle, "GTSH", NULL, &hyst);
  51. if (ACPI_FAILURE(status))
  52. return -EIO;
  53. /*
  54. * Thermal hysteresis represents a temperature difference.
  55. * Kelvin and Celsius have same degree size. So the
  56. * conversion here between tenths of degree Kelvin unit
  57. * and Milli-Celsius unit is just to multiply 100.
  58. */
  59. *temp = hyst * 100;
  60. return 0;
  61. }
  62. static int sys_get_trip_temp(struct thermal_zone_device *tzone,
  63. int trip, unsigned long *temp)
  64. {
  65. struct acpi_device *device = tzone->devdata;
  66. struct int3403_sensor *obj = acpi_driver_data(device);
  67. /*
  68. * get_trip_temp is a mandatory callback but
  69. * PATx method doesn't return any value, so return
  70. * cached value, which was last set from user space.
  71. */
  72. *temp = obj->thresholds[trip];
  73. return 0;
  74. }
  75. static int sys_get_trip_type(struct thermal_zone_device *thermal,
  76. int trip, enum thermal_trip_type *type)
  77. {
  78. /* Mandatory callback, may not mean much here */
  79. *type = THERMAL_TRIP_PASSIVE;
  80. return 0;
  81. }
  82. int sys_set_trip_temp(struct thermal_zone_device *tzone, int trip,
  83. unsigned long temp)
  84. {
  85. struct acpi_device *device = tzone->devdata;
  86. acpi_status status;
  87. char name[10];
  88. int ret = 0;
  89. struct int3403_sensor *obj = acpi_driver_data(device);
  90. snprintf(name, sizeof(name), "PAT%d", trip);
  91. if (acpi_has_method(device->handle, name)) {
  92. status = acpi_execute_simple_method(device->handle, name,
  93. MILLI_CELSIUS_TO_DECI_KELVIN(temp,
  94. KELVIN_OFFSET));
  95. if (ACPI_FAILURE(status))
  96. ret = -EIO;
  97. else
  98. obj->thresholds[trip] = temp;
  99. } else {
  100. ret = -EIO;
  101. dev_err(&device->dev, "sys_set_trip_temp: method not found\n");
  102. }
  103. return ret;
  104. }
  105. static struct thermal_zone_device_ops tzone_ops = {
  106. .get_temp = sys_get_curr_temp,
  107. .get_trip_temp = sys_get_trip_temp,
  108. .get_trip_type = sys_get_trip_type,
  109. .set_trip_temp = sys_set_trip_temp,
  110. .get_trip_hyst = sys_get_trip_hyst,
  111. };
  112. static void acpi_thermal_notify(struct acpi_device *device, u32 event)
  113. {
  114. struct int3403_sensor *obj;
  115. if (!device)
  116. return;
  117. obj = acpi_driver_data(device);
  118. if (!obj)
  119. return;
  120. switch (event) {
  121. case INT3403_PERF_CHANGED_EVENT:
  122. break;
  123. case INT3403_THERMAL_EVENT:
  124. thermal_zone_device_update(obj->tzone);
  125. break;
  126. default:
  127. dev_err(&device->dev, "Unsupported event [0x%x]\n", event);
  128. break;
  129. }
  130. }
  131. static int acpi_int3403_add(struct acpi_device *device)
  132. {
  133. int result = 0;
  134. unsigned long long ptyp;
  135. acpi_status status;
  136. struct int3403_sensor *obj;
  137. unsigned long long trip_cnt;
  138. int trip_mask = 0;
  139. if (!device)
  140. return -EINVAL;
  141. status = acpi_evaluate_integer(device->handle, "PTYP", NULL, &ptyp);
  142. if (ACPI_FAILURE(status))
  143. return -EINVAL;
  144. if (ptyp != INT3403_TYPE_SENSOR)
  145. return -EINVAL;
  146. obj = devm_kzalloc(&device->dev, sizeof(*obj), GFP_KERNEL);
  147. if (!obj)
  148. return -ENOMEM;
  149. device->driver_data = obj;
  150. status = acpi_evaluate_integer(device->handle, "PATC", NULL,
  151. &trip_cnt);
  152. if (ACPI_FAILURE(status))
  153. trip_cnt = 0;
  154. if (trip_cnt) {
  155. /* We have to cache, thresholds can't be readback */
  156. obj->thresholds = devm_kzalloc(&device->dev,
  157. sizeof(*obj->thresholds) * trip_cnt,
  158. GFP_KERNEL);
  159. if (!obj->thresholds)
  160. return -ENOMEM;
  161. trip_mask = BIT(trip_cnt) - 1;
  162. }
  163. obj->tzone = thermal_zone_device_register(acpi_device_bid(device),
  164. trip_cnt, trip_mask, device, &tzone_ops,
  165. NULL, 0, 0);
  166. if (IS_ERR(obj->tzone)) {
  167. result = PTR_ERR(obj->tzone);
  168. return result;
  169. }
  170. strcpy(acpi_device_name(device), "INT3403");
  171. strcpy(acpi_device_class(device), ACPI_INT3403_CLASS);
  172. return 0;
  173. }
  174. static int acpi_int3403_remove(struct acpi_device *device)
  175. {
  176. struct int3403_sensor *obj;
  177. obj = acpi_driver_data(device);
  178. thermal_zone_device_unregister(obj->tzone);
  179. return 0;
  180. }
  181. ACPI_MODULE_NAME("int3403");
  182. static const struct acpi_device_id int3403_device_ids[] = {
  183. {"INT3403", 0},
  184. {"", 0},
  185. };
  186. MODULE_DEVICE_TABLE(acpi, int3403_device_ids);
  187. static struct acpi_driver acpi_int3403_driver = {
  188. .name = "INT3403",
  189. .class = ACPI_INT3403_CLASS,
  190. .ids = int3403_device_ids,
  191. .ops = {
  192. .add = acpi_int3403_add,
  193. .remove = acpi_int3403_remove,
  194. .notify = acpi_thermal_notify,
  195. },
  196. };
  197. module_acpi_driver(acpi_int3403_driver);
  198. MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
  199. MODULE_LICENSE("GPL v2");
  200. MODULE_DESCRIPTION("ACPI INT3403 thermal driver");