asus_atk0110.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470
  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. /* Old interface reads the same sensor for fan0 and fan1 */
  32. .ident = "Asus M5A78L",
  33. .matches = {
  34. DMI_MATCH(DMI_BOARD_NAME, "M5A78L")
  35. }
  36. },
  37. { }
  38. };
  39. /*
  40. * Minimum time between readings, enforced in order to avoid
  41. * hogging the CPU.
  42. */
  43. #define CACHE_TIME HZ
  44. #define BOARD_ID "MBIF"
  45. #define METHOD_ENUMERATE "GGRP"
  46. #define METHOD_READ "GITM"
  47. #define METHOD_WRITE "SITM"
  48. #define METHOD_OLD_READ_TMP "RTMP"
  49. #define METHOD_OLD_READ_VLT "RVLT"
  50. #define METHOD_OLD_READ_FAN "RFAN"
  51. #define METHOD_OLD_ENUM_TMP "TSIF"
  52. #define METHOD_OLD_ENUM_VLT "VSIF"
  53. #define METHOD_OLD_ENUM_FAN "FSIF"
  54. #define ATK_MUX_HWMON 0x00000006ULL
  55. #define ATK_MUX_MGMT 0x00000011ULL
  56. #define ATK_CLASS_MASK 0xff000000ULL
  57. #define ATK_CLASS_FREQ_CTL 0x03000000ULL
  58. #define ATK_CLASS_FAN_CTL 0x04000000ULL
  59. #define ATK_CLASS_HWMON 0x06000000ULL
  60. #define ATK_CLASS_MGMT 0x11000000ULL
  61. #define ATK_TYPE_MASK 0x00ff0000ULL
  62. #define HWMON_TYPE_VOLT 0x00020000ULL
  63. #define HWMON_TYPE_TEMP 0x00030000ULL
  64. #define HWMON_TYPE_FAN 0x00040000ULL
  65. #define ATK_ELEMENT_ID_MASK 0x0000ffffULL
  66. #define ATK_EC_ID 0x11060004ULL
  67. enum atk_pack_member {
  68. HWMON_PACK_FLAGS,
  69. HWMON_PACK_NAME,
  70. HWMON_PACK_LIMIT1,
  71. HWMON_PACK_LIMIT2,
  72. HWMON_PACK_ENABLE
  73. };
  74. /* New package format */
  75. #define _HWMON_NEW_PACK_SIZE 7
  76. #define _HWMON_NEW_PACK_FLAGS 0
  77. #define _HWMON_NEW_PACK_NAME 1
  78. #define _HWMON_NEW_PACK_UNK1 2
  79. #define _HWMON_NEW_PACK_UNK2 3
  80. #define _HWMON_NEW_PACK_LIMIT1 4
  81. #define _HWMON_NEW_PACK_LIMIT2 5
  82. #define _HWMON_NEW_PACK_ENABLE 6
  83. /* Old package format */
  84. #define _HWMON_OLD_PACK_SIZE 5
  85. #define _HWMON_OLD_PACK_FLAGS 0
  86. #define _HWMON_OLD_PACK_NAME 1
  87. #define _HWMON_OLD_PACK_LIMIT1 2
  88. #define _HWMON_OLD_PACK_LIMIT2 3
  89. #define _HWMON_OLD_PACK_ENABLE 4
  90. struct atk_data {
  91. struct device *hwmon_dev;
  92. acpi_handle atk_handle;
  93. struct acpi_device *acpi_dev;
  94. bool old_interface;
  95. /* old interface */
  96. acpi_handle rtmp_handle;
  97. acpi_handle rvlt_handle;
  98. acpi_handle rfan_handle;
  99. /* new inteface */
  100. acpi_handle enumerate_handle;
  101. acpi_handle read_handle;
  102. acpi_handle write_handle;
  103. bool disable_ec;
  104. int voltage_count;
  105. int temperature_count;
  106. int fan_count;
  107. struct list_head sensor_list;
  108. struct {
  109. struct dentry *root;
  110. u32 id;
  111. } debugfs;
  112. };
  113. typedef ssize_t (*sysfs_show_func)(struct device *dev,
  114. struct device_attribute *attr, char *buf);
  115. static const struct acpi_device_id atk_ids[] = {
  116. {ATK_HID, 0},
  117. {"", 0},
  118. };
  119. MODULE_DEVICE_TABLE(acpi, atk_ids);
  120. #define ATTR_NAME_SIZE 16 /* Worst case is "tempN_input" */
  121. struct atk_sensor_data {
  122. struct list_head list;
  123. struct atk_data *data;
  124. struct device_attribute label_attr;
  125. struct device_attribute input_attr;
  126. struct device_attribute limit1_attr;
  127. struct device_attribute limit2_attr;
  128. char label_attr_name[ATTR_NAME_SIZE];
  129. char input_attr_name[ATTR_NAME_SIZE];
  130. char limit1_attr_name[ATTR_NAME_SIZE];
  131. char limit2_attr_name[ATTR_NAME_SIZE];
  132. u64 id;
  133. u64 type;
  134. u64 limit1;
  135. u64 limit2;
  136. u64 cached_value;
  137. unsigned long last_updated; /* in jiffies */
  138. bool is_valid;
  139. char const *acpi_name;
  140. };
  141. /*
  142. * Return buffer format:
  143. * [0-3] "value" is valid flag
  144. * [4-7] value
  145. * [8- ] unknown stuff on newer mobos
  146. */
  147. struct atk_acpi_ret_buffer {
  148. u32 flags;
  149. u32 value;
  150. u8 data[];
  151. };
  152. /* Input buffer used for GITM and SITM methods */
  153. struct atk_acpi_input_buf {
  154. u32 id;
  155. u32 param1;
  156. u32 param2;
  157. };
  158. static int atk_add(struct acpi_device *device);
  159. static int atk_remove(struct acpi_device *device, int type);
  160. static void atk_print_sensor(struct atk_data *data, union acpi_object *obj);
  161. static int atk_read_value(struct atk_sensor_data *sensor, u64 *value);
  162. static void atk_free_sensors(struct atk_data *data);
  163. static struct acpi_driver atk_driver = {
  164. .name = ATK_HID,
  165. .class = "hwmon",
  166. .ids = atk_ids,
  167. .ops = {
  168. .add = atk_add,
  169. .remove = atk_remove,
  170. },
  171. };
  172. #define input_to_atk_sensor(attr) \
  173. container_of(attr, struct atk_sensor_data, input_attr)
  174. #define label_to_atk_sensor(attr) \
  175. container_of(attr, struct atk_sensor_data, label_attr)
  176. #define limit1_to_atk_sensor(attr) \
  177. container_of(attr, struct atk_sensor_data, limit1_attr)
  178. #define limit2_to_atk_sensor(attr) \
  179. container_of(attr, struct atk_sensor_data, limit2_attr)
  180. static ssize_t atk_input_show(struct device *dev,
  181. struct device_attribute *attr, char *buf)
  182. {
  183. struct atk_sensor_data *s = input_to_atk_sensor(attr);
  184. u64 value;
  185. int err;
  186. err = atk_read_value(s, &value);
  187. if (err)
  188. return err;
  189. if (s->type == HWMON_TYPE_TEMP)
  190. /* ACPI returns decidegree */
  191. value *= 100;
  192. return sprintf(buf, "%llu\n", value);
  193. }
  194. static ssize_t atk_label_show(struct device *dev,
  195. struct device_attribute *attr, char *buf)
  196. {
  197. struct atk_sensor_data *s = label_to_atk_sensor(attr);
  198. return sprintf(buf, "%s\n", s->acpi_name);
  199. }
  200. static ssize_t atk_limit1_show(struct device *dev,
  201. struct device_attribute *attr, char *buf)
  202. {
  203. struct atk_sensor_data *s = limit1_to_atk_sensor(attr);
  204. u64 value = s->limit1;
  205. if (s->type == HWMON_TYPE_TEMP)
  206. value *= 100;
  207. return sprintf(buf, "%lld\n", value);
  208. }
  209. static ssize_t atk_limit2_show(struct device *dev,
  210. struct device_attribute *attr, char *buf)
  211. {
  212. struct atk_sensor_data *s = limit2_to_atk_sensor(attr);
  213. u64 value = s->limit2;
  214. if (s->type == HWMON_TYPE_TEMP)
  215. value *= 100;
  216. return sprintf(buf, "%lld\n", value);
  217. }
  218. static ssize_t atk_name_show(struct device *dev,
  219. struct device_attribute *attr, char *buf)
  220. {
  221. return sprintf(buf, "atk0110\n");
  222. }
  223. static struct device_attribute atk_name_attr =
  224. __ATTR(name, 0444, atk_name_show, NULL);
  225. static void atk_init_attribute(struct device_attribute *attr, char *name,
  226. sysfs_show_func show)
  227. {
  228. sysfs_attr_init(&attr->attr);
  229. attr->attr.name = name;
  230. attr->attr.mode = 0444;
  231. attr->show = show;
  232. attr->store = NULL;
  233. }
  234. static union acpi_object *atk_get_pack_member(struct atk_data *data,
  235. union acpi_object *pack,
  236. enum atk_pack_member m)
  237. {
  238. bool old_if = data->old_interface;
  239. int offset;
  240. switch (m) {
  241. case HWMON_PACK_FLAGS:
  242. offset = old_if ? _HWMON_OLD_PACK_FLAGS : _HWMON_NEW_PACK_FLAGS;
  243. break;
  244. case HWMON_PACK_NAME:
  245. offset = old_if ? _HWMON_OLD_PACK_NAME : _HWMON_NEW_PACK_NAME;
  246. break;
  247. case HWMON_PACK_LIMIT1:
  248. offset = old_if ? _HWMON_OLD_PACK_LIMIT1 :
  249. _HWMON_NEW_PACK_LIMIT1;
  250. break;
  251. case HWMON_PACK_LIMIT2:
  252. offset = old_if ? _HWMON_OLD_PACK_LIMIT2 :
  253. _HWMON_NEW_PACK_LIMIT2;
  254. break;
  255. case HWMON_PACK_ENABLE:
  256. offset = old_if ? _HWMON_OLD_PACK_ENABLE :
  257. _HWMON_NEW_PACK_ENABLE;
  258. break;
  259. default:
  260. return NULL;
  261. }
  262. return &pack->package.elements[offset];
  263. }
  264. /*
  265. * New package format is:
  266. * - flag (int)
  267. * class - used for de-muxing the request to the correct GITn
  268. * type (volt, temp, fan)
  269. * sensor id |
  270. * sensor id - used for de-muxing the request _inside_ the GITn
  271. * - name (str)
  272. * - unknown (int)
  273. * - unknown (int)
  274. * - limit1 (int)
  275. * - limit2 (int)
  276. * - enable (int)
  277. *
  278. * The old package has the same format but it's missing the two unknown fields.
  279. */
  280. static int validate_hwmon_pack(struct atk_data *data, union acpi_object *obj)
  281. {
  282. struct device *dev = &data->acpi_dev->dev;
  283. union acpi_object *tmp;
  284. bool old_if = data->old_interface;
  285. int const expected_size = old_if ? _HWMON_OLD_PACK_SIZE :
  286. _HWMON_NEW_PACK_SIZE;
  287. if (obj->type != ACPI_TYPE_PACKAGE) {
  288. dev_warn(dev, "Invalid type: %d\n", obj->type);
  289. return -EINVAL;
  290. }
  291. if (obj->package.count != expected_size) {
  292. dev_warn(dev, "Invalid package size: %d, expected: %d\n",
  293. obj->package.count, expected_size);
  294. return -EINVAL;
  295. }
  296. tmp = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS);
  297. if (tmp->type != ACPI_TYPE_INTEGER) {
  298. dev_warn(dev, "Invalid type (flag): %d\n", tmp->type);
  299. return -EINVAL;
  300. }
  301. tmp = atk_get_pack_member(data, obj, HWMON_PACK_NAME);
  302. if (tmp->type != ACPI_TYPE_STRING) {
  303. dev_warn(dev, "Invalid type (name): %d\n", tmp->type);
  304. return -EINVAL;
  305. }
  306. /* Don't check... we don't know what they're useful for anyway */
  307. #if 0
  308. tmp = &obj->package.elements[HWMON_PACK_UNK1];
  309. if (tmp->type != ACPI_TYPE_INTEGER) {
  310. dev_warn(dev, "Invalid type (unk1): %d\n", tmp->type);
  311. return -EINVAL;
  312. }
  313. tmp = &obj->package.elements[HWMON_PACK_UNK2];
  314. if (tmp->type != ACPI_TYPE_INTEGER) {
  315. dev_warn(dev, "Invalid type (unk2): %d\n", tmp->type);
  316. return -EINVAL;
  317. }
  318. #endif
  319. tmp = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT1);
  320. if (tmp->type != ACPI_TYPE_INTEGER) {
  321. dev_warn(dev, "Invalid type (limit1): %d\n", tmp->type);
  322. return -EINVAL;
  323. }
  324. tmp = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT2);
  325. if (tmp->type != ACPI_TYPE_INTEGER) {
  326. dev_warn(dev, "Invalid type (limit2): %d\n", tmp->type);
  327. return -EINVAL;
  328. }
  329. tmp = atk_get_pack_member(data, obj, HWMON_PACK_ENABLE);
  330. if (tmp->type != ACPI_TYPE_INTEGER) {
  331. dev_warn(dev, "Invalid type (enable): %d\n", tmp->type);
  332. return -EINVAL;
  333. }
  334. atk_print_sensor(data, obj);
  335. return 0;
  336. }
  337. #ifdef DEBUG
  338. static char const *atk_sensor_type(union acpi_object *flags)
  339. {
  340. u64 type = flags->integer.value & ATK_TYPE_MASK;
  341. char const *what;
  342. switch (type) {
  343. case HWMON_TYPE_VOLT:
  344. what = "voltage";
  345. break;
  346. case HWMON_TYPE_TEMP:
  347. what = "temperature";
  348. break;
  349. case HWMON_TYPE_FAN:
  350. what = "fan";
  351. break;
  352. default:
  353. what = "unknown";
  354. break;
  355. }
  356. return what;
  357. }
  358. #endif
  359. static void atk_print_sensor(struct atk_data *data, union acpi_object *obj)
  360. {
  361. #ifdef DEBUG
  362. struct device *dev = &data->acpi_dev->dev;
  363. union acpi_object *flags;
  364. union acpi_object *name;
  365. union acpi_object *limit1;
  366. union acpi_object *limit2;
  367. union acpi_object *enable;
  368. char const *what;
  369. flags = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS);
  370. name = atk_get_pack_member(data, obj, HWMON_PACK_NAME);
  371. limit1 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT1);
  372. limit2 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT2);
  373. enable = atk_get_pack_member(data, obj, HWMON_PACK_ENABLE);
  374. what = atk_sensor_type(flags);
  375. dev_dbg(dev, "%s: %#llx %s [%llu-%llu] %s\n", what,
  376. flags->integer.value,
  377. name->string.pointer,
  378. limit1->integer.value, limit2->integer.value,
  379. enable->integer.value ? "enabled" : "disabled");
  380. #endif
  381. }
  382. static int atk_read_value_old(struct atk_sensor_data *sensor, u64 *value)
  383. {
  384. struct atk_data *data = sensor->data;
  385. struct device *dev = &data->acpi_dev->dev;
  386. struct acpi_object_list params;
  387. union acpi_object id;
  388. acpi_status status;
  389. acpi_handle method;
  390. switch (sensor->type) {
  391. case HWMON_TYPE_VOLT:
  392. method = data->rvlt_handle;
  393. break;
  394. case HWMON_TYPE_TEMP:
  395. method = data->rtmp_handle;
  396. break;
  397. case HWMON_TYPE_FAN:
  398. method = data->rfan_handle;
  399. break;
  400. default:
  401. return -EINVAL;
  402. }
  403. id.type = ACPI_TYPE_INTEGER;
  404. id.integer.value = sensor->id;
  405. params.count = 1;
  406. params.pointer = &id;
  407. status = acpi_evaluate_integer(method, NULL, &params, value);
  408. if (status != AE_OK) {
  409. dev_warn(dev, "%s: ACPI exception: %s\n", __func__,
  410. acpi_format_exception(status));
  411. return -EIO;
  412. }
  413. return 0;
  414. }
  415. static union acpi_object *atk_ggrp(struct atk_data *data, u16 mux)
  416. {
  417. struct device *dev = &data->acpi_dev->dev;
  418. struct acpi_buffer buf;
  419. acpi_status ret;
  420. struct acpi_object_list params;
  421. union acpi_object id;
  422. union acpi_object *pack;
  423. id.type = ACPI_TYPE_INTEGER;
  424. id.integer.value = mux;
  425. params.count = 1;
  426. params.pointer = &id;
  427. buf.length = ACPI_ALLOCATE_BUFFER;
  428. ret = acpi_evaluate_object(data->enumerate_handle, NULL, &params, &buf);
  429. if (ret != AE_OK) {
  430. dev_err(dev, "GGRP[%#x] ACPI exception: %s\n", mux,
  431. acpi_format_exception(ret));
  432. return ERR_PTR(-EIO);
  433. }
  434. pack = buf.pointer;
  435. if (pack->type != ACPI_TYPE_PACKAGE) {
  436. /* Execution was successful, but the id was not found */
  437. ACPI_FREE(pack);
  438. return ERR_PTR(-ENOENT);
  439. }
  440. if (pack->package.count < 1) {
  441. dev_err(dev, "GGRP[%#x] package is too small\n", mux);
  442. ACPI_FREE(pack);
  443. return ERR_PTR(-EIO);
  444. }
  445. return pack;
  446. }
  447. static union acpi_object *atk_gitm(struct atk_data *data, u64 id)
  448. {
  449. struct device *dev = &data->acpi_dev->dev;
  450. struct atk_acpi_input_buf buf;
  451. union acpi_object tmp;
  452. struct acpi_object_list params;
  453. struct acpi_buffer ret;
  454. union acpi_object *obj;
  455. acpi_status status;
  456. buf.id = id;
  457. buf.param1 = 0;
  458. buf.param2 = 0;
  459. tmp.type = ACPI_TYPE_BUFFER;
  460. tmp.buffer.pointer = (u8 *)&buf;
  461. tmp.buffer.length = sizeof(buf);
  462. params.count = 1;
  463. params.pointer = (void *)&tmp;
  464. ret.length = ACPI_ALLOCATE_BUFFER;
  465. status = acpi_evaluate_object_typed(data->read_handle, NULL, &params,
  466. &ret, ACPI_TYPE_BUFFER);
  467. if (status != AE_OK) {
  468. dev_warn(dev, "GITM[%#llx] ACPI exception: %s\n", id,
  469. acpi_format_exception(status));
  470. return ERR_PTR(-EIO);
  471. }
  472. obj = ret.pointer;
  473. /* Sanity check */
  474. if (obj->buffer.length < 8) {
  475. dev_warn(dev, "Unexpected ASBF length: %u\n",
  476. obj->buffer.length);
  477. ACPI_FREE(obj);
  478. return ERR_PTR(-EIO);
  479. }
  480. return obj;
  481. }
  482. static union acpi_object *atk_sitm(struct atk_data *data,
  483. struct atk_acpi_input_buf *buf)
  484. {
  485. struct device *dev = &data->acpi_dev->dev;
  486. struct acpi_object_list params;
  487. union acpi_object tmp;
  488. struct acpi_buffer ret;
  489. union acpi_object *obj;
  490. acpi_status status;
  491. tmp.type = ACPI_TYPE_BUFFER;
  492. tmp.buffer.pointer = (u8 *)buf;
  493. tmp.buffer.length = sizeof(*buf);
  494. params.count = 1;
  495. params.pointer = &tmp;
  496. ret.length = ACPI_ALLOCATE_BUFFER;
  497. status = acpi_evaluate_object_typed(data->write_handle, NULL, &params,
  498. &ret, ACPI_TYPE_BUFFER);
  499. if (status != AE_OK) {
  500. dev_warn(dev, "SITM[%#x] ACPI exception: %s\n", buf->id,
  501. acpi_format_exception(status));
  502. return ERR_PTR(-EIO);
  503. }
  504. obj = ret.pointer;
  505. /* Sanity check */
  506. if (obj->buffer.length < 8) {
  507. dev_warn(dev, "Unexpected ASBF length: %u\n",
  508. obj->buffer.length);
  509. ACPI_FREE(obj);
  510. return ERR_PTR(-EIO);
  511. }
  512. return obj;
  513. }
  514. static int atk_read_value_new(struct atk_sensor_data *sensor, u64 *value)
  515. {
  516. struct atk_data *data = sensor->data;
  517. struct device *dev = &data->acpi_dev->dev;
  518. union acpi_object *obj;
  519. struct atk_acpi_ret_buffer *buf;
  520. int err = 0;
  521. obj = atk_gitm(data, sensor->id);
  522. if (IS_ERR(obj))
  523. return PTR_ERR(obj);
  524. buf = (struct atk_acpi_ret_buffer *)obj->buffer.pointer;
  525. if (buf->flags == 0) {
  526. /*
  527. * The reading is not valid, possible causes:
  528. * - sensor failure
  529. * - enumeration was FUBAR (and we didn't notice)
  530. */
  531. dev_warn(dev, "Read failed, sensor = %#llx\n", sensor->id);
  532. err = -EIO;
  533. goto out;
  534. }
  535. *value = buf->value;
  536. out:
  537. ACPI_FREE(obj);
  538. return err;
  539. }
  540. static int atk_read_value(struct atk_sensor_data *sensor, u64 *value)
  541. {
  542. int err;
  543. if (!sensor->is_valid ||
  544. time_after(jiffies, sensor->last_updated + CACHE_TIME)) {
  545. if (sensor->data->old_interface)
  546. err = atk_read_value_old(sensor, value);
  547. else
  548. err = atk_read_value_new(sensor, value);
  549. sensor->is_valid = true;
  550. sensor->last_updated = jiffies;
  551. sensor->cached_value = *value;
  552. } else {
  553. *value = sensor->cached_value;
  554. err = 0;
  555. }
  556. return err;
  557. }
  558. #ifdef CONFIG_DEBUG_FS
  559. static int atk_debugfs_gitm_get(void *p, u64 *val)
  560. {
  561. struct atk_data *data = p;
  562. union acpi_object *ret;
  563. struct atk_acpi_ret_buffer *buf;
  564. int err = 0;
  565. if (!data->read_handle)
  566. return -ENODEV;
  567. if (!data->debugfs.id)
  568. return -EINVAL;
  569. ret = atk_gitm(data, data->debugfs.id);
  570. if (IS_ERR(ret))
  571. return PTR_ERR(ret);
  572. buf = (struct atk_acpi_ret_buffer *)ret->buffer.pointer;
  573. if (buf->flags)
  574. *val = buf->value;
  575. else
  576. err = -EIO;
  577. ACPI_FREE(ret);
  578. return err;
  579. }
  580. DEFINE_SIMPLE_ATTRIBUTE(atk_debugfs_gitm,
  581. atk_debugfs_gitm_get,
  582. NULL,
  583. "0x%08llx\n")
  584. static int atk_acpi_print(char *buf, size_t sz, union acpi_object *obj)
  585. {
  586. int ret = 0;
  587. switch (obj->type) {
  588. case ACPI_TYPE_INTEGER:
  589. ret = snprintf(buf, sz, "0x%08llx\n", obj->integer.value);
  590. break;
  591. case ACPI_TYPE_STRING:
  592. ret = snprintf(buf, sz, "%s\n", obj->string.pointer);
  593. break;
  594. }
  595. return ret;
  596. }
  597. static void atk_pack_print(char *buf, size_t sz, union acpi_object *pack)
  598. {
  599. int ret;
  600. int i;
  601. for (i = 0; i < pack->package.count; i++) {
  602. union acpi_object *obj = &pack->package.elements[i];
  603. ret = atk_acpi_print(buf, sz, obj);
  604. if (ret >= sz)
  605. break;
  606. buf += ret;
  607. sz -= ret;
  608. }
  609. }
  610. static int atk_debugfs_ggrp_open(struct inode *inode, struct file *file)
  611. {
  612. struct atk_data *data = inode->i_private;
  613. char *buf = NULL;
  614. union acpi_object *ret;
  615. u8 cls;
  616. int i;
  617. if (!data->enumerate_handle)
  618. return -ENODEV;
  619. if (!data->debugfs.id)
  620. return -EINVAL;
  621. cls = (data->debugfs.id & 0xff000000) >> 24;
  622. ret = atk_ggrp(data, cls);
  623. if (IS_ERR(ret))
  624. return PTR_ERR(ret);
  625. for (i = 0; i < ret->package.count; i++) {
  626. union acpi_object *pack = &ret->package.elements[i];
  627. union acpi_object *id;
  628. if (pack->type != ACPI_TYPE_PACKAGE)
  629. continue;
  630. if (!pack->package.count)
  631. continue;
  632. id = &pack->package.elements[0];
  633. if (id->integer.value == data->debugfs.id) {
  634. /* Print the package */
  635. buf = kzalloc(512, GFP_KERNEL);
  636. if (!buf) {
  637. ACPI_FREE(ret);
  638. return -ENOMEM;
  639. }
  640. atk_pack_print(buf, 512, pack);
  641. break;
  642. }
  643. }
  644. ACPI_FREE(ret);
  645. if (!buf)
  646. return -EINVAL;
  647. file->private_data = buf;
  648. return nonseekable_open(inode, file);
  649. }
  650. static ssize_t atk_debugfs_ggrp_read(struct file *file, char __user *buf,
  651. size_t count, loff_t *pos)
  652. {
  653. char *str = file->private_data;
  654. size_t len = strlen(str);
  655. return simple_read_from_buffer(buf, count, pos, str, len);
  656. }
  657. static int atk_debugfs_ggrp_release(struct inode *inode, struct file *file)
  658. {
  659. kfree(file->private_data);
  660. return 0;
  661. }
  662. static const struct file_operations atk_debugfs_ggrp_fops = {
  663. .read = atk_debugfs_ggrp_read,
  664. .open = atk_debugfs_ggrp_open,
  665. .release = atk_debugfs_ggrp_release,
  666. .llseek = no_llseek,
  667. };
  668. static void atk_debugfs_init(struct atk_data *data)
  669. {
  670. struct dentry *d;
  671. struct dentry *f;
  672. data->debugfs.id = 0;
  673. d = debugfs_create_dir("asus_atk0110", NULL);
  674. if (!d || IS_ERR(d))
  675. return;
  676. f = debugfs_create_x32("id", S_IRUSR | S_IWUSR, d, &data->debugfs.id);
  677. if (!f || IS_ERR(f))
  678. goto cleanup;
  679. f = debugfs_create_file("gitm", S_IRUSR, d, data,
  680. &atk_debugfs_gitm);
  681. if (!f || IS_ERR(f))
  682. goto cleanup;
  683. f = debugfs_create_file("ggrp", S_IRUSR, d, data,
  684. &atk_debugfs_ggrp_fops);
  685. if (!f || IS_ERR(f))
  686. goto cleanup;
  687. data->debugfs.root = d;
  688. return;
  689. cleanup:
  690. debugfs_remove_recursive(d);
  691. }
  692. static void atk_debugfs_cleanup(struct atk_data *data)
  693. {
  694. debugfs_remove_recursive(data->debugfs.root);
  695. }
  696. #else /* CONFIG_DEBUG_FS */
  697. static void atk_debugfs_init(struct atk_data *data)
  698. {
  699. }
  700. static void atk_debugfs_cleanup(struct atk_data *data)
  701. {
  702. }
  703. #endif
  704. static int atk_add_sensor(struct atk_data *data, union acpi_object *obj)
  705. {
  706. struct device *dev = &data->acpi_dev->dev;
  707. union acpi_object *flags;
  708. union acpi_object *name;
  709. union acpi_object *limit1;
  710. union acpi_object *limit2;
  711. union acpi_object *enable;
  712. struct atk_sensor_data *sensor;
  713. char const *base_name;
  714. char const *limit1_name;
  715. char const *limit2_name;
  716. u64 type;
  717. int err;
  718. int *num;
  719. int start;
  720. if (obj->type != ACPI_TYPE_PACKAGE) {
  721. /* wft is this? */
  722. dev_warn(dev, "Unknown type for ACPI object: (%d)\n",
  723. obj->type);
  724. return -EINVAL;
  725. }
  726. err = validate_hwmon_pack(data, obj);
  727. if (err)
  728. return err;
  729. /* Ok, we have a valid hwmon package */
  730. type = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS)->integer.value
  731. & ATK_TYPE_MASK;
  732. switch (type) {
  733. case HWMON_TYPE_VOLT:
  734. base_name = "in";
  735. limit1_name = "min";
  736. limit2_name = "max";
  737. num = &data->voltage_count;
  738. start = 0;
  739. break;
  740. case HWMON_TYPE_TEMP:
  741. base_name = "temp";
  742. limit1_name = "max";
  743. limit2_name = "crit";
  744. num = &data->temperature_count;
  745. start = 1;
  746. break;
  747. case HWMON_TYPE_FAN:
  748. base_name = "fan";
  749. limit1_name = "min";
  750. limit2_name = "max";
  751. num = &data->fan_count;
  752. start = 1;
  753. break;
  754. default:
  755. dev_warn(dev, "Unknown sensor type: %#llx\n", type);
  756. return -EINVAL;
  757. }
  758. enable = atk_get_pack_member(data, obj, HWMON_PACK_ENABLE);
  759. if (!enable->integer.value)
  760. /* sensor is disabled */
  761. return 0;
  762. flags = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS);
  763. name = atk_get_pack_member(data, obj, HWMON_PACK_NAME);
  764. limit1 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT1);
  765. limit2 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT2);
  766. sensor = kzalloc(sizeof(*sensor), GFP_KERNEL);
  767. if (!sensor)
  768. return -ENOMEM;
  769. sensor->acpi_name = kstrdup(name->string.pointer, GFP_KERNEL);
  770. if (!sensor->acpi_name) {
  771. err = -ENOMEM;
  772. goto out;
  773. }
  774. INIT_LIST_HEAD(&sensor->list);
  775. sensor->type = type;
  776. sensor->data = data;
  777. sensor->id = flags->integer.value;
  778. sensor->limit1 = limit1->integer.value;
  779. if (data->old_interface)
  780. sensor->limit2 = limit2->integer.value;
  781. else
  782. /* The upper limit is expressed as delta from lower limit */
  783. sensor->limit2 = sensor->limit1 + limit2->integer.value;
  784. snprintf(sensor->input_attr_name, ATTR_NAME_SIZE,
  785. "%s%d_input", base_name, start + *num);
  786. atk_init_attribute(&sensor->input_attr,
  787. sensor->input_attr_name,
  788. atk_input_show);
  789. snprintf(sensor->label_attr_name, ATTR_NAME_SIZE,
  790. "%s%d_label", base_name, start + *num);
  791. atk_init_attribute(&sensor->label_attr,
  792. sensor->label_attr_name,
  793. atk_label_show);
  794. snprintf(sensor->limit1_attr_name, ATTR_NAME_SIZE,
  795. "%s%d_%s", base_name, start + *num, limit1_name);
  796. atk_init_attribute(&sensor->limit1_attr,
  797. sensor->limit1_attr_name,
  798. atk_limit1_show);
  799. snprintf(sensor->limit2_attr_name, ATTR_NAME_SIZE,
  800. "%s%d_%s", base_name, start + *num, limit2_name);
  801. atk_init_attribute(&sensor->limit2_attr,
  802. sensor->limit2_attr_name,
  803. atk_limit2_show);
  804. list_add(&sensor->list, &data->sensor_list);
  805. (*num)++;
  806. return 1;
  807. out:
  808. kfree(sensor->acpi_name);
  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, int type)
  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");