nouveau_hwmon.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. /*
  2. * Copyright 2010 Red Hat Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Ben Skeggs
  23. */
  24. #ifdef CONFIG_ACPI
  25. #include <linux/acpi.h>
  26. #endif
  27. #include <linux/power_supply.h>
  28. #include <linux/hwmon.h>
  29. #include <linux/hwmon-sysfs.h>
  30. #include <drm/drmP.h>
  31. #include "nouveau_drv.h"
  32. #include "nouveau_hwmon.h"
  33. #include <nvkm/subdev/iccsense.h>
  34. #include <nvkm/subdev/volt.h>
  35. #if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
  36. static ssize_t
  37. nouveau_hwmon_show_temp(struct device *d, struct device_attribute *a, char *buf)
  38. {
  39. struct drm_device *dev = dev_get_drvdata(d);
  40. struct nouveau_drm *drm = nouveau_drm(dev);
  41. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  42. int temp = nvkm_therm_temp_get(therm);
  43. if (temp < 0)
  44. return temp;
  45. return snprintf(buf, PAGE_SIZE, "%d\n", temp * 1000);
  46. }
  47. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, nouveau_hwmon_show_temp,
  48. NULL, 0);
  49. static ssize_t
  50. nouveau_hwmon_show_temp1_auto_point1_pwm(struct device *d,
  51. struct device_attribute *a, char *buf)
  52. {
  53. return snprintf(buf, PAGE_SIZE, "%d\n", 100);
  54. }
  55. static SENSOR_DEVICE_ATTR(temp1_auto_point1_pwm, S_IRUGO,
  56. nouveau_hwmon_show_temp1_auto_point1_pwm, NULL, 0);
  57. static ssize_t
  58. nouveau_hwmon_temp1_auto_point1_temp(struct device *d,
  59. struct device_attribute *a, char *buf)
  60. {
  61. struct drm_device *dev = dev_get_drvdata(d);
  62. struct nouveau_drm *drm = nouveau_drm(dev);
  63. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  64. return snprintf(buf, PAGE_SIZE, "%d\n",
  65. therm->attr_get(therm, NVKM_THERM_ATTR_THRS_FAN_BOOST) * 1000);
  66. }
  67. static ssize_t
  68. nouveau_hwmon_set_temp1_auto_point1_temp(struct device *d,
  69. struct device_attribute *a,
  70. const char *buf, size_t count)
  71. {
  72. struct drm_device *dev = dev_get_drvdata(d);
  73. struct nouveau_drm *drm = nouveau_drm(dev);
  74. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  75. long value;
  76. if (kstrtol(buf, 10, &value) == -EINVAL)
  77. return count;
  78. therm->attr_set(therm, NVKM_THERM_ATTR_THRS_FAN_BOOST,
  79. value / 1000);
  80. return count;
  81. }
  82. static SENSOR_DEVICE_ATTR(temp1_auto_point1_temp, S_IRUGO | S_IWUSR,
  83. nouveau_hwmon_temp1_auto_point1_temp,
  84. nouveau_hwmon_set_temp1_auto_point1_temp, 0);
  85. static ssize_t
  86. nouveau_hwmon_temp1_auto_point1_temp_hyst(struct device *d,
  87. struct device_attribute *a, char *buf)
  88. {
  89. struct drm_device *dev = dev_get_drvdata(d);
  90. struct nouveau_drm *drm = nouveau_drm(dev);
  91. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  92. return snprintf(buf, PAGE_SIZE, "%d\n",
  93. therm->attr_get(therm, NVKM_THERM_ATTR_THRS_FAN_BOOST_HYST) * 1000);
  94. }
  95. static ssize_t
  96. nouveau_hwmon_set_temp1_auto_point1_temp_hyst(struct device *d,
  97. struct device_attribute *a,
  98. const char *buf, size_t count)
  99. {
  100. struct drm_device *dev = dev_get_drvdata(d);
  101. struct nouveau_drm *drm = nouveau_drm(dev);
  102. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  103. long value;
  104. if (kstrtol(buf, 10, &value) == -EINVAL)
  105. return count;
  106. therm->attr_set(therm, NVKM_THERM_ATTR_THRS_FAN_BOOST_HYST,
  107. value / 1000);
  108. return count;
  109. }
  110. static SENSOR_DEVICE_ATTR(temp1_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
  111. nouveau_hwmon_temp1_auto_point1_temp_hyst,
  112. nouveau_hwmon_set_temp1_auto_point1_temp_hyst, 0);
  113. static ssize_t
  114. nouveau_hwmon_max_temp(struct device *d, struct device_attribute *a, char *buf)
  115. {
  116. struct drm_device *dev = dev_get_drvdata(d);
  117. struct nouveau_drm *drm = nouveau_drm(dev);
  118. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  119. return snprintf(buf, PAGE_SIZE, "%d\n",
  120. therm->attr_get(therm, NVKM_THERM_ATTR_THRS_DOWN_CLK) * 1000);
  121. }
  122. static ssize_t
  123. nouveau_hwmon_set_max_temp(struct device *d, struct device_attribute *a,
  124. const char *buf, size_t count)
  125. {
  126. struct drm_device *dev = dev_get_drvdata(d);
  127. struct nouveau_drm *drm = nouveau_drm(dev);
  128. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  129. long value;
  130. if (kstrtol(buf, 10, &value) == -EINVAL)
  131. return count;
  132. therm->attr_set(therm, NVKM_THERM_ATTR_THRS_DOWN_CLK, value / 1000);
  133. return count;
  134. }
  135. static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, nouveau_hwmon_max_temp,
  136. nouveau_hwmon_set_max_temp,
  137. 0);
  138. static ssize_t
  139. nouveau_hwmon_max_temp_hyst(struct device *d, struct device_attribute *a,
  140. char *buf)
  141. {
  142. struct drm_device *dev = dev_get_drvdata(d);
  143. struct nouveau_drm *drm = nouveau_drm(dev);
  144. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  145. return snprintf(buf, PAGE_SIZE, "%d\n",
  146. therm->attr_get(therm, NVKM_THERM_ATTR_THRS_DOWN_CLK_HYST) * 1000);
  147. }
  148. static ssize_t
  149. nouveau_hwmon_set_max_temp_hyst(struct device *d, struct device_attribute *a,
  150. const char *buf, size_t count)
  151. {
  152. struct drm_device *dev = dev_get_drvdata(d);
  153. struct nouveau_drm *drm = nouveau_drm(dev);
  154. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  155. long value;
  156. if (kstrtol(buf, 10, &value) == -EINVAL)
  157. return count;
  158. therm->attr_set(therm, NVKM_THERM_ATTR_THRS_DOWN_CLK_HYST,
  159. value / 1000);
  160. return count;
  161. }
  162. static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
  163. nouveau_hwmon_max_temp_hyst,
  164. nouveau_hwmon_set_max_temp_hyst, 0);
  165. static ssize_t
  166. nouveau_hwmon_critical_temp(struct device *d, struct device_attribute *a,
  167. char *buf)
  168. {
  169. struct drm_device *dev = dev_get_drvdata(d);
  170. struct nouveau_drm *drm = nouveau_drm(dev);
  171. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  172. return snprintf(buf, PAGE_SIZE, "%d\n",
  173. therm->attr_get(therm, NVKM_THERM_ATTR_THRS_CRITICAL) * 1000);
  174. }
  175. static ssize_t
  176. nouveau_hwmon_set_critical_temp(struct device *d, struct device_attribute *a,
  177. const char *buf,
  178. size_t count)
  179. {
  180. struct drm_device *dev = dev_get_drvdata(d);
  181. struct nouveau_drm *drm = nouveau_drm(dev);
  182. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  183. long value;
  184. if (kstrtol(buf, 10, &value) == -EINVAL)
  185. return count;
  186. therm->attr_set(therm, NVKM_THERM_ATTR_THRS_CRITICAL, value / 1000);
  187. return count;
  188. }
  189. static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO | S_IWUSR,
  190. nouveau_hwmon_critical_temp,
  191. nouveau_hwmon_set_critical_temp,
  192. 0);
  193. static ssize_t
  194. nouveau_hwmon_critical_temp_hyst(struct device *d, struct device_attribute *a,
  195. char *buf)
  196. {
  197. struct drm_device *dev = dev_get_drvdata(d);
  198. struct nouveau_drm *drm = nouveau_drm(dev);
  199. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  200. return snprintf(buf, PAGE_SIZE, "%d\n",
  201. therm->attr_get(therm, NVKM_THERM_ATTR_THRS_CRITICAL_HYST) * 1000);
  202. }
  203. static ssize_t
  204. nouveau_hwmon_set_critical_temp_hyst(struct device *d,
  205. struct device_attribute *a,
  206. const char *buf,
  207. size_t count)
  208. {
  209. struct drm_device *dev = dev_get_drvdata(d);
  210. struct nouveau_drm *drm = nouveau_drm(dev);
  211. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  212. long value;
  213. if (kstrtol(buf, 10, &value) == -EINVAL)
  214. return count;
  215. therm->attr_set(therm, NVKM_THERM_ATTR_THRS_CRITICAL_HYST,
  216. value / 1000);
  217. return count;
  218. }
  219. static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO | S_IWUSR,
  220. nouveau_hwmon_critical_temp_hyst,
  221. nouveau_hwmon_set_critical_temp_hyst, 0);
  222. static ssize_t
  223. nouveau_hwmon_emergency_temp(struct device *d, struct device_attribute *a,
  224. char *buf)
  225. {
  226. struct drm_device *dev = dev_get_drvdata(d);
  227. struct nouveau_drm *drm = nouveau_drm(dev);
  228. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  229. return snprintf(buf, PAGE_SIZE, "%d\n",
  230. therm->attr_get(therm, NVKM_THERM_ATTR_THRS_SHUTDOWN) * 1000);
  231. }
  232. static ssize_t
  233. nouveau_hwmon_set_emergency_temp(struct device *d, struct device_attribute *a,
  234. const char *buf,
  235. size_t count)
  236. {
  237. struct drm_device *dev = dev_get_drvdata(d);
  238. struct nouveau_drm *drm = nouveau_drm(dev);
  239. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  240. long value;
  241. if (kstrtol(buf, 10, &value) == -EINVAL)
  242. return count;
  243. therm->attr_set(therm, NVKM_THERM_ATTR_THRS_SHUTDOWN, value / 1000);
  244. return count;
  245. }
  246. static SENSOR_DEVICE_ATTR(temp1_emergency, S_IRUGO | S_IWUSR,
  247. nouveau_hwmon_emergency_temp,
  248. nouveau_hwmon_set_emergency_temp,
  249. 0);
  250. static ssize_t
  251. nouveau_hwmon_emergency_temp_hyst(struct device *d, struct device_attribute *a,
  252. char *buf)
  253. {
  254. struct drm_device *dev = dev_get_drvdata(d);
  255. struct nouveau_drm *drm = nouveau_drm(dev);
  256. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  257. return snprintf(buf, PAGE_SIZE, "%d\n",
  258. therm->attr_get(therm, NVKM_THERM_ATTR_THRS_SHUTDOWN_HYST) * 1000);
  259. }
  260. static ssize_t
  261. nouveau_hwmon_set_emergency_temp_hyst(struct device *d,
  262. struct device_attribute *a,
  263. const char *buf,
  264. size_t count)
  265. {
  266. struct drm_device *dev = dev_get_drvdata(d);
  267. struct nouveau_drm *drm = nouveau_drm(dev);
  268. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  269. long value;
  270. if (kstrtol(buf, 10, &value) == -EINVAL)
  271. return count;
  272. therm->attr_set(therm, NVKM_THERM_ATTR_THRS_SHUTDOWN_HYST,
  273. value / 1000);
  274. return count;
  275. }
  276. static SENSOR_DEVICE_ATTR(temp1_emergency_hyst, S_IRUGO | S_IWUSR,
  277. nouveau_hwmon_emergency_temp_hyst,
  278. nouveau_hwmon_set_emergency_temp_hyst,
  279. 0);
  280. static ssize_t nouveau_hwmon_show_name(struct device *dev,
  281. struct device_attribute *attr,
  282. char *buf)
  283. {
  284. return sprintf(buf, "nouveau\n");
  285. }
  286. static SENSOR_DEVICE_ATTR(name, S_IRUGO, nouveau_hwmon_show_name, NULL, 0);
  287. static ssize_t nouveau_hwmon_show_update_rate(struct device *dev,
  288. struct device_attribute *attr,
  289. char *buf)
  290. {
  291. return sprintf(buf, "1000\n");
  292. }
  293. static SENSOR_DEVICE_ATTR(update_rate, S_IRUGO,
  294. nouveau_hwmon_show_update_rate,
  295. NULL, 0);
  296. static ssize_t
  297. nouveau_hwmon_show_fan1_input(struct device *d, struct device_attribute *attr,
  298. char *buf)
  299. {
  300. struct drm_device *dev = dev_get_drvdata(d);
  301. struct nouveau_drm *drm = nouveau_drm(dev);
  302. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  303. return snprintf(buf, PAGE_SIZE, "%d\n", nvkm_therm_fan_sense(therm));
  304. }
  305. static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, nouveau_hwmon_show_fan1_input,
  306. NULL, 0);
  307. static ssize_t
  308. nouveau_hwmon_get_pwm1_enable(struct device *d,
  309. struct device_attribute *a, char *buf)
  310. {
  311. struct drm_device *dev = dev_get_drvdata(d);
  312. struct nouveau_drm *drm = nouveau_drm(dev);
  313. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  314. int ret;
  315. ret = therm->attr_get(therm, NVKM_THERM_ATTR_FAN_MODE);
  316. if (ret < 0)
  317. return ret;
  318. return sprintf(buf, "%i\n", ret);
  319. }
  320. static ssize_t
  321. nouveau_hwmon_set_pwm1_enable(struct device *d, struct device_attribute *a,
  322. const char *buf, size_t count)
  323. {
  324. struct drm_device *dev = dev_get_drvdata(d);
  325. struct nouveau_drm *drm = nouveau_drm(dev);
  326. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  327. long value;
  328. int ret;
  329. ret = kstrtol(buf, 10, &value);
  330. if (ret)
  331. return ret;
  332. ret = therm->attr_set(therm, NVKM_THERM_ATTR_FAN_MODE, value);
  333. if (ret)
  334. return ret;
  335. else
  336. return count;
  337. }
  338. static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
  339. nouveau_hwmon_get_pwm1_enable,
  340. nouveau_hwmon_set_pwm1_enable, 0);
  341. static ssize_t
  342. nouveau_hwmon_get_pwm1(struct device *d, struct device_attribute *a, char *buf)
  343. {
  344. struct drm_device *dev = dev_get_drvdata(d);
  345. struct nouveau_drm *drm = nouveau_drm(dev);
  346. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  347. int ret;
  348. ret = therm->fan_get(therm);
  349. if (ret < 0)
  350. return ret;
  351. return sprintf(buf, "%i\n", ret);
  352. }
  353. static ssize_t
  354. nouveau_hwmon_set_pwm1(struct device *d, struct device_attribute *a,
  355. const char *buf, size_t count)
  356. {
  357. struct drm_device *dev = dev_get_drvdata(d);
  358. struct nouveau_drm *drm = nouveau_drm(dev);
  359. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  360. int ret = -ENODEV;
  361. long value;
  362. if (kstrtol(buf, 10, &value) == -EINVAL)
  363. return -EINVAL;
  364. ret = therm->fan_set(therm, value);
  365. if (ret)
  366. return ret;
  367. return count;
  368. }
  369. static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR,
  370. nouveau_hwmon_get_pwm1,
  371. nouveau_hwmon_set_pwm1, 0);
  372. static ssize_t
  373. nouveau_hwmon_get_pwm1_min(struct device *d,
  374. struct device_attribute *a, char *buf)
  375. {
  376. struct drm_device *dev = dev_get_drvdata(d);
  377. struct nouveau_drm *drm = nouveau_drm(dev);
  378. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  379. int ret;
  380. ret = therm->attr_get(therm, NVKM_THERM_ATTR_FAN_MIN_DUTY);
  381. if (ret < 0)
  382. return ret;
  383. return sprintf(buf, "%i\n", ret);
  384. }
  385. static ssize_t
  386. nouveau_hwmon_set_pwm1_min(struct device *d, struct device_attribute *a,
  387. const char *buf, size_t count)
  388. {
  389. struct drm_device *dev = dev_get_drvdata(d);
  390. struct nouveau_drm *drm = nouveau_drm(dev);
  391. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  392. long value;
  393. int ret;
  394. if (kstrtol(buf, 10, &value) == -EINVAL)
  395. return -EINVAL;
  396. ret = therm->attr_set(therm, NVKM_THERM_ATTR_FAN_MIN_DUTY, value);
  397. if (ret < 0)
  398. return ret;
  399. return count;
  400. }
  401. static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO | S_IWUSR,
  402. nouveau_hwmon_get_pwm1_min,
  403. nouveau_hwmon_set_pwm1_min, 0);
  404. static ssize_t
  405. nouveau_hwmon_get_pwm1_max(struct device *d,
  406. struct device_attribute *a, char *buf)
  407. {
  408. struct drm_device *dev = dev_get_drvdata(d);
  409. struct nouveau_drm *drm = nouveau_drm(dev);
  410. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  411. int ret;
  412. ret = therm->attr_get(therm, NVKM_THERM_ATTR_FAN_MAX_DUTY);
  413. if (ret < 0)
  414. return ret;
  415. return sprintf(buf, "%i\n", ret);
  416. }
  417. static ssize_t
  418. nouveau_hwmon_set_pwm1_max(struct device *d, struct device_attribute *a,
  419. const char *buf, size_t count)
  420. {
  421. struct drm_device *dev = dev_get_drvdata(d);
  422. struct nouveau_drm *drm = nouveau_drm(dev);
  423. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  424. long value;
  425. int ret;
  426. if (kstrtol(buf, 10, &value) == -EINVAL)
  427. return -EINVAL;
  428. ret = therm->attr_set(therm, NVKM_THERM_ATTR_FAN_MAX_DUTY, value);
  429. if (ret < 0)
  430. return ret;
  431. return count;
  432. }
  433. static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO | S_IWUSR,
  434. nouveau_hwmon_get_pwm1_max,
  435. nouveau_hwmon_set_pwm1_max, 0);
  436. static ssize_t
  437. nouveau_hwmon_get_in0_input(struct device *d,
  438. struct device_attribute *a, char *buf)
  439. {
  440. struct drm_device *dev = dev_get_drvdata(d);
  441. struct nouveau_drm *drm = nouveau_drm(dev);
  442. struct nvkm_volt *volt = nvxx_volt(&drm->device);
  443. int ret;
  444. ret = nvkm_volt_get(volt);
  445. if (ret < 0)
  446. return ret;
  447. return sprintf(buf, "%i\n", ret / 1000);
  448. }
  449. static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO,
  450. nouveau_hwmon_get_in0_input, NULL, 0);
  451. static ssize_t
  452. nouveau_hwmon_get_in0_min(struct device *d,
  453. struct device_attribute *a, char *buf)
  454. {
  455. struct drm_device *dev = dev_get_drvdata(d);
  456. struct nouveau_drm *drm = nouveau_drm(dev);
  457. struct nvkm_volt *volt = nvxx_volt(&drm->device);
  458. if (!volt || !volt->min_uv)
  459. return -ENODEV;
  460. return sprintf(buf, "%i\n", volt->min_uv / 1000);
  461. }
  462. static SENSOR_DEVICE_ATTR(in0_min, S_IRUGO,
  463. nouveau_hwmon_get_in0_min, NULL, 0);
  464. static ssize_t
  465. nouveau_hwmon_get_in0_max(struct device *d,
  466. struct device_attribute *a, char *buf)
  467. {
  468. struct drm_device *dev = dev_get_drvdata(d);
  469. struct nouveau_drm *drm = nouveau_drm(dev);
  470. struct nvkm_volt *volt = nvxx_volt(&drm->device);
  471. if (!volt || !volt->max_uv)
  472. return -ENODEV;
  473. return sprintf(buf, "%i\n", volt->max_uv / 1000);
  474. }
  475. static SENSOR_DEVICE_ATTR(in0_max, S_IRUGO,
  476. nouveau_hwmon_get_in0_max, NULL, 0);
  477. static ssize_t
  478. nouveau_hwmon_get_in0_label(struct device *d,
  479. struct device_attribute *a, char *buf)
  480. {
  481. return sprintf(buf, "GPU core\n");
  482. }
  483. static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO,
  484. nouveau_hwmon_get_in0_label, NULL, 0);
  485. static ssize_t
  486. nouveau_hwmon_get_power1_input(struct device *d, struct device_attribute *a,
  487. char *buf)
  488. {
  489. struct drm_device *dev = dev_get_drvdata(d);
  490. struct nouveau_drm *drm = nouveau_drm(dev);
  491. struct nvkm_iccsense *iccsense = nvxx_iccsense(&drm->device);
  492. int result = nvkm_iccsense_read_all(iccsense);
  493. if (result < 0)
  494. return result;
  495. return sprintf(buf, "%i\n", result);
  496. }
  497. static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO,
  498. nouveau_hwmon_get_power1_input, NULL, 0);
  499. static struct attribute *hwmon_default_attributes[] = {
  500. &sensor_dev_attr_name.dev_attr.attr,
  501. &sensor_dev_attr_update_rate.dev_attr.attr,
  502. NULL
  503. };
  504. static struct attribute *hwmon_temp_attributes[] = {
  505. &sensor_dev_attr_temp1_input.dev_attr.attr,
  506. &sensor_dev_attr_temp1_auto_point1_pwm.dev_attr.attr,
  507. &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
  508. &sensor_dev_attr_temp1_auto_point1_temp_hyst.dev_attr.attr,
  509. &sensor_dev_attr_temp1_max.dev_attr.attr,
  510. &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
  511. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  512. &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
  513. &sensor_dev_attr_temp1_emergency.dev_attr.attr,
  514. &sensor_dev_attr_temp1_emergency_hyst.dev_attr.attr,
  515. NULL
  516. };
  517. static struct attribute *hwmon_fan_rpm_attributes[] = {
  518. &sensor_dev_attr_fan1_input.dev_attr.attr,
  519. NULL
  520. };
  521. static struct attribute *hwmon_pwm_fan_attributes[] = {
  522. &sensor_dev_attr_pwm1_enable.dev_attr.attr,
  523. &sensor_dev_attr_pwm1.dev_attr.attr,
  524. &sensor_dev_attr_pwm1_min.dev_attr.attr,
  525. &sensor_dev_attr_pwm1_max.dev_attr.attr,
  526. NULL
  527. };
  528. static struct attribute *hwmon_in0_attributes[] = {
  529. &sensor_dev_attr_in0_input.dev_attr.attr,
  530. &sensor_dev_attr_in0_min.dev_attr.attr,
  531. &sensor_dev_attr_in0_max.dev_attr.attr,
  532. &sensor_dev_attr_in0_label.dev_attr.attr,
  533. NULL
  534. };
  535. static struct attribute *hwmon_power_attributes[] = {
  536. &sensor_dev_attr_power1_input.dev_attr.attr,
  537. NULL
  538. };
  539. static const struct attribute_group hwmon_default_attrgroup = {
  540. .attrs = hwmon_default_attributes,
  541. };
  542. static const struct attribute_group hwmon_temp_attrgroup = {
  543. .attrs = hwmon_temp_attributes,
  544. };
  545. static const struct attribute_group hwmon_fan_rpm_attrgroup = {
  546. .attrs = hwmon_fan_rpm_attributes,
  547. };
  548. static const struct attribute_group hwmon_pwm_fan_attrgroup = {
  549. .attrs = hwmon_pwm_fan_attributes,
  550. };
  551. static const struct attribute_group hwmon_in0_attrgroup = {
  552. .attrs = hwmon_in0_attributes,
  553. };
  554. static const struct attribute_group hwmon_power_attrgroup = {
  555. .attrs = hwmon_power_attributes,
  556. };
  557. #endif
  558. int
  559. nouveau_hwmon_init(struct drm_device *dev)
  560. {
  561. #if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
  562. struct nouveau_drm *drm = nouveau_drm(dev);
  563. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  564. struct nvkm_volt *volt = nvxx_volt(&drm->device);
  565. struct nvkm_iccsense *iccsense = nvxx_iccsense(&drm->device);
  566. struct nouveau_hwmon *hwmon;
  567. struct device *hwmon_dev;
  568. int ret = 0;
  569. hwmon = drm->hwmon = kzalloc(sizeof(*hwmon), GFP_KERNEL);
  570. if (!hwmon)
  571. return -ENOMEM;
  572. hwmon->dev = dev;
  573. hwmon_dev = hwmon_device_register(dev->dev);
  574. if (IS_ERR(hwmon_dev)) {
  575. ret = PTR_ERR(hwmon_dev);
  576. NV_ERROR(drm, "Unable to register hwmon device: %d\n", ret);
  577. return ret;
  578. }
  579. dev_set_drvdata(hwmon_dev, dev);
  580. /* set the default attributes */
  581. ret = sysfs_create_group(&hwmon_dev->kobj, &hwmon_default_attrgroup);
  582. if (ret)
  583. goto error;
  584. if (therm && therm->attr_get && therm->attr_set) {
  585. /* if the card has a working thermal sensor */
  586. if (nvkm_therm_temp_get(therm) >= 0) {
  587. ret = sysfs_create_group(&hwmon_dev->kobj, &hwmon_temp_attrgroup);
  588. if (ret)
  589. goto error;
  590. }
  591. /* if the card has a pwm fan */
  592. /*XXX: incorrect, need better detection for this, some boards have
  593. * the gpio entries for pwm fan control even when there's no
  594. * actual fan connected to it... therm table? */
  595. if (therm->fan_get && therm->fan_get(therm) >= 0) {
  596. ret = sysfs_create_group(&hwmon_dev->kobj,
  597. &hwmon_pwm_fan_attrgroup);
  598. if (ret)
  599. goto error;
  600. }
  601. }
  602. /* if the card can read the fan rpm */
  603. if (therm && nvkm_therm_fan_sense(therm) >= 0) {
  604. ret = sysfs_create_group(&hwmon_dev->kobj,
  605. &hwmon_fan_rpm_attrgroup);
  606. if (ret)
  607. goto error;
  608. }
  609. if (volt && nvkm_volt_get(volt) >= 0) {
  610. ret = sysfs_create_group(&hwmon_dev->kobj,
  611. &hwmon_in0_attrgroup);
  612. if (ret)
  613. goto error;
  614. }
  615. if (iccsense && iccsense->data_valid && !list_empty(&iccsense->rails)) {
  616. ret = sysfs_create_group(&hwmon_dev->kobj,
  617. &hwmon_power_attrgroup);
  618. if (ret)
  619. goto error;
  620. }
  621. hwmon->hwmon = hwmon_dev;
  622. return 0;
  623. error:
  624. NV_ERROR(drm, "Unable to create some hwmon sysfs files: %d\n", ret);
  625. hwmon_device_unregister(hwmon_dev);
  626. hwmon->hwmon = NULL;
  627. return ret;
  628. #else
  629. return 0;
  630. #endif
  631. }
  632. void
  633. nouveau_hwmon_fini(struct drm_device *dev)
  634. {
  635. #if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
  636. struct nouveau_hwmon *hwmon = nouveau_hwmon(dev);
  637. if (hwmon->hwmon) {
  638. sysfs_remove_group(&hwmon->hwmon->kobj, &hwmon_default_attrgroup);
  639. sysfs_remove_group(&hwmon->hwmon->kobj, &hwmon_temp_attrgroup);
  640. sysfs_remove_group(&hwmon->hwmon->kobj, &hwmon_pwm_fan_attrgroup);
  641. sysfs_remove_group(&hwmon->hwmon->kobj, &hwmon_fan_rpm_attrgroup);
  642. sysfs_remove_group(&hwmon->hwmon->kobj, &hwmon_in0_attrgroup);
  643. sysfs_remove_group(&hwmon->hwmon->kobj, &hwmon_power_attrgroup);
  644. hwmon_device_unregister(hwmon->hwmon);
  645. }
  646. nouveau_drm(dev)->hwmon = NULL;
  647. kfree(hwmon);
  648. #endif
  649. }