intel_menlow.c 13 KB

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