asus_atk0110.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468
  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");