intel_menlow.c 13 KB

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