intel_menlow.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. /*
  2. * intel_menlow.c - Intel menlow Driver for thermal management extension
  3. *
  4. * Copyright (C) 2008 Intel Corp
  5. * Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
  6. * Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
  7. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; version 2 of the License.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  21. *
  22. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  23. *
  24. * This driver creates the sys I/F for programming the sensors.
  25. * It also implements the driver for intel menlow memory controller (hardware
  26. * id is INT0002) which makes use of the platform specific ACPI methods
  27. * to get/set bandwidth.
  28. */
  29. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  30. #include <linux/kernel.h>
  31. #include <linux/module.h>
  32. #include <linux/init.h>
  33. #include <linux/slab.h>
  34. #include <linux/types.h>
  35. #include <linux/pci.h>
  36. #include <linux/pm.h>
  37. #include <linux/thermal.h>
  38. #include <acpi/acpi_bus.h>
  39. #include <acpi/acpi_drivers.h>
  40. MODULE_AUTHOR("Thomas Sujith");
  41. MODULE_AUTHOR("Zhang Rui");
  42. MODULE_DESCRIPTION("Intel Menlow platform specific driver");
  43. MODULE_LICENSE("GPL");
  44. /*
  45. * Memory controller device control
  46. */
  47. #define MEMORY_GET_BANDWIDTH "GTHS"
  48. #define MEMORY_SET_BANDWIDTH "STHS"
  49. #define MEMORY_ARG_CUR_BANDWIDTH 1
  50. #define MEMORY_ARG_MAX_BANDWIDTH 0
  51. static void intel_menlow_unregister_sensor(void);
  52. /*
  53. * GTHS returning 'n' would mean that [0,n-1] states are supported
  54. * In that case max_cstate would be n-1
  55. * GTHS returning '0' would mean that no bandwidth control states are supported
  56. */
  57. static int memory_get_max_bandwidth(struct thermal_cooling_device *cdev,
  58. unsigned long *max_state)
  59. {
  60. struct acpi_device *device = cdev->devdata;
  61. acpi_handle handle = device->handle;
  62. unsigned long long value;
  63. struct acpi_object_list arg_list;
  64. union acpi_object arg;
  65. acpi_status status = AE_OK;
  66. arg_list.count = 1;
  67. arg_list.pointer = &arg;
  68. arg.type = ACPI_TYPE_INTEGER;
  69. arg.integer.value = MEMORY_ARG_MAX_BANDWIDTH;
  70. status = acpi_evaluate_integer(handle, MEMORY_GET_BANDWIDTH,
  71. &arg_list, &value);
  72. if (ACPI_FAILURE(status))
  73. return -EFAULT;
  74. if (!value)
  75. return -EINVAL;
  76. *max_state = value - 1;
  77. return 0;
  78. }
  79. static int memory_get_cur_bandwidth(struct thermal_cooling_device *cdev,
  80. unsigned long *value)
  81. {
  82. struct acpi_device *device = cdev->devdata;
  83. acpi_handle handle = device->handle;
  84. unsigned long long result;
  85. struct acpi_object_list arg_list;
  86. union acpi_object arg;
  87. acpi_status status = AE_OK;
  88. arg_list.count = 1;
  89. arg_list.pointer = &arg;
  90. arg.type = ACPI_TYPE_INTEGER;
  91. arg.integer.value = MEMORY_ARG_CUR_BANDWIDTH;
  92. status = acpi_evaluate_integer(handle, MEMORY_GET_BANDWIDTH,
  93. &arg_list, &result);
  94. if (ACPI_FAILURE(status))
  95. return -EFAULT;
  96. *value = result;
  97. return 0;
  98. }
  99. static int memory_set_cur_bandwidth(struct thermal_cooling_device *cdev,
  100. unsigned long state)
  101. {
  102. struct acpi_device *device = cdev->devdata;
  103. acpi_handle handle = device->handle;
  104. struct acpi_object_list arg_list;
  105. union acpi_object arg;
  106. acpi_status status;
  107. unsigned long long temp;
  108. unsigned long max_state;
  109. if (memory_get_max_bandwidth(cdev, &max_state))
  110. return -EFAULT;
  111. if (state > max_state)
  112. return -EINVAL;
  113. arg_list.count = 1;
  114. arg_list.pointer = &arg;
  115. arg.type = ACPI_TYPE_INTEGER;
  116. arg.integer.value = state;
  117. status =
  118. acpi_evaluate_integer(handle, MEMORY_SET_BANDWIDTH, &arg_list,
  119. &temp);
  120. pr_info("Bandwidth value was %ld: status is %d\n", state, status);
  121. if (ACPI_FAILURE(status))
  122. return -EFAULT;
  123. return 0;
  124. }
  125. static struct thermal_cooling_device_ops memory_cooling_ops = {
  126. .get_max_state = memory_get_max_bandwidth,
  127. .get_cur_state = memory_get_cur_bandwidth,
  128. .set_cur_state = memory_set_cur_bandwidth,
  129. };
  130. /*
  131. * Memory Device Management
  132. */
  133. static int intel_menlow_memory_add(struct acpi_device *device)
  134. {
  135. int result = -ENODEV;
  136. struct thermal_cooling_device *cdev;
  137. if (!device)
  138. return -EINVAL;
  139. if (!acpi_has_method(device->handle, MEMORY_GET_BANDWIDTH))
  140. goto end;
  141. if (!acpi_has_method(device->handle, MEMORY_SET_BANDWIDTH))
  142. goto end;
  143. cdev = thermal_cooling_device_register("Memory controller", device,
  144. &memory_cooling_ops);
  145. if (IS_ERR(cdev)) {
  146. result = PTR_ERR(cdev);
  147. goto end;
  148. }
  149. device->driver_data = cdev;
  150. result = sysfs_create_link(&device->dev.kobj,
  151. &cdev->device.kobj, "thermal_cooling");
  152. if (result)
  153. goto unregister;
  154. result = sysfs_create_link(&cdev->device.kobj,
  155. &device->dev.kobj, "device");
  156. if (result) {
  157. sysfs_remove_link(&device->dev.kobj, "thermal_cooling");
  158. goto unregister;
  159. }
  160. end:
  161. return result;
  162. unregister:
  163. thermal_cooling_device_unregister(cdev);
  164. return result;
  165. }
  166. static int intel_menlow_memory_remove(struct acpi_device *device)
  167. {
  168. struct thermal_cooling_device *cdev = acpi_driver_data(device);
  169. if (!device || !cdev)
  170. return -EINVAL;
  171. sysfs_remove_link(&device->dev.kobj, "thermal_cooling");
  172. sysfs_remove_link(&cdev->device.kobj, "device");
  173. thermal_cooling_device_unregister(cdev);
  174. return 0;
  175. }
  176. static const struct acpi_device_id intel_menlow_memory_ids[] = {
  177. {"INT0002", 0},
  178. {"", 0},
  179. };
  180. static struct acpi_driver intel_menlow_memory_driver = {
  181. .name = "intel_menlow_thermal_control",
  182. .ids = intel_menlow_memory_ids,
  183. .ops = {
  184. .add = intel_menlow_memory_add,
  185. .remove = intel_menlow_memory_remove,
  186. },
  187. };
  188. /*
  189. * Sensor control on menlow platform
  190. */
  191. #define THERMAL_AUX0 0
  192. #define THERMAL_AUX1 1
  193. #define GET_AUX0 "GAX0"
  194. #define GET_AUX1 "GAX1"
  195. #define SET_AUX0 "SAX0"
  196. #define SET_AUX1 "SAX1"
  197. struct intel_menlow_attribute {
  198. struct device_attribute attr;
  199. struct device *device;
  200. acpi_handle handle;
  201. struct list_head node;
  202. };
  203. static LIST_HEAD(intel_menlow_attr_list);
  204. static DEFINE_MUTEX(intel_menlow_attr_lock);
  205. /*
  206. * sensor_get_auxtrip - get the current auxtrip value from sensor
  207. * @name: Thermalzone name
  208. * @auxtype : AUX0/AUX1
  209. * @buf: syfs buffer
  210. */
  211. static int sensor_get_auxtrip(acpi_handle handle, int index,
  212. unsigned long long *value)
  213. {
  214. acpi_status status;
  215. if ((index != 0 && index != 1) || !value)
  216. return -EINVAL;
  217. status = acpi_evaluate_integer(handle, index ? GET_AUX1 : GET_AUX0,
  218. NULL, value);
  219. if (ACPI_FAILURE(status))
  220. return -EIO;
  221. return 0;
  222. }
  223. /*
  224. * sensor_set_auxtrip - set the new auxtrip value to sensor
  225. * @name: Thermalzone name
  226. * @auxtype : AUX0/AUX1
  227. * @buf: syfs buffer
  228. */
  229. static int sensor_set_auxtrip(acpi_handle handle, int index, int value)
  230. {
  231. acpi_status status;
  232. union acpi_object arg = {
  233. ACPI_TYPE_INTEGER
  234. };
  235. struct acpi_object_list args = {
  236. 1, &arg
  237. };
  238. unsigned long long temp;
  239. if (index != 0 && index != 1)
  240. return -EINVAL;
  241. status = acpi_evaluate_integer(handle, index ? GET_AUX0 : GET_AUX1,
  242. NULL, &temp);
  243. if (ACPI_FAILURE(status))
  244. return -EIO;
  245. if ((index && value < temp) || (!index && value > temp))
  246. return -EINVAL;
  247. arg.integer.value = value;
  248. status = acpi_evaluate_integer(handle, index ? SET_AUX1 : SET_AUX0,
  249. &args, &temp);
  250. if (ACPI_FAILURE(status))
  251. return -EIO;
  252. /* do we need to check the return value of SAX0/SAX1 ? */
  253. return 0;
  254. }
  255. #define to_intel_menlow_attr(_attr) \
  256. container_of(_attr, struct intel_menlow_attribute, attr)
  257. static ssize_t aux0_show(struct device *dev,
  258. struct device_attribute *dev_attr, char *buf)
  259. {
  260. struct intel_menlow_attribute *attr = to_intel_menlow_attr(dev_attr);
  261. unsigned long long value;
  262. int result;
  263. result = sensor_get_auxtrip(attr->handle, 0, &value);
  264. return result ? result : sprintf(buf, "%lu", KELVIN_TO_CELSIUS(value));
  265. }
  266. static ssize_t aux1_show(struct device *dev,
  267. struct device_attribute *dev_attr, char *buf)
  268. {
  269. struct intel_menlow_attribute *attr = to_intel_menlow_attr(dev_attr);
  270. unsigned long long value;
  271. int result;
  272. result = sensor_get_auxtrip(attr->handle, 1, &value);
  273. return result ? result : sprintf(buf, "%lu", KELVIN_TO_CELSIUS(value));
  274. }
  275. static ssize_t aux0_store(struct device *dev,
  276. struct device_attribute *dev_attr,
  277. const char *buf, size_t count)
  278. {
  279. struct intel_menlow_attribute *attr = to_intel_menlow_attr(dev_attr);
  280. int value;
  281. int result;
  282. /*Sanity check; should be a positive integer */
  283. if (!sscanf(buf, "%d", &value))
  284. return -EINVAL;
  285. if (value < 0)
  286. return -EINVAL;
  287. result = sensor_set_auxtrip(attr->handle, 0, CELSIUS_TO_KELVIN(value));
  288. return result ? result : count;
  289. }
  290. static ssize_t aux1_store(struct device *dev,
  291. struct device_attribute *dev_attr,
  292. const char *buf, size_t count)
  293. {
  294. struct intel_menlow_attribute *attr = to_intel_menlow_attr(dev_attr);
  295. int value;
  296. int result;
  297. /*Sanity check; should be a positive integer */
  298. if (!sscanf(buf, "%d", &value))
  299. return -EINVAL;
  300. if (value < 0)
  301. return -EINVAL;
  302. result = sensor_set_auxtrip(attr->handle, 1, CELSIUS_TO_KELVIN(value));
  303. return result ? result : count;
  304. }
  305. /* BIOS can enable/disable the thermal user application in dabney platform */
  306. #define BIOS_ENABLED "\\_TZ.GSTS"
  307. static ssize_t bios_enabled_show(struct device *dev,
  308. struct device_attribute *attr, char *buf)
  309. {
  310. acpi_status status;
  311. unsigned long long bios_enabled;
  312. status = acpi_evaluate_integer(NULL, BIOS_ENABLED, NULL, &bios_enabled);
  313. if (ACPI_FAILURE(status))
  314. return -ENODEV;
  315. return sprintf(buf, "%s\n", bios_enabled ? "enabled" : "disabled");
  316. }
  317. static int intel_menlow_add_one_attribute(char *name, umode_t mode, void *show,
  318. void *store, struct device *dev,
  319. acpi_handle handle)
  320. {
  321. struct intel_menlow_attribute *attr;
  322. int result;
  323. attr = kzalloc(sizeof(struct intel_menlow_attribute), GFP_KERNEL);
  324. if (!attr)
  325. return -ENOMEM;
  326. sysfs_attr_init(&attr->attr.attr); /* That is consistent naming :D */
  327. attr->attr.attr.name = name;
  328. attr->attr.attr.mode = mode;
  329. attr->attr.show = show;
  330. attr->attr.store = store;
  331. attr->device = dev;
  332. attr->handle = handle;
  333. result = device_create_file(dev, &attr->attr);
  334. if (result) {
  335. kfree(attr);
  336. return result;
  337. }
  338. mutex_lock(&intel_menlow_attr_lock);
  339. list_add_tail(&attr->node, &intel_menlow_attr_list);
  340. mutex_unlock(&intel_menlow_attr_lock);
  341. return 0;
  342. }
  343. static acpi_status intel_menlow_register_sensor(acpi_handle handle, u32 lvl,
  344. void *context, void **rv)
  345. {
  346. acpi_status status;
  347. acpi_handle dummy;
  348. struct thermal_zone_device *thermal;
  349. int result;
  350. result = acpi_bus_get_private_data(handle, (void **)&thermal);
  351. if (result)
  352. return 0;
  353. /* _TZ must have the AUX0/1 methods */
  354. status = acpi_get_handle(handle, GET_AUX0, &dummy);
  355. if (ACPI_FAILURE(status))
  356. return (status == AE_NOT_FOUND) ? AE_OK : status;
  357. status = acpi_get_handle(handle, SET_AUX0, &dummy);
  358. if (ACPI_FAILURE(status))
  359. return (status == AE_NOT_FOUND) ? AE_OK : status;
  360. result = intel_menlow_add_one_attribute("aux0", 0644,
  361. aux0_show, aux0_store,
  362. &thermal->device, handle);
  363. if (result)
  364. return AE_ERROR;
  365. status = acpi_get_handle(handle, GET_AUX1, &dummy);
  366. if (ACPI_FAILURE(status))
  367. goto aux1_not_found;
  368. status = acpi_get_handle(handle, SET_AUX1, &dummy);
  369. if (ACPI_FAILURE(status))
  370. goto aux1_not_found;
  371. result = intel_menlow_add_one_attribute("aux1", 0644,
  372. aux1_show, aux1_store,
  373. &thermal->device, handle);
  374. if (result) {
  375. intel_menlow_unregister_sensor();
  376. return AE_ERROR;
  377. }
  378. /*
  379. * create the "dabney_enabled" attribute which means the user app
  380. * should be loaded or not
  381. */
  382. result = intel_menlow_add_one_attribute("bios_enabled", 0444,
  383. bios_enabled_show, NULL,
  384. &thermal->device, handle);
  385. if (result) {
  386. intel_menlow_unregister_sensor();
  387. return AE_ERROR;
  388. }
  389. return AE_OK;
  390. aux1_not_found:
  391. if (status == AE_NOT_FOUND)
  392. return AE_OK;
  393. intel_menlow_unregister_sensor();
  394. return status;
  395. }
  396. static void intel_menlow_unregister_sensor(void)
  397. {
  398. struct intel_menlow_attribute *pos, *next;
  399. mutex_lock(&intel_menlow_attr_lock);
  400. list_for_each_entry_safe(pos, next, &intel_menlow_attr_list, node) {
  401. list_del(&pos->node);
  402. device_remove_file(pos->device, &pos->attr);
  403. kfree(pos);
  404. }
  405. mutex_unlock(&intel_menlow_attr_lock);
  406. return;
  407. }
  408. static int __init intel_menlow_module_init(void)
  409. {
  410. int result = -ENODEV;
  411. acpi_status status;
  412. unsigned long long enable;
  413. if (acpi_disabled)
  414. return result;
  415. /* Looking for the \_TZ.GSTS method */
  416. status = acpi_evaluate_integer(NULL, BIOS_ENABLED, NULL, &enable);
  417. if (ACPI_FAILURE(status) || !enable)
  418. return -ENODEV;
  419. /* Looking for ACPI device MEM0 with hardware id INT0002 */
  420. result = acpi_bus_register_driver(&intel_menlow_memory_driver);
  421. if (result)
  422. return result;
  423. /* Looking for sensors in each ACPI thermal zone */
  424. status = acpi_walk_namespace(ACPI_TYPE_THERMAL, ACPI_ROOT_OBJECT,
  425. ACPI_UINT32_MAX,
  426. intel_menlow_register_sensor, NULL, NULL, NULL);
  427. if (ACPI_FAILURE(status)) {
  428. acpi_bus_unregister_driver(&intel_menlow_memory_driver);
  429. return -ENODEV;
  430. }
  431. return 0;
  432. }
  433. static void __exit intel_menlow_module_exit(void)
  434. {
  435. acpi_bus_unregister_driver(&intel_menlow_memory_driver);
  436. intel_menlow_unregister_sensor();
  437. }
  438. module_init(intel_menlow_module_init);
  439. module_exit(intel_menlow_module_exit);