asus_atk0110.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469
  1. /*
  2. * Copyright (C) 2007-2009 Luca Tettamanti <kronos.it@gmail.com>
  3. *
  4. * This file is released under the GPLv2
  5. * See COPYING in the top level directory of the kernel tree.
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/debugfs.h>
  9. #include <linux/kernel.h>
  10. #include <linux/hwmon.h>
  11. #include <linux/list.h>
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <linux/dmi.h>
  15. #include <linux/jiffies.h>
  16. #include <linux/err.h>
  17. #include <linux/acpi.h>
  18. #define ATK_HID "ATK0110"
  19. static bool new_if;
  20. module_param(new_if, bool, 0);
  21. MODULE_PARM_DESC(new_if, "Override detection heuristic and force the use of the new ATK0110 interface");
  22. static const struct dmi_system_id __initconst atk_force_new_if[] = {
  23. {
  24. /* Old interface has broken MCH temp monitoring */
  25. .ident = "Asus Sabertooth X58",
  26. .matches = {
  27. DMI_MATCH(DMI_BOARD_NAME, "SABERTOOTH X58")
  28. }
  29. }, {
  30. /* Old interface reads the same sensor for fan0 and fan1 */
  31. .ident = "Asus M5A78L",
  32. .matches = {
  33. DMI_MATCH(DMI_BOARD_NAME, "M5A78L")
  34. }
  35. },
  36. { }
  37. };
  38. /*
  39. * Minimum time between readings, enforced in order to avoid
  40. * hogging the CPU.
  41. */
  42. #define CACHE_TIME HZ
  43. #define BOARD_ID "MBIF"
  44. #define METHOD_ENUMERATE "GGRP"
  45. #define METHOD_READ "GITM"
  46. #define METHOD_WRITE "SITM"
  47. #define METHOD_OLD_READ_TMP "RTMP"
  48. #define METHOD_OLD_READ_VLT "RVLT"
  49. #define METHOD_OLD_READ_FAN "RFAN"
  50. #define METHOD_OLD_ENUM_TMP "TSIF"
  51. #define METHOD_OLD_ENUM_VLT "VSIF"
  52. #define METHOD_OLD_ENUM_FAN "FSIF"
  53. #define ATK_MUX_HWMON 0x00000006ULL
  54. #define ATK_MUX_MGMT 0x00000011ULL
  55. #define ATK_CLASS_MASK 0xff000000ULL
  56. #define ATK_CLASS_FREQ_CTL 0x03000000ULL
  57. #define ATK_CLASS_FAN_CTL 0x04000000ULL
  58. #define ATK_CLASS_HWMON 0x06000000ULL
  59. #define ATK_CLASS_MGMT 0x11000000ULL
  60. #define ATK_TYPE_MASK 0x00ff0000ULL
  61. #define HWMON_TYPE_VOLT 0x00020000ULL
  62. #define HWMON_TYPE_TEMP 0x00030000ULL
  63. #define HWMON_TYPE_FAN 0x00040000ULL
  64. #define ATK_ELEMENT_ID_MASK 0x0000ffffULL
  65. #define ATK_EC_ID 0x11060004ULL
  66. enum atk_pack_member {
  67. HWMON_PACK_FLAGS,
  68. HWMON_PACK_NAME,
  69. HWMON_PACK_LIMIT1,
  70. HWMON_PACK_LIMIT2,
  71. HWMON_PACK_ENABLE
  72. };
  73. /* New package format */
  74. #define _HWMON_NEW_PACK_SIZE 7
  75. #define _HWMON_NEW_PACK_FLAGS 0
  76. #define _HWMON_NEW_PACK_NAME 1
  77. #define _HWMON_NEW_PACK_UNK1 2
  78. #define _HWMON_NEW_PACK_UNK2 3
  79. #define _HWMON_NEW_PACK_LIMIT1 4
  80. #define _HWMON_NEW_PACK_LIMIT2 5
  81. #define _HWMON_NEW_PACK_ENABLE 6
  82. /* Old package format */
  83. #define _HWMON_OLD_PACK_SIZE 5
  84. #define _HWMON_OLD_PACK_FLAGS 0
  85. #define _HWMON_OLD_PACK_NAME 1
  86. #define _HWMON_OLD_PACK_LIMIT1 2
  87. #define _HWMON_OLD_PACK_LIMIT2 3
  88. #define _HWMON_OLD_PACK_ENABLE 4
  89. struct atk_data {
  90. struct device *hwmon_dev;
  91. acpi_handle atk_handle;
  92. struct acpi_device *acpi_dev;
  93. bool old_interface;
  94. /* old interface */
  95. acpi_handle rtmp_handle;
  96. acpi_handle rvlt_handle;
  97. acpi_handle rfan_handle;
  98. /* new interface */
  99. acpi_handle enumerate_handle;
  100. acpi_handle read_handle;
  101. acpi_handle write_handle;
  102. bool disable_ec;
  103. int voltage_count;
  104. int temperature_count;
  105. int fan_count;
  106. struct list_head sensor_list;
  107. struct {
  108. struct dentry *root;
  109. u32 id;
  110. } debugfs;
  111. };
  112. typedef ssize_t (*sysfs_show_func)(struct device *dev,
  113. struct device_attribute *attr, char *buf);
  114. static const struct acpi_device_id atk_ids[] = {
  115. {ATK_HID, 0},
  116. {"", 0},
  117. };
  118. MODULE_DEVICE_TABLE(acpi, atk_ids);
  119. #define ATTR_NAME_SIZE 16 /* Worst case is "tempN_input" */
  120. struct atk_sensor_data {
  121. struct list_head list;
  122. struct atk_data *data;
  123. struct device_attribute label_attr;
  124. struct device_attribute input_attr;
  125. struct device_attribute limit1_attr;
  126. struct device_attribute limit2_attr;
  127. char label_attr_name[ATTR_NAME_SIZE];
  128. char input_attr_name[ATTR_NAME_SIZE];
  129. char limit1_attr_name[ATTR_NAME_SIZE];
  130. char limit2_attr_name[ATTR_NAME_SIZE];
  131. u64 id;
  132. u64 type;
  133. u64 limit1;
  134. u64 limit2;
  135. u64 cached_value;
  136. unsigned long last_updated; /* in jiffies */
  137. bool is_valid;
  138. char const *acpi_name;
  139. };
  140. /*
  141. * Return buffer format:
  142. * [0-3] "value" is valid flag
  143. * [4-7] value
  144. * [8- ] unknown stuff on newer mobos
  145. */
  146. struct atk_acpi_ret_buffer {
  147. u32 flags;
  148. u32 value;
  149. u8 data[];
  150. };
  151. /* Input buffer used for GITM and SITM methods */
  152. struct atk_acpi_input_buf {
  153. u32 id;
  154. u32 param1;
  155. u32 param2;
  156. };
  157. static int atk_add(struct acpi_device *device);
  158. static int atk_remove(struct acpi_device *device);
  159. static void atk_print_sensor(struct atk_data *data, union acpi_object *obj);
  160. static int atk_read_value(struct atk_sensor_data *sensor, u64 *value);
  161. static void atk_free_sensors(struct atk_data *data);
  162. static struct acpi_driver atk_driver = {
  163. .name = ATK_HID,
  164. .class = "hwmon",
  165. .ids = atk_ids,
  166. .ops = {
  167. .add = atk_add,
  168. .remove = atk_remove,
  169. },
  170. };
  171. #define input_to_atk_sensor(attr) \
  172. container_of(attr, struct atk_sensor_data, input_attr)
  173. #define label_to_atk_sensor(attr) \
  174. container_of(attr, struct atk_sensor_data, label_attr)
  175. #define limit1_to_atk_sensor(attr) \
  176. container_of(attr, struct atk_sensor_data, limit1_attr)
  177. #define limit2_to_atk_sensor(attr) \
  178. container_of(attr, struct atk_sensor_data, limit2_attr)
  179. static ssize_t atk_input_show(struct device *dev,
  180. struct device_attribute *attr, char *buf)
  181. {
  182. struct atk_sensor_data *s = input_to_atk_sensor(attr);
  183. u64 value;
  184. int err;
  185. err = atk_read_value(s, &value);
  186. if (err)
  187. return err;
  188. if (s->type == HWMON_TYPE_TEMP)
  189. /* ACPI returns decidegree */
  190. value *= 100;
  191. return sprintf(buf, "%llu\n", value);
  192. }
  193. static ssize_t atk_label_show(struct device *dev,
  194. struct device_attribute *attr, char *buf)
  195. {
  196. struct atk_sensor_data *s = label_to_atk_sensor(attr);
  197. return sprintf(buf, "%s\n", s->acpi_name);
  198. }
  199. static ssize_t atk_limit1_show(struct device *dev,
  200. struct device_attribute *attr, char *buf)
  201. {
  202. struct atk_sensor_data *s = limit1_to_atk_sensor(attr);
  203. u64 value = s->limit1;
  204. if (s->type == HWMON_TYPE_TEMP)
  205. value *= 100;
  206. return sprintf(buf, "%lld\n", value);
  207. }
  208. static ssize_t atk_limit2_show(struct device *dev,
  209. struct device_attribute *attr, char *buf)
  210. {
  211. struct atk_sensor_data *s = limit2_to_atk_sensor(attr);
  212. u64 value = s->limit2;
  213. if (s->type == HWMON_TYPE_TEMP)
  214. value *= 100;
  215. return sprintf(buf, "%lld\n", value);
  216. }
  217. static ssize_t atk_name_show(struct device *dev,
  218. struct device_attribute *attr, char *buf)
  219. {
  220. return sprintf(buf, "atk0110\n");
  221. }
  222. static struct device_attribute atk_name_attr =
  223. __ATTR(name, 0444, atk_name_show, NULL);
  224. static void atk_init_attribute(struct device_attribute *attr, char *name,
  225. sysfs_show_func show)
  226. {
  227. sysfs_attr_init(&attr->attr);
  228. attr->attr.name = name;
  229. attr->attr.mode = 0444;
  230. attr->show = show;
  231. attr->store = NULL;
  232. }
  233. static union acpi_object *atk_get_pack_member(struct atk_data *data,
  234. union acpi_object *pack,
  235. enum atk_pack_member m)
  236. {
  237. bool old_if = data->old_interface;
  238. int offset;
  239. switch (m) {
  240. case HWMON_PACK_FLAGS:
  241. offset = old_if ? _HWMON_OLD_PACK_FLAGS : _HWMON_NEW_PACK_FLAGS;
  242. break;
  243. case HWMON_PACK_NAME:
  244. offset = old_if ? _HWMON_OLD_PACK_NAME : _HWMON_NEW_PACK_NAME;
  245. break;
  246. case HWMON_PACK_LIMIT1:
  247. offset = old_if ? _HWMON_OLD_PACK_LIMIT1 :
  248. _HWMON_NEW_PACK_LIMIT1;
  249. break;
  250. case HWMON_PACK_LIMIT2:
  251. offset = old_if ? _HWMON_OLD_PACK_LIMIT2 :
  252. _HWMON_NEW_PACK_LIMIT2;
  253. break;
  254. case HWMON_PACK_ENABLE:
  255. offset = old_if ? _HWMON_OLD_PACK_ENABLE :
  256. _HWMON_NEW_PACK_ENABLE;
  257. break;
  258. default:
  259. return NULL;
  260. }
  261. return &pack->package.elements[offset];
  262. }
  263. /*
  264. * New package format is:
  265. * - flag (int)
  266. * class - used for de-muxing the request to the correct GITn
  267. * type (volt, temp, fan)
  268. * sensor id |
  269. * sensor id - used for de-muxing the request _inside_ the GITn
  270. * - name (str)
  271. * - unknown (int)
  272. * - unknown (int)
  273. * - limit1 (int)
  274. * - limit2 (int)
  275. * - enable (int)
  276. *
  277. * The old package has the same format but it's missing the two unknown fields.
  278. */
  279. static int validate_hwmon_pack(struct atk_data *data, union acpi_object *obj)
  280. {
  281. struct device *dev = &data->acpi_dev->dev;
  282. union acpi_object *tmp;
  283. bool old_if = data->old_interface;
  284. int const expected_size = old_if ? _HWMON_OLD_PACK_SIZE :
  285. _HWMON_NEW_PACK_SIZE;
  286. if (obj->type != ACPI_TYPE_PACKAGE) {
  287. dev_warn(dev, "Invalid type: %d\n", obj->type);
  288. return -EINVAL;
  289. }
  290. if (obj->package.count != expected_size) {
  291. dev_warn(dev, "Invalid package size: %d, expected: %d\n",
  292. obj->package.count, expected_size);
  293. return -EINVAL;
  294. }
  295. tmp = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS);
  296. if (tmp->type != ACPI_TYPE_INTEGER) {
  297. dev_warn(dev, "Invalid type (flag): %d\n", tmp->type);
  298. return -EINVAL;
  299. }
  300. tmp = atk_get_pack_member(data, obj, HWMON_PACK_NAME);
  301. if (tmp->type != ACPI_TYPE_STRING) {
  302. dev_warn(dev, "Invalid type (name): %d\n", tmp->type);
  303. return -EINVAL;
  304. }
  305. /* Don't check... we don't know what they're useful for anyway */
  306. #if 0
  307. tmp = &obj->package.elements[HWMON_PACK_UNK1];
  308. if (tmp->type != ACPI_TYPE_INTEGER) {
  309. dev_warn(dev, "Invalid type (unk1): %d\n", tmp->type);
  310. return -EINVAL;
  311. }
  312. tmp = &obj->package.elements[HWMON_PACK_UNK2];
  313. if (tmp->type != ACPI_TYPE_INTEGER) {
  314. dev_warn(dev, "Invalid type (unk2): %d\n", tmp->type);
  315. return -EINVAL;
  316. }
  317. #endif
  318. tmp = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT1);
  319. if (tmp->type != ACPI_TYPE_INTEGER) {
  320. dev_warn(dev, "Invalid type (limit1): %d\n", tmp->type);
  321. return -EINVAL;
  322. }
  323. tmp = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT2);
  324. if (tmp->type != ACPI_TYPE_INTEGER) {
  325. dev_warn(dev, "Invalid type (limit2): %d\n", tmp->type);
  326. return -EINVAL;
  327. }
  328. tmp = atk_get_pack_member(data, obj, HWMON_PACK_ENABLE);
  329. if (tmp->type != ACPI_TYPE_INTEGER) {
  330. dev_warn(dev, "Invalid type (enable): %d\n", tmp->type);
  331. return -EINVAL;
  332. }
  333. atk_print_sensor(data, obj);
  334. return 0;
  335. }
  336. #ifdef DEBUG
  337. static char const *atk_sensor_type(union acpi_object *flags)
  338. {
  339. u64 type = flags->integer.value & ATK_TYPE_MASK;
  340. char const *what;
  341. switch (type) {
  342. case HWMON_TYPE_VOLT:
  343. what = "voltage";
  344. break;
  345. case HWMON_TYPE_TEMP:
  346. what = "temperature";
  347. break;
  348. case HWMON_TYPE_FAN:
  349. what = "fan";
  350. break;
  351. default:
  352. what = "unknown";
  353. break;
  354. }
  355. return what;
  356. }
  357. #endif
  358. static void atk_print_sensor(struct atk_data *data, union acpi_object *obj)
  359. {
  360. #ifdef DEBUG
  361. struct device *dev = &data->acpi_dev->dev;
  362. union acpi_object *flags;
  363. union acpi_object *name;
  364. union acpi_object *limit1;
  365. union acpi_object *limit2;
  366. union acpi_object *enable;
  367. char const *what;
  368. flags = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS);
  369. name = atk_get_pack_member(data, obj, HWMON_PACK_NAME);
  370. limit1 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT1);
  371. limit2 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT2);
  372. enable = atk_get_pack_member(data, obj, HWMON_PACK_ENABLE);
  373. what = atk_sensor_type(flags);
  374. dev_dbg(dev, "%s: %#llx %s [%llu-%llu] %s\n", what,
  375. flags->integer.value,
  376. name->string.pointer,
  377. limit1->integer.value, limit2->integer.value,
  378. enable->integer.value ? "enabled" : "disabled");
  379. #endif
  380. }
  381. static int atk_read_value_old(struct atk_sensor_data *sensor, u64 *value)
  382. {
  383. struct atk_data *data = sensor->data;
  384. struct device *dev = &data->acpi_dev->dev;
  385. struct acpi_object_list params;
  386. union acpi_object id;
  387. acpi_status status;
  388. acpi_handle method;
  389. switch (sensor->type) {
  390. case HWMON_TYPE_VOLT:
  391. method = data->rvlt_handle;
  392. break;
  393. case HWMON_TYPE_TEMP:
  394. method = data->rtmp_handle;
  395. break;
  396. case HWMON_TYPE_FAN:
  397. method = data->rfan_handle;
  398. break;
  399. default:
  400. return -EINVAL;
  401. }
  402. id.type = ACPI_TYPE_INTEGER;
  403. id.integer.value = sensor->id;
  404. params.count = 1;
  405. params.pointer = &id;
  406. status = acpi_evaluate_integer(method, NULL, &params, value);
  407. if (status != AE_OK) {
  408. dev_warn(dev, "%s: ACPI exception: %s\n", __func__,
  409. acpi_format_exception(status));
  410. return -EIO;
  411. }
  412. return 0;
  413. }
  414. static union acpi_object *atk_ggrp(struct atk_data *data, u16 mux)
  415. {
  416. struct device *dev = &data->acpi_dev->dev;
  417. struct acpi_buffer buf;
  418. acpi_status ret;
  419. struct acpi_object_list params;
  420. union acpi_object id;
  421. union acpi_object *pack;
  422. id.type = ACPI_TYPE_INTEGER;
  423. id.integer.value = mux;
  424. params.count = 1;
  425. params.pointer = &id;
  426. buf.length = ACPI_ALLOCATE_BUFFER;
  427. ret = acpi_evaluate_object(data->enumerate_handle, NULL, &params, &buf);
  428. if (ret != AE_OK) {
  429. dev_err(dev, "GGRP[%#x] ACPI exception: %s\n", mux,
  430. acpi_format_exception(ret));
  431. return ERR_PTR(-EIO);
  432. }
  433. pack = buf.pointer;
  434. if (pack->type != ACPI_TYPE_PACKAGE) {
  435. /* Execution was successful, but the id was not found */
  436. ACPI_FREE(pack);
  437. return ERR_PTR(-ENOENT);
  438. }
  439. if (pack->package.count < 1) {
  440. dev_err(dev, "GGRP[%#x] package is too small\n", mux);
  441. ACPI_FREE(pack);
  442. return ERR_PTR(-EIO);
  443. }
  444. return pack;
  445. }
  446. static union acpi_object *atk_gitm(struct atk_data *data, u64 id)
  447. {
  448. struct device *dev = &data->acpi_dev->dev;
  449. struct atk_acpi_input_buf buf;
  450. union acpi_object tmp;
  451. struct acpi_object_list params;
  452. struct acpi_buffer ret;
  453. union acpi_object *obj;
  454. acpi_status status;
  455. buf.id = id;
  456. buf.param1 = 0;
  457. buf.param2 = 0;
  458. tmp.type = ACPI_TYPE_BUFFER;
  459. tmp.buffer.pointer = (u8 *)&buf;
  460. tmp.buffer.length = sizeof(buf);
  461. params.count = 1;
  462. params.pointer = (void *)&tmp;
  463. ret.length = ACPI_ALLOCATE_BUFFER;
  464. status = acpi_evaluate_object_typed(data->read_handle, NULL, &params,
  465. &ret, ACPI_TYPE_BUFFER);
  466. if (status != AE_OK) {
  467. dev_warn(dev, "GITM[%#llx] ACPI exception: %s\n", id,
  468. acpi_format_exception(status));
  469. return ERR_PTR(-EIO);
  470. }
  471. obj = ret.pointer;
  472. /* Sanity check */
  473. if (obj->buffer.length < 8) {
  474. dev_warn(dev, "Unexpected ASBF length: %u\n",
  475. obj->buffer.length);
  476. ACPI_FREE(obj);
  477. return ERR_PTR(-EIO);
  478. }
  479. return obj;
  480. }
  481. static union acpi_object *atk_sitm(struct atk_data *data,
  482. struct atk_acpi_input_buf *buf)
  483. {
  484. struct device *dev = &data->acpi_dev->dev;
  485. struct acpi_object_list params;
  486. union acpi_object tmp;
  487. struct acpi_buffer ret;
  488. union acpi_object *obj;
  489. acpi_status status;
  490. tmp.type = ACPI_TYPE_BUFFER;
  491. tmp.buffer.pointer = (u8 *)buf;
  492. tmp.buffer.length = sizeof(*buf);
  493. params.count = 1;
  494. params.pointer = &tmp;
  495. ret.length = ACPI_ALLOCATE_BUFFER;
  496. status = acpi_evaluate_object_typed(data->write_handle, NULL, &params,
  497. &ret, ACPI_TYPE_BUFFER);
  498. if (status != AE_OK) {
  499. dev_warn(dev, "SITM[%#x] ACPI exception: %s\n", buf->id,
  500. acpi_format_exception(status));
  501. return ERR_PTR(-EIO);
  502. }
  503. obj = ret.pointer;
  504. /* Sanity check */
  505. if (obj->buffer.length < 8) {
  506. dev_warn(dev, "Unexpected ASBF length: %u\n",
  507. obj->buffer.length);
  508. ACPI_FREE(obj);
  509. return ERR_PTR(-EIO);
  510. }
  511. return obj;
  512. }
  513. static int atk_read_value_new(struct atk_sensor_data *sensor, u64 *value)
  514. {
  515. struct atk_data *data = sensor->data;
  516. struct device *dev = &data->acpi_dev->dev;
  517. union acpi_object *obj;
  518. struct atk_acpi_ret_buffer *buf;
  519. int err = 0;
  520. obj = atk_gitm(data, sensor->id);
  521. if (IS_ERR(obj))
  522. return PTR_ERR(obj);
  523. buf = (struct atk_acpi_ret_buffer *)obj->buffer.pointer;
  524. if (buf->flags == 0) {
  525. /*
  526. * The reading is not valid, possible causes:
  527. * - sensor failure
  528. * - enumeration was FUBAR (and we didn't notice)
  529. */
  530. dev_warn(dev, "Read failed, sensor = %#llx\n", sensor->id);
  531. err = -EIO;
  532. goto out;
  533. }
  534. *value = buf->value;
  535. out:
  536. ACPI_FREE(obj);
  537. return err;
  538. }
  539. static int atk_read_value(struct atk_sensor_data *sensor, u64 *value)
  540. {
  541. int err;
  542. if (!sensor->is_valid ||
  543. time_after(jiffies, sensor->last_updated + CACHE_TIME)) {
  544. if (sensor->data->old_interface)
  545. err = atk_read_value_old(sensor, value);
  546. else
  547. err = atk_read_value_new(sensor, value);
  548. if (err)
  549. return err;
  550. sensor->is_valid = true;
  551. sensor->last_updated = jiffies;
  552. sensor->cached_value = *value;
  553. } else {
  554. *value = sensor->cached_value;
  555. err = 0;
  556. }
  557. return err;
  558. }
  559. #ifdef CONFIG_DEBUG_FS
  560. static int atk_debugfs_gitm_get(void *p, u64 *val)
  561. {
  562. struct atk_data *data = p;
  563. union acpi_object *ret;
  564. struct atk_acpi_ret_buffer *buf;
  565. int err = 0;
  566. if (!data->read_handle)
  567. return -ENODEV;
  568. if (!data->debugfs.id)
  569. return -EINVAL;
  570. ret = atk_gitm(data, data->debugfs.id);
  571. if (IS_ERR(ret))
  572. return PTR_ERR(ret);
  573. buf = (struct atk_acpi_ret_buffer *)ret->buffer.pointer;
  574. if (buf->flags)
  575. *val = buf->value;
  576. else
  577. err = -EIO;
  578. ACPI_FREE(ret);
  579. return err;
  580. }
  581. DEFINE_SIMPLE_ATTRIBUTE(atk_debugfs_gitm,
  582. atk_debugfs_gitm_get,
  583. NULL,
  584. "0x%08llx\n");
  585. static int atk_acpi_print(char *buf, size_t sz, union acpi_object *obj)
  586. {
  587. int ret = 0;
  588. switch (obj->type) {
  589. case ACPI_TYPE_INTEGER:
  590. ret = snprintf(buf, sz, "0x%08llx\n", obj->integer.value);
  591. break;
  592. case ACPI_TYPE_STRING:
  593. ret = snprintf(buf, sz, "%s\n", obj->string.pointer);
  594. break;
  595. }
  596. return ret;
  597. }
  598. static void atk_pack_print(char *buf, size_t sz, union acpi_object *pack)
  599. {
  600. int ret;
  601. int i;
  602. for (i = 0; i < pack->package.count; i++) {
  603. union acpi_object *obj = &pack->package.elements[i];
  604. ret = atk_acpi_print(buf, sz, obj);
  605. if (ret >= sz)
  606. break;
  607. buf += ret;
  608. sz -= ret;
  609. }
  610. }
  611. static int atk_debugfs_ggrp_open(struct inode *inode, struct file *file)
  612. {
  613. struct atk_data *data = inode->i_private;
  614. char *buf = NULL;
  615. union acpi_object *ret;
  616. u8 cls;
  617. int i;
  618. if (!data->enumerate_handle)
  619. return -ENODEV;
  620. if (!data->debugfs.id)
  621. return -EINVAL;
  622. cls = (data->debugfs.id & 0xff000000) >> 24;
  623. ret = atk_ggrp(data, cls);
  624. if (IS_ERR(ret))
  625. return PTR_ERR(ret);
  626. for (i = 0; i < ret->package.count; i++) {
  627. union acpi_object *pack = &ret->package.elements[i];
  628. union acpi_object *id;
  629. if (pack->type != ACPI_TYPE_PACKAGE)
  630. continue;
  631. if (!pack->package.count)
  632. continue;
  633. id = &pack->package.elements[0];
  634. if (id->integer.value == data->debugfs.id) {
  635. /* Print the package */
  636. buf = kzalloc(512, GFP_KERNEL);
  637. if (!buf) {
  638. ACPI_FREE(ret);
  639. return -ENOMEM;
  640. }
  641. atk_pack_print(buf, 512, pack);
  642. break;
  643. }
  644. }
  645. ACPI_FREE(ret);
  646. if (!buf)
  647. return -EINVAL;
  648. file->private_data = buf;
  649. return nonseekable_open(inode, file);
  650. }
  651. static ssize_t atk_debugfs_ggrp_read(struct file *file, char __user *buf,
  652. size_t count, loff_t *pos)
  653. {
  654. char *str = file->private_data;
  655. size_t len = strlen(str);
  656. return simple_read_from_buffer(buf, count, pos, str, len);
  657. }
  658. static int atk_debugfs_ggrp_release(struct inode *inode, struct file *file)
  659. {
  660. kfree(file->private_data);
  661. return 0;
  662. }
  663. static const struct file_operations atk_debugfs_ggrp_fops = {
  664. .read = atk_debugfs_ggrp_read,
  665. .open = atk_debugfs_ggrp_open,
  666. .release = atk_debugfs_ggrp_release,
  667. .llseek = no_llseek,
  668. };
  669. static void atk_debugfs_init(struct atk_data *data)
  670. {
  671. struct dentry *d;
  672. struct dentry *f;
  673. data->debugfs.id = 0;
  674. d = debugfs_create_dir("asus_atk0110", NULL);
  675. if (!d || IS_ERR(d))
  676. return;
  677. f = debugfs_create_x32("id", S_IRUSR | S_IWUSR, d, &data->debugfs.id);
  678. if (!f || IS_ERR(f))
  679. goto cleanup;
  680. f = debugfs_create_file("gitm", S_IRUSR, d, data,
  681. &atk_debugfs_gitm);
  682. if (!f || IS_ERR(f))
  683. goto cleanup;
  684. f = debugfs_create_file("ggrp", S_IRUSR, d, data,
  685. &atk_debugfs_ggrp_fops);
  686. if (!f || IS_ERR(f))
  687. goto cleanup;
  688. data->debugfs.root = d;
  689. return;
  690. cleanup:
  691. debugfs_remove_recursive(d);
  692. }
  693. static void atk_debugfs_cleanup(struct atk_data *data)
  694. {
  695. debugfs_remove_recursive(data->debugfs.root);
  696. }
  697. #else /* CONFIG_DEBUG_FS */
  698. static void atk_debugfs_init(struct atk_data *data)
  699. {
  700. }
  701. static void atk_debugfs_cleanup(struct atk_data *data)
  702. {
  703. }
  704. #endif
  705. static int atk_add_sensor(struct atk_data *data, union acpi_object *obj)
  706. {
  707. struct device *dev = &data->acpi_dev->dev;
  708. union acpi_object *flags;
  709. union acpi_object *name;
  710. union acpi_object *limit1;
  711. union acpi_object *limit2;
  712. union acpi_object *enable;
  713. struct atk_sensor_data *sensor;
  714. char const *base_name;
  715. char const *limit1_name;
  716. char const *limit2_name;
  717. u64 type;
  718. int err;
  719. int *num;
  720. int start;
  721. if (obj->type != ACPI_TYPE_PACKAGE) {
  722. /* wft is this? */
  723. dev_warn(dev, "Unknown type for ACPI object: (%d)\n",
  724. obj->type);
  725. return -EINVAL;
  726. }
  727. err = validate_hwmon_pack(data, obj);
  728. if (err)
  729. return err;
  730. /* Ok, we have a valid hwmon package */
  731. type = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS)->integer.value
  732. & ATK_TYPE_MASK;
  733. switch (type) {
  734. case HWMON_TYPE_VOLT:
  735. base_name = "in";
  736. limit1_name = "min";
  737. limit2_name = "max";
  738. num = &data->voltage_count;
  739. start = 0;
  740. break;
  741. case HWMON_TYPE_TEMP:
  742. base_name = "temp";
  743. limit1_name = "max";
  744. limit2_name = "crit";
  745. num = &data->temperature_count;
  746. start = 1;
  747. break;
  748. case HWMON_TYPE_FAN:
  749. base_name = "fan";
  750. limit1_name = "min";
  751. limit2_name = "max";
  752. num = &data->fan_count;
  753. start = 1;
  754. break;
  755. default:
  756. dev_warn(dev, "Unknown sensor type: %#llx\n", type);
  757. return -EINVAL;
  758. }
  759. enable = atk_get_pack_member(data, obj, HWMON_PACK_ENABLE);
  760. if (!enable->integer.value)
  761. /* sensor is disabled */
  762. return 0;
  763. flags = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS);
  764. name = atk_get_pack_member(data, obj, HWMON_PACK_NAME);
  765. limit1 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT1);
  766. limit2 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT2);
  767. sensor = kzalloc(sizeof(*sensor), GFP_KERNEL);
  768. if (!sensor)
  769. return -ENOMEM;
  770. sensor->acpi_name = kstrdup(name->string.pointer, GFP_KERNEL);
  771. if (!sensor->acpi_name) {
  772. err = -ENOMEM;
  773. goto out;
  774. }
  775. INIT_LIST_HEAD(&sensor->list);
  776. sensor->type = type;
  777. sensor->data = data;
  778. sensor->id = flags->integer.value;
  779. sensor->limit1 = limit1->integer.value;
  780. if (data->old_interface)
  781. sensor->limit2 = limit2->integer.value;
  782. else
  783. /* The upper limit is expressed as delta from lower limit */
  784. sensor->limit2 = sensor->limit1 + limit2->integer.value;
  785. snprintf(sensor->input_attr_name, ATTR_NAME_SIZE,
  786. "%s%d_input", base_name, start + *num);
  787. atk_init_attribute(&sensor->input_attr,
  788. sensor->input_attr_name,
  789. atk_input_show);
  790. snprintf(sensor->label_attr_name, ATTR_NAME_SIZE,
  791. "%s%d_label", base_name, start + *num);
  792. atk_init_attribute(&sensor->label_attr,
  793. sensor->label_attr_name,
  794. atk_label_show);
  795. snprintf(sensor->limit1_attr_name, ATTR_NAME_SIZE,
  796. "%s%d_%s", base_name, start + *num, limit1_name);
  797. atk_init_attribute(&sensor->limit1_attr,
  798. sensor->limit1_attr_name,
  799. atk_limit1_show);
  800. snprintf(sensor->limit2_attr_name, ATTR_NAME_SIZE,
  801. "%s%d_%s", base_name, start + *num, limit2_name);
  802. atk_init_attribute(&sensor->limit2_attr,
  803. sensor->limit2_attr_name,
  804. atk_limit2_show);
  805. list_add(&sensor->list, &data->sensor_list);
  806. (*num)++;
  807. return 1;
  808. out:
  809. kfree(sensor);
  810. return err;
  811. }
  812. static int atk_enumerate_old_hwmon(struct atk_data *data)
  813. {
  814. struct device *dev = &data->acpi_dev->dev;
  815. struct acpi_buffer buf;
  816. union acpi_object *pack;
  817. acpi_status status;
  818. int i, ret;
  819. int count = 0;
  820. /* Voltages */
  821. buf.length = ACPI_ALLOCATE_BUFFER;
  822. status = acpi_evaluate_object_typed(data->atk_handle,
  823. METHOD_OLD_ENUM_VLT, NULL, &buf, ACPI_TYPE_PACKAGE);
  824. if (status != AE_OK) {
  825. dev_warn(dev, METHOD_OLD_ENUM_VLT ": ACPI exception: %s\n",
  826. acpi_format_exception(status));
  827. return -ENODEV;
  828. }
  829. pack = buf.pointer;
  830. for (i = 1; i < pack->package.count; i++) {
  831. union acpi_object *obj = &pack->package.elements[i];
  832. ret = atk_add_sensor(data, obj);
  833. if (ret > 0)
  834. count++;
  835. }
  836. ACPI_FREE(buf.pointer);
  837. /* Temperatures */
  838. buf.length = ACPI_ALLOCATE_BUFFER;
  839. status = acpi_evaluate_object_typed(data->atk_handle,
  840. METHOD_OLD_ENUM_TMP, NULL, &buf, ACPI_TYPE_PACKAGE);
  841. if (status != AE_OK) {
  842. dev_warn(dev, METHOD_OLD_ENUM_TMP ": ACPI exception: %s\n",
  843. acpi_format_exception(status));
  844. ret = -ENODEV;
  845. goto cleanup;
  846. }
  847. pack = buf.pointer;
  848. for (i = 1; i < pack->package.count; i++) {
  849. union acpi_object *obj = &pack->package.elements[i];
  850. ret = atk_add_sensor(data, obj);
  851. if (ret > 0)
  852. count++;
  853. }
  854. ACPI_FREE(buf.pointer);
  855. /* Fans */
  856. buf.length = ACPI_ALLOCATE_BUFFER;
  857. status = acpi_evaluate_object_typed(data->atk_handle,
  858. METHOD_OLD_ENUM_FAN, NULL, &buf, ACPI_TYPE_PACKAGE);
  859. if (status != AE_OK) {
  860. dev_warn(dev, METHOD_OLD_ENUM_FAN ": ACPI exception: %s\n",
  861. acpi_format_exception(status));
  862. ret = -ENODEV;
  863. goto cleanup;
  864. }
  865. pack = buf.pointer;
  866. for (i = 1; i < pack->package.count; i++) {
  867. union acpi_object *obj = &pack->package.elements[i];
  868. ret = atk_add_sensor(data, obj);
  869. if (ret > 0)
  870. count++;
  871. }
  872. ACPI_FREE(buf.pointer);
  873. return count;
  874. cleanup:
  875. atk_free_sensors(data);
  876. return ret;
  877. }
  878. static int atk_ec_present(struct atk_data *data)
  879. {
  880. struct device *dev = &data->acpi_dev->dev;
  881. union acpi_object *pack;
  882. union acpi_object *ec;
  883. int ret;
  884. int i;
  885. pack = atk_ggrp(data, ATK_MUX_MGMT);
  886. if (IS_ERR(pack)) {
  887. if (PTR_ERR(pack) == -ENOENT) {
  888. /* The MGMT class does not exists - that's ok */
  889. dev_dbg(dev, "Class %#llx not found\n", ATK_MUX_MGMT);
  890. return 0;
  891. }
  892. return PTR_ERR(pack);
  893. }
  894. /* Search the EC */
  895. ec = NULL;
  896. for (i = 0; i < pack->package.count; i++) {
  897. union acpi_object *obj = &pack->package.elements[i];
  898. union acpi_object *id;
  899. if (obj->type != ACPI_TYPE_PACKAGE)
  900. continue;
  901. id = &obj->package.elements[0];
  902. if (id->type != ACPI_TYPE_INTEGER)
  903. continue;
  904. if (id->integer.value == ATK_EC_ID) {
  905. ec = obj;
  906. break;
  907. }
  908. }
  909. ret = (ec != NULL);
  910. if (!ret)
  911. /* The system has no EC */
  912. dev_dbg(dev, "EC not found\n");
  913. ACPI_FREE(pack);
  914. return ret;
  915. }
  916. static int atk_ec_enabled(struct atk_data *data)
  917. {
  918. struct device *dev = &data->acpi_dev->dev;
  919. union acpi_object *obj;
  920. struct atk_acpi_ret_buffer *buf;
  921. int err;
  922. obj = atk_gitm(data, ATK_EC_ID);
  923. if (IS_ERR(obj)) {
  924. dev_err(dev, "Unable to query EC status\n");
  925. return PTR_ERR(obj);
  926. }
  927. buf = (struct atk_acpi_ret_buffer *)obj->buffer.pointer;
  928. if (buf->flags == 0) {
  929. dev_err(dev, "Unable to query EC status\n");
  930. err = -EIO;
  931. } else {
  932. err = (buf->value != 0);
  933. dev_dbg(dev, "EC is %sabled\n",
  934. err ? "en" : "dis");
  935. }
  936. ACPI_FREE(obj);
  937. return err;
  938. }
  939. static int atk_ec_ctl(struct atk_data *data, int enable)
  940. {
  941. struct device *dev = &data->acpi_dev->dev;
  942. union acpi_object *obj;
  943. struct atk_acpi_input_buf sitm;
  944. struct atk_acpi_ret_buffer *ec_ret;
  945. int err = 0;
  946. sitm.id = ATK_EC_ID;
  947. sitm.param1 = enable;
  948. sitm.param2 = 0;
  949. obj = atk_sitm(data, &sitm);
  950. if (IS_ERR(obj)) {
  951. dev_err(dev, "Failed to %sable the EC\n",
  952. enable ? "en" : "dis");
  953. return PTR_ERR(obj);
  954. }
  955. ec_ret = (struct atk_acpi_ret_buffer *)obj->buffer.pointer;
  956. if (ec_ret->flags == 0) {
  957. dev_err(dev, "Failed to %sable the EC\n",
  958. enable ? "en" : "dis");
  959. err = -EIO;
  960. } else {
  961. dev_info(dev, "EC %sabled\n",
  962. enable ? "en" : "dis");
  963. }
  964. ACPI_FREE(obj);
  965. return err;
  966. }
  967. static int atk_enumerate_new_hwmon(struct atk_data *data)
  968. {
  969. struct device *dev = &data->acpi_dev->dev;
  970. union acpi_object *pack;
  971. int err;
  972. int i;
  973. err = atk_ec_present(data);
  974. if (err < 0)
  975. return err;
  976. if (err) {
  977. err = atk_ec_enabled(data);
  978. if (err < 0)
  979. return err;
  980. /* If the EC was disabled we will disable it again on unload */
  981. data->disable_ec = err;
  982. err = atk_ec_ctl(data, 1);
  983. if (err) {
  984. data->disable_ec = false;
  985. return err;
  986. }
  987. }
  988. dev_dbg(dev, "Enumerating hwmon sensors\n");
  989. pack = atk_ggrp(data, ATK_MUX_HWMON);
  990. if (IS_ERR(pack))
  991. return PTR_ERR(pack);
  992. for (i = 0; i < pack->package.count; i++) {
  993. union acpi_object *obj = &pack->package.elements[i];
  994. atk_add_sensor(data, obj);
  995. }
  996. err = data->voltage_count + data->temperature_count + data->fan_count;
  997. ACPI_FREE(pack);
  998. return err;
  999. }
  1000. static int atk_create_files(struct atk_data *data)
  1001. {
  1002. struct atk_sensor_data *s;
  1003. int err;
  1004. list_for_each_entry(s, &data->sensor_list, list) {
  1005. err = device_create_file(data->hwmon_dev, &s->input_attr);
  1006. if (err)
  1007. return err;
  1008. err = device_create_file(data->hwmon_dev, &s->label_attr);
  1009. if (err)
  1010. return err;
  1011. err = device_create_file(data->hwmon_dev, &s->limit1_attr);
  1012. if (err)
  1013. return err;
  1014. err = device_create_file(data->hwmon_dev, &s->limit2_attr);
  1015. if (err)
  1016. return err;
  1017. }
  1018. err = device_create_file(data->hwmon_dev, &atk_name_attr);
  1019. return err;
  1020. }
  1021. static void atk_remove_files(struct atk_data *data)
  1022. {
  1023. struct atk_sensor_data *s;
  1024. list_for_each_entry(s, &data->sensor_list, list) {
  1025. device_remove_file(data->hwmon_dev, &s->input_attr);
  1026. device_remove_file(data->hwmon_dev, &s->label_attr);
  1027. device_remove_file(data->hwmon_dev, &s->limit1_attr);
  1028. device_remove_file(data->hwmon_dev, &s->limit2_attr);
  1029. }
  1030. device_remove_file(data->hwmon_dev, &atk_name_attr);
  1031. }
  1032. static void atk_free_sensors(struct atk_data *data)
  1033. {
  1034. struct list_head *head = &data->sensor_list;
  1035. struct atk_sensor_data *s, *tmp;
  1036. list_for_each_entry_safe(s, tmp, head, list) {
  1037. kfree(s->acpi_name);
  1038. kfree(s);
  1039. }
  1040. }
  1041. static int atk_register_hwmon(struct atk_data *data)
  1042. {
  1043. struct device *dev = &data->acpi_dev->dev;
  1044. int err;
  1045. dev_dbg(dev, "registering hwmon device\n");
  1046. data->hwmon_dev = hwmon_device_register(dev);
  1047. if (IS_ERR(data->hwmon_dev))
  1048. return PTR_ERR(data->hwmon_dev);
  1049. dev_dbg(dev, "populating sysfs directory\n");
  1050. err = atk_create_files(data);
  1051. if (err)
  1052. goto remove;
  1053. return 0;
  1054. remove:
  1055. /* Cleanup the registered files */
  1056. atk_remove_files(data);
  1057. hwmon_device_unregister(data->hwmon_dev);
  1058. return err;
  1059. }
  1060. static int atk_probe_if(struct atk_data *data)
  1061. {
  1062. struct device *dev = &data->acpi_dev->dev;
  1063. acpi_handle ret;
  1064. acpi_status status;
  1065. int err = 0;
  1066. /* RTMP: read temperature */
  1067. status = acpi_get_handle(data->atk_handle, METHOD_OLD_READ_TMP, &ret);
  1068. if (ACPI_SUCCESS(status))
  1069. data->rtmp_handle = ret;
  1070. else
  1071. dev_dbg(dev, "method " METHOD_OLD_READ_TMP " not found: %s\n",
  1072. acpi_format_exception(status));
  1073. /* RVLT: read voltage */
  1074. status = acpi_get_handle(data->atk_handle, METHOD_OLD_READ_VLT, &ret);
  1075. if (ACPI_SUCCESS(status))
  1076. data->rvlt_handle = ret;
  1077. else
  1078. dev_dbg(dev, "method " METHOD_OLD_READ_VLT " not found: %s\n",
  1079. acpi_format_exception(status));
  1080. /* RFAN: read fan status */
  1081. status = acpi_get_handle(data->atk_handle, METHOD_OLD_READ_FAN, &ret);
  1082. if (ACPI_SUCCESS(status))
  1083. data->rfan_handle = ret;
  1084. else
  1085. dev_dbg(dev, "method " METHOD_OLD_READ_FAN " not found: %s\n",
  1086. acpi_format_exception(status));
  1087. /* Enumeration */
  1088. status = acpi_get_handle(data->atk_handle, METHOD_ENUMERATE, &ret);
  1089. if (ACPI_SUCCESS(status))
  1090. data->enumerate_handle = ret;
  1091. else
  1092. dev_dbg(dev, "method " METHOD_ENUMERATE " not found: %s\n",
  1093. acpi_format_exception(status));
  1094. /* De-multiplexer (read) */
  1095. status = acpi_get_handle(data->atk_handle, METHOD_READ, &ret);
  1096. if (ACPI_SUCCESS(status))
  1097. data->read_handle = ret;
  1098. else
  1099. dev_dbg(dev, "method " METHOD_READ " not found: %s\n",
  1100. acpi_format_exception(status));
  1101. /* De-multiplexer (write) */
  1102. status = acpi_get_handle(data->atk_handle, METHOD_WRITE, &ret);
  1103. if (ACPI_SUCCESS(status))
  1104. data->write_handle = ret;
  1105. else
  1106. dev_dbg(dev, "method " METHOD_WRITE " not found: %s\n",
  1107. acpi_format_exception(status));
  1108. /*
  1109. * Check for hwmon methods: first check "old" style methods; note that
  1110. * both may be present: in this case we stick to the old interface;
  1111. * analysis of multiple DSDTs indicates that when both interfaces
  1112. * are present the new one (GGRP/GITM) is not functional.
  1113. */
  1114. if (new_if)
  1115. dev_info(dev, "Overriding interface detection\n");
  1116. if (data->rtmp_handle &&
  1117. data->rvlt_handle && data->rfan_handle && !new_if)
  1118. data->old_interface = true;
  1119. else if (data->enumerate_handle && data->read_handle &&
  1120. data->write_handle)
  1121. data->old_interface = false;
  1122. else
  1123. err = -ENODEV;
  1124. return err;
  1125. }
  1126. static int atk_add(struct acpi_device *device)
  1127. {
  1128. acpi_status ret;
  1129. int err;
  1130. struct acpi_buffer buf;
  1131. union acpi_object *obj;
  1132. struct atk_data *data;
  1133. dev_dbg(&device->dev, "adding...\n");
  1134. data = kzalloc(sizeof(*data), GFP_KERNEL);
  1135. if (!data)
  1136. return -ENOMEM;
  1137. data->acpi_dev = device;
  1138. data->atk_handle = device->handle;
  1139. INIT_LIST_HEAD(&data->sensor_list);
  1140. data->disable_ec = false;
  1141. buf.length = ACPI_ALLOCATE_BUFFER;
  1142. ret = acpi_evaluate_object_typed(data->atk_handle, BOARD_ID, NULL,
  1143. &buf, ACPI_TYPE_PACKAGE);
  1144. if (ret != AE_OK) {
  1145. dev_dbg(&device->dev, "atk: method MBIF not found\n");
  1146. } else {
  1147. obj = buf.pointer;
  1148. if (obj->package.count >= 2) {
  1149. union acpi_object *id = &obj->package.elements[1];
  1150. if (id->type == ACPI_TYPE_STRING)
  1151. dev_dbg(&device->dev, "board ID = %s\n",
  1152. id->string.pointer);
  1153. }
  1154. ACPI_FREE(buf.pointer);
  1155. }
  1156. err = atk_probe_if(data);
  1157. if (err) {
  1158. dev_err(&device->dev, "No usable hwmon interface detected\n");
  1159. goto out;
  1160. }
  1161. if (data->old_interface) {
  1162. dev_dbg(&device->dev, "Using old hwmon interface\n");
  1163. err = atk_enumerate_old_hwmon(data);
  1164. } else {
  1165. dev_dbg(&device->dev, "Using new hwmon interface\n");
  1166. err = atk_enumerate_new_hwmon(data);
  1167. }
  1168. if (err < 0)
  1169. goto out;
  1170. if (err == 0) {
  1171. dev_info(&device->dev,
  1172. "No usable sensor detected, bailing out\n");
  1173. err = -ENODEV;
  1174. goto out;
  1175. }
  1176. err = atk_register_hwmon(data);
  1177. if (err)
  1178. goto cleanup;
  1179. atk_debugfs_init(data);
  1180. device->driver_data = data;
  1181. return 0;
  1182. cleanup:
  1183. atk_free_sensors(data);
  1184. out:
  1185. if (data->disable_ec)
  1186. atk_ec_ctl(data, 0);
  1187. kfree(data);
  1188. return err;
  1189. }
  1190. static int atk_remove(struct acpi_device *device)
  1191. {
  1192. struct atk_data *data = device->driver_data;
  1193. dev_dbg(&device->dev, "removing...\n");
  1194. device->driver_data = NULL;
  1195. atk_debugfs_cleanup(data);
  1196. atk_remove_files(data);
  1197. atk_free_sensors(data);
  1198. hwmon_device_unregister(data->hwmon_dev);
  1199. if (data->disable_ec) {
  1200. if (atk_ec_ctl(data, 0))
  1201. dev_err(&device->dev, "Failed to disable EC\n");
  1202. }
  1203. kfree(data);
  1204. return 0;
  1205. }
  1206. static int __init atk0110_init(void)
  1207. {
  1208. int ret;
  1209. /* Make sure it's safe to access the device through ACPI */
  1210. if (!acpi_resources_are_enforced()) {
  1211. pr_err("Resources not safely usable due to acpi_enforce_resources kernel parameter\n");
  1212. return -EBUSY;
  1213. }
  1214. if (dmi_check_system(atk_force_new_if))
  1215. new_if = true;
  1216. ret = acpi_bus_register_driver(&atk_driver);
  1217. if (ret)
  1218. pr_info("acpi_bus_register_driver failed: %d\n", ret);
  1219. return ret;
  1220. }
  1221. static void __exit atk0110_exit(void)
  1222. {
  1223. acpi_bus_unregister_driver(&atk_driver);
  1224. }
  1225. module_init(atk0110_init);
  1226. module_exit(atk0110_exit);
  1227. MODULE_LICENSE("GPL");