intel_menlow.c 13 KB

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