intel_menlow.c 13 KB

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