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/types.h>
  33. #include <linux/pci.h>
  34. #include <linux/pm.h>
  35. #include <linux/thermal.h>
  36. #include <acpi/acpi_bus.h>
  37. #include <acpi/acpi_drivers.h>
  38. MODULE_AUTHOR("Thomas Sujith");
  39. MODULE_AUTHOR("Zhang Rui");
  40. MODULE_DESCRIPTION("Intel Menlow platform specific driver");
  41. MODULE_LICENSE("GPL");
  42. /*
  43. * Memory controller device control
  44. */
  45. #define MEMORY_GET_BANDWIDTH "GTHS"
  46. #define MEMORY_SET_BANDWIDTH "STHS"
  47. #define MEMORY_ARG_CUR_BANDWIDTH 1
  48. #define MEMORY_ARG_MAX_BANDWIDTH 0
  49. static int memory_get_int_max_bandwidth(struct thermal_cooling_device *cdev,
  50. unsigned long *max_state)
  51. {
  52. struct acpi_device *device = cdev->devdata;
  53. acpi_handle handle = device->handle;
  54. unsigned long value;
  55. struct acpi_object_list arg_list;
  56. union acpi_object arg;
  57. acpi_status status = AE_OK;
  58. arg_list.count = 1;
  59. arg_list.pointer = &arg;
  60. arg.type = ACPI_TYPE_INTEGER;
  61. arg.integer.value = MEMORY_ARG_MAX_BANDWIDTH;
  62. status = acpi_evaluate_integer(handle, MEMORY_GET_BANDWIDTH,
  63. &arg_list, &value);
  64. if (ACPI_FAILURE(status))
  65. return -EFAULT;
  66. *max_state = value - 1;
  67. return 0;
  68. }
  69. static int memory_get_max_bandwidth(struct thermal_cooling_device *cdev,
  70. char *buf)
  71. {
  72. unsigned long value;
  73. if (memory_get_int_max_bandwidth(cdev, &value))
  74. return -EINVAL;
  75. return sprintf(buf, "%ld\n", value);
  76. }
  77. static int memory_get_cur_bandwidth(struct thermal_cooling_device *cdev,
  78. char *buf)
  79. {
  80. struct acpi_device *device = cdev->devdata;
  81. acpi_handle handle = device->handle;
  82. unsigned long value;
  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, &value);
  92. if (ACPI_FAILURE(status))
  93. return -EFAULT;
  94. return sprintf(buf, "%ld\n", value);
  95. }
  96. static int memory_set_cur_bandwidth(struct thermal_cooling_device *cdev,
  97. unsigned int state)
  98. {
  99. struct acpi_device *device = cdev->devdata;
  100. acpi_handle handle = device->handle;
  101. struct acpi_object_list arg_list;
  102. union acpi_object arg;
  103. acpi_status status;
  104. int temp;
  105. unsigned long max_state;
  106. if (memory_get_int_max_bandwidth(cdev, &max_state))
  107. return -EFAULT;
  108. if (max_state < 0 || state > max_state)
  109. return -EINVAL;
  110. arg_list.count = 1;
  111. arg_list.pointer = &arg;
  112. arg.type = ACPI_TYPE_INTEGER;
  113. arg.integer.value = state;
  114. status =
  115. acpi_evaluate_integer(handle, MEMORY_SET_BANDWIDTH, &arg_list,
  116. (unsigned long *)&temp);
  117. printk(KERN_INFO
  118. "Bandwidth value was %d: status is %d\n", state, status);
  119. if (ACPI_FAILURE(status))
  120. return -EFAULT;
  121. return 0;
  122. }
  123. static struct thermal_cooling_device_ops memory_cooling_ops = {
  124. .get_max_state = memory_get_max_bandwidth,
  125. .get_cur_state = memory_get_cur_bandwidth,
  126. .set_cur_state = memory_set_cur_bandwidth,
  127. };
  128. /*
  129. * Memory Device Management
  130. */
  131. static int intel_menlow_memory_add(struct acpi_device *device)
  132. {
  133. int result = -ENODEV;
  134. acpi_status status = AE_OK;
  135. acpi_handle dummy;
  136. struct thermal_cooling_device *cdev;
  137. if (!device)
  138. return -EINVAL;
  139. status = acpi_get_handle(device->handle, MEMORY_GET_BANDWIDTH, &dummy);
  140. if (ACPI_FAILURE(status))
  141. goto end;
  142. status = acpi_get_handle(device->handle, MEMORY_SET_BANDWIDTH, &dummy);
  143. if (ACPI_FAILURE(status))
  144. goto end;
  145. cdev = thermal_cooling_device_register("Memory controller", device,
  146. &memory_cooling_ops);
  147. if (IS_ERR(cdev)) {
  148. result = PTR_ERR(cdev);
  149. goto end;
  150. }
  151. if (cdev) {
  152. acpi_driver_data(device) = 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. }
  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, int *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, (unsigned long *)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. int temp;
  242. if (index != 0 && index != 1)
  243. return -EINVAL;
  244. status = acpi_evaluate_integer(handle, index ? GET_AUX0 : GET_AUX1,
  245. NULL, (unsigned long *)&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, (unsigned long *)&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. int 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. int 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 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. attr->attr.attr.name = name;
  330. attr->attr.attr.mode = mode;
  331. attr->attr.show = show;
  332. attr->attr.store = store;
  333. attr->device = dev;
  334. attr->handle = handle;
  335. result = device_create_file(dev, &attr->attr);
  336. if (result)
  337. return result;
  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. goto not_found;
  357. status = acpi_get_handle(handle, SET_AUX0, &dummy);
  358. if (ACPI_FAILURE(status))
  359. goto not_found;
  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 not_found;
  368. status = acpi_get_handle(handle, SET_AUX1, &dummy);
  369. if (ACPI_FAILURE(status))
  370. goto not_found;
  371. result = intel_menlow_add_one_attribute("aux1", 0644,
  372. aux1_show, aux1_store,
  373. &thermal->device, handle);
  374. if (result)
  375. return AE_ERROR;
  376. /*
  377. * create the "dabney_enabled" attribute which means the user app
  378. * should be loaded or not
  379. */
  380. result = intel_menlow_add_one_attribute("bios_enabled", 0444,
  381. bios_enabled_show, NULL,
  382. &thermal->device, handle);
  383. if (result)
  384. return AE_ERROR;
  385. not_found:
  386. if (status == AE_NOT_FOUND)
  387. return AE_OK;
  388. else
  389. return status;
  390. }
  391. static void intel_menlow_unregister_sensor(void)
  392. {
  393. struct intel_menlow_attribute *pos, *next;
  394. mutex_lock(&intel_menlow_attr_lock);
  395. list_for_each_entry_safe(pos, next, &intel_menlow_attr_list, node) {
  396. list_del(&pos->node);
  397. device_remove_file(pos->device, &pos->attr);
  398. kfree(pos);
  399. }
  400. mutex_unlock(&intel_menlow_attr_lock);
  401. return;
  402. }
  403. static int __init intel_menlow_module_init(void)
  404. {
  405. int result = -ENODEV;
  406. acpi_status status;
  407. unsigned long enable;
  408. if (acpi_disabled)
  409. return result;
  410. /* Looking for the \_TZ.GSTS method */
  411. status = acpi_evaluate_integer(NULL, BIOS_ENABLED, NULL, &enable);
  412. if (ACPI_FAILURE(status) || !enable)
  413. return -ENODEV;
  414. /* Looking for ACPI device MEM0 with hardware id INT0002 */
  415. result = acpi_bus_register_driver(&intel_menlow_memory_driver);
  416. if (result)
  417. return result;
  418. /* Looking for sensors in each ACPI thermal zone */
  419. status = acpi_walk_namespace(ACPI_TYPE_THERMAL, ACPI_ROOT_OBJECT,
  420. ACPI_UINT32_MAX,
  421. intel_menlow_register_sensor, NULL, NULL);
  422. if (ACPI_FAILURE(status))
  423. return -ENODEV;
  424. return 0;
  425. }
  426. static void __exit intel_menlow_module_exit(void)
  427. {
  428. acpi_bus_unregister_driver(&intel_menlow_memory_driver);
  429. intel_menlow_unregister_sensor();
  430. }
  431. module_init(intel_menlow_module_init);
  432. module_exit(intel_menlow_module_exit);