int3403_thermal.c 5.7 KB

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