intel_menlow.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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. acpi_status status = AE_OK;
  137. acpi_handle dummy;
  138. struct thermal_cooling_device *cdev;
  139. if (!device)
  140. return -EINVAL;
  141. status = acpi_get_handle(device->handle, MEMORY_GET_BANDWIDTH, &dummy);
  142. if (ACPI_FAILURE(status))
  143. goto end;
  144. status = acpi_get_handle(device->handle, MEMORY_SET_BANDWIDTH, &dummy);
  145. if (ACPI_FAILURE(status))
  146. goto end;
  147. cdev = thermal_cooling_device_register("Memory controller", device,
  148. &memory_cooling_ops);
  149. if (IS_ERR(cdev)) {
  150. result = PTR_ERR(cdev);
  151. goto end;
  152. }
  153. device->driver_data = cdev;
  154. result = sysfs_create_link(&device->dev.kobj,
  155. &cdev->device.kobj, "thermal_cooling");
  156. if (result)
  157. goto unregister;
  158. result = sysfs_create_link(&cdev->device.kobj,
  159. &device->dev.kobj, "device");
  160. if (result) {
  161. sysfs_remove_link(&device->dev.kobj, "thermal_cooling");
  162. goto unregister;
  163. }
  164. end:
  165. return result;
  166. unregister:
  167. thermal_cooling_device_unregister(cdev);
  168. return result;
  169. }
  170. static int intel_menlow_memory_remove(struct acpi_device *device, int type)
  171. {
  172. struct thermal_cooling_device *cdev = acpi_driver_data(device);
  173. if (!device || !cdev)
  174. return -EINVAL;
  175. sysfs_remove_link(&device->dev.kobj, "thermal_cooling");
  176. sysfs_remove_link(&cdev->device.kobj, "device");
  177. thermal_cooling_device_unregister(cdev);
  178. return 0;
  179. }
  180. static const struct acpi_device_id intel_menlow_memory_ids[] = {
  181. {"INT0002", 0},
  182. {"", 0},
  183. };
  184. static struct acpi_driver intel_menlow_memory_driver = {
  185. .name = "intel_menlow_thermal_control",
  186. .ids = intel_menlow_memory_ids,
  187. .ops = {
  188. .add = intel_menlow_memory_add,
  189. .remove = intel_menlow_memory_remove,
  190. },
  191. };
  192. /*
  193. * Sensor control on menlow platform
  194. */
  195. #define THERMAL_AUX0 0
  196. #define THERMAL_AUX1 1
  197. #define GET_AUX0 "GAX0"
  198. #define GET_AUX1 "GAX1"
  199. #define SET_AUX0 "SAX0"
  200. #define SET_AUX1 "SAX1"
  201. struct intel_menlow_attribute {
  202. struct device_attribute attr;
  203. struct device *device;
  204. acpi_handle handle;
  205. struct list_head node;
  206. };
  207. static LIST_HEAD(intel_menlow_attr_list);
  208. static DEFINE_MUTEX(intel_menlow_attr_lock);
  209. /*
  210. * sensor_get_auxtrip - get the current auxtrip value from sensor
  211. * @name: Thermalzone name
  212. * @auxtype : AUX0/AUX1
  213. * @buf: syfs buffer
  214. */
  215. static int sensor_get_auxtrip(acpi_handle handle, int index,
  216. unsigned long long *value)
  217. {
  218. acpi_status status;
  219. if ((index != 0 && index != 1) || !value)
  220. return -EINVAL;
  221. status = acpi_evaluate_integer(handle, index ? GET_AUX1 : GET_AUX0,
  222. NULL, value);
  223. if (ACPI_FAILURE(status))
  224. return -EIO;
  225. return 0;
  226. }
  227. /*
  228. * sensor_set_auxtrip - set the new auxtrip value to sensor
  229. * @name: Thermalzone name
  230. * @auxtype : AUX0/AUX1
  231. * @buf: syfs buffer
  232. */
  233. static int sensor_set_auxtrip(acpi_handle handle, int index, int value)
  234. {
  235. acpi_status status;
  236. union acpi_object arg = {
  237. ACPI_TYPE_INTEGER
  238. };
  239. struct acpi_object_list args = {
  240. 1, &arg
  241. };
  242. unsigned long long temp;
  243. if (index != 0 && index != 1)
  244. return -EINVAL;
  245. status = acpi_evaluate_integer(handle, index ? GET_AUX0 : GET_AUX1,
  246. NULL, &temp);
  247. if (ACPI_FAILURE(status))
  248. return -EIO;
  249. if ((index && value < temp) || (!index && value > temp))
  250. return -EINVAL;
  251. arg.integer.value = value;
  252. status = acpi_evaluate_integer(handle, index ? SET_AUX1 : SET_AUX0,
  253. &args, &temp);
  254. if (ACPI_FAILURE(status))
  255. return -EIO;
  256. /* do we need to check the return value of SAX0/SAX1 ? */
  257. return 0;
  258. }
  259. #define to_intel_menlow_attr(_attr) \
  260. container_of(_attr, struct intel_menlow_attribute, attr)
  261. static ssize_t aux0_show(struct device *dev,
  262. struct device_attribute *dev_attr, char *buf)
  263. {
  264. struct intel_menlow_attribute *attr = to_intel_menlow_attr(dev_attr);
  265. unsigned long long value;
  266. int result;
  267. result = sensor_get_auxtrip(attr->handle, 0, &value);
  268. return result ? result : sprintf(buf, "%lu", KELVIN_TO_CELSIUS(value));
  269. }
  270. static ssize_t aux1_show(struct device *dev,
  271. struct device_attribute *dev_attr, char *buf)
  272. {
  273. struct intel_menlow_attribute *attr = to_intel_menlow_attr(dev_attr);
  274. unsigned long long value;
  275. int result;
  276. result = sensor_get_auxtrip(attr->handle, 1, &value);
  277. return result ? result : sprintf(buf, "%lu", KELVIN_TO_CELSIUS(value));
  278. }
  279. static ssize_t aux0_store(struct device *dev,
  280. struct device_attribute *dev_attr,
  281. const char *buf, size_t count)
  282. {
  283. struct intel_menlow_attribute *attr = to_intel_menlow_attr(dev_attr);
  284. int value;
  285. int result;
  286. /*Sanity check; should be a positive integer */
  287. if (!sscanf(buf, "%d", &value))
  288. return -EINVAL;
  289. if (value < 0)
  290. return -EINVAL;
  291. result = sensor_set_auxtrip(attr->handle, 0, CELSIUS_TO_KELVIN(value));
  292. return result ? result : count;
  293. }
  294. static ssize_t aux1_store(struct device *dev,
  295. struct device_attribute *dev_attr,
  296. const char *buf, size_t count)
  297. {
  298. struct intel_menlow_attribute *attr = to_intel_menlow_attr(dev_attr);
  299. int value;
  300. int result;
  301. /*Sanity check; should be a positive integer */
  302. if (!sscanf(buf, "%d", &value))
  303. return -EINVAL;
  304. if (value < 0)
  305. return -EINVAL;
  306. result = sensor_set_auxtrip(attr->handle, 1, CELSIUS_TO_KELVIN(value));
  307. return result ? result : count;
  308. }
  309. /* BIOS can enable/disable the thermal user application in dabney platform */
  310. #define BIOS_ENABLED "\\_TZ.GSTS"
  311. static ssize_t bios_enabled_show(struct device *dev,
  312. struct device_attribute *attr, char *buf)
  313. {
  314. acpi_status status;
  315. unsigned long long bios_enabled;
  316. status = acpi_evaluate_integer(NULL, BIOS_ENABLED, NULL, &bios_enabled);
  317. if (ACPI_FAILURE(status))
  318. return -ENODEV;
  319. return sprintf(buf, "%s\n", bios_enabled ? "enabled" : "disabled");
  320. }
  321. static int intel_menlow_add_one_attribute(char *name, umode_t mode, void *show,
  322. void *store, struct device *dev,
  323. acpi_handle handle)
  324. {
  325. struct intel_menlow_attribute *attr;
  326. int result;
  327. attr = kzalloc(sizeof(struct intel_menlow_attribute), GFP_KERNEL);
  328. if (!attr)
  329. return -ENOMEM;
  330. sysfs_attr_init(&attr->attr.attr); /* That is consistent naming :D */
  331. attr->attr.attr.name = name;
  332. attr->attr.attr.mode = mode;
  333. attr->attr.show = show;
  334. attr->attr.store = store;
  335. attr->device = dev;
  336. attr->handle = handle;
  337. result = device_create_file(dev, &attr->attr);
  338. if (result) {
  339. kfree(attr);
  340. return result;
  341. }
  342. mutex_lock(&intel_menlow_attr_lock);
  343. list_add_tail(&attr->node, &intel_menlow_attr_list);
  344. mutex_unlock(&intel_menlow_attr_lock);
  345. return 0;
  346. }
  347. static acpi_status intel_menlow_register_sensor(acpi_handle handle, u32 lvl,
  348. void *context, void **rv)
  349. {
  350. acpi_status status;
  351. acpi_handle dummy;
  352. struct thermal_zone_device *thermal;
  353. int result;
  354. result = acpi_bus_get_private_data(handle, (void **)&thermal);
  355. if (result)
  356. return 0;
  357. /* _TZ must have the AUX0/1 methods */
  358. status = acpi_get_handle(handle, GET_AUX0, &dummy);
  359. if (ACPI_FAILURE(status))
  360. return (status == AE_NOT_FOUND) ? AE_OK : status;
  361. status = acpi_get_handle(handle, SET_AUX0, &dummy);
  362. if (ACPI_FAILURE(status))
  363. return (status == AE_NOT_FOUND) ? AE_OK : status;
  364. result = intel_menlow_add_one_attribute("aux0", 0644,
  365. aux0_show, aux0_store,
  366. &thermal->device, handle);
  367. if (result)
  368. return AE_ERROR;
  369. status = acpi_get_handle(handle, GET_AUX1, &dummy);
  370. if (ACPI_FAILURE(status))
  371. goto aux1_not_found;
  372. status = acpi_get_handle(handle, SET_AUX1, &dummy);
  373. if (ACPI_FAILURE(status))
  374. goto aux1_not_found;
  375. result = intel_menlow_add_one_attribute("aux1", 0644,
  376. aux1_show, aux1_store,
  377. &thermal->device, handle);
  378. if (result) {
  379. intel_menlow_unregister_sensor();
  380. return AE_ERROR;
  381. }
  382. /*
  383. * create the "dabney_enabled" attribute which means the user app
  384. * should be loaded or not
  385. */
  386. result = intel_menlow_add_one_attribute("bios_enabled", 0444,
  387. bios_enabled_show, NULL,
  388. &thermal->device, handle);
  389. if (result) {
  390. intel_menlow_unregister_sensor();
  391. return AE_ERROR;
  392. }
  393. return AE_OK;
  394. aux1_not_found:
  395. if (status == AE_NOT_FOUND)
  396. return AE_OK;
  397. intel_menlow_unregister_sensor();
  398. return status;
  399. }
  400. static void intel_menlow_unregister_sensor(void)
  401. {
  402. struct intel_menlow_attribute *pos, *next;
  403. mutex_lock(&intel_menlow_attr_lock);
  404. list_for_each_entry_safe(pos, next, &intel_menlow_attr_list, node) {
  405. list_del(&pos->node);
  406. device_remove_file(pos->device, &pos->attr);
  407. kfree(pos);
  408. }
  409. mutex_unlock(&intel_menlow_attr_lock);
  410. return;
  411. }
  412. static int __init intel_menlow_module_init(void)
  413. {
  414. int result = -ENODEV;
  415. acpi_status status;
  416. unsigned long long enable;
  417. if (acpi_disabled)
  418. return result;
  419. /* Looking for the \_TZ.GSTS method */
  420. status = acpi_evaluate_integer(NULL, BIOS_ENABLED, NULL, &enable);
  421. if (ACPI_FAILURE(status) || !enable)
  422. return -ENODEV;
  423. /* Looking for ACPI device MEM0 with hardware id INT0002 */
  424. result = acpi_bus_register_driver(&intel_menlow_memory_driver);
  425. if (result)
  426. return result;
  427. /* Looking for sensors in each ACPI thermal zone */
  428. status = acpi_walk_namespace(ACPI_TYPE_THERMAL, ACPI_ROOT_OBJECT,
  429. ACPI_UINT32_MAX,
  430. intel_menlow_register_sensor, NULL, NULL, NULL);
  431. if (ACPI_FAILURE(status)) {
  432. acpi_bus_unregister_driver(&intel_menlow_memory_driver);
  433. return -ENODEV;
  434. }
  435. return 0;
  436. }
  437. static void __exit intel_menlow_module_exit(void)
  438. {
  439. acpi_bus_unregister_driver(&intel_menlow_memory_driver);
  440. intel_menlow_unregister_sensor();
  441. }
  442. module_init(intel_menlow_module_init);
  443. module_exit(intel_menlow_module_exit);