asus_atk0110.c 33 KB

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