intel_menlow.c 13 KB

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