nouveau_hwmon.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  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_drm.h"
  32. #include "nouveau_hwmon.h"
  33. #if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
  34. static ssize_t
  35. nouveau_hwmon_show_temp(struct device *d, struct device_attribute *a, char *buf)
  36. {
  37. struct drm_device *dev = dev_get_drvdata(d);
  38. struct nouveau_drm *drm = nouveau_drm(dev);
  39. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  40. int temp = nvkm_therm_temp_get(therm);
  41. if (temp < 0)
  42. return temp;
  43. return snprintf(buf, PAGE_SIZE, "%d\n", temp * 1000);
  44. }
  45. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, nouveau_hwmon_show_temp,
  46. NULL, 0);
  47. static ssize_t
  48. nouveau_hwmon_show_temp1_auto_point1_pwm(struct device *d,
  49. struct device_attribute *a, char *buf)
  50. {
  51. return snprintf(buf, PAGE_SIZE, "%d\n", 100);
  52. }
  53. static SENSOR_DEVICE_ATTR(temp1_auto_point1_pwm, S_IRUGO,
  54. nouveau_hwmon_show_temp1_auto_point1_pwm, NULL, 0);
  55. static ssize_t
  56. nouveau_hwmon_temp1_auto_point1_temp(struct device *d,
  57. struct device_attribute *a, char *buf)
  58. {
  59. struct drm_device *dev = dev_get_drvdata(d);
  60. struct nouveau_drm *drm = nouveau_drm(dev);
  61. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  62. return snprintf(buf, PAGE_SIZE, "%d\n",
  63. therm->attr_get(therm, NVKM_THERM_ATTR_THRS_FAN_BOOST) * 1000);
  64. }
  65. static ssize_t
  66. nouveau_hwmon_set_temp1_auto_point1_temp(struct device *d,
  67. struct device_attribute *a,
  68. const char *buf, size_t count)
  69. {
  70. struct drm_device *dev = dev_get_drvdata(d);
  71. struct nouveau_drm *drm = nouveau_drm(dev);
  72. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  73. long value;
  74. if (kstrtol(buf, 10, &value) == -EINVAL)
  75. return count;
  76. therm->attr_set(therm, NVKM_THERM_ATTR_THRS_FAN_BOOST,
  77. value / 1000);
  78. return count;
  79. }
  80. static SENSOR_DEVICE_ATTR(temp1_auto_point1_temp, S_IRUGO | S_IWUSR,
  81. nouveau_hwmon_temp1_auto_point1_temp,
  82. nouveau_hwmon_set_temp1_auto_point1_temp, 0);
  83. static ssize_t
  84. nouveau_hwmon_temp1_auto_point1_temp_hyst(struct device *d,
  85. struct device_attribute *a, char *buf)
  86. {
  87. struct drm_device *dev = dev_get_drvdata(d);
  88. struct nouveau_drm *drm = nouveau_drm(dev);
  89. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  90. return snprintf(buf, PAGE_SIZE, "%d\n",
  91. therm->attr_get(therm, NVKM_THERM_ATTR_THRS_FAN_BOOST_HYST) * 1000);
  92. }
  93. static ssize_t
  94. nouveau_hwmon_set_temp1_auto_point1_temp_hyst(struct device *d,
  95. struct device_attribute *a,
  96. const char *buf, size_t count)
  97. {
  98. struct drm_device *dev = dev_get_drvdata(d);
  99. struct nouveau_drm *drm = nouveau_drm(dev);
  100. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  101. long value;
  102. if (kstrtol(buf, 10, &value) == -EINVAL)
  103. return count;
  104. therm->attr_set(therm, NVKM_THERM_ATTR_THRS_FAN_BOOST_HYST,
  105. value / 1000);
  106. return count;
  107. }
  108. static SENSOR_DEVICE_ATTR(temp1_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
  109. nouveau_hwmon_temp1_auto_point1_temp_hyst,
  110. nouveau_hwmon_set_temp1_auto_point1_temp_hyst, 0);
  111. static ssize_t
  112. nouveau_hwmon_max_temp(struct device *d, struct device_attribute *a, char *buf)
  113. {
  114. struct drm_device *dev = dev_get_drvdata(d);
  115. struct nouveau_drm *drm = nouveau_drm(dev);
  116. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  117. return snprintf(buf, PAGE_SIZE, "%d\n",
  118. therm->attr_get(therm, NVKM_THERM_ATTR_THRS_DOWN_CLK) * 1000);
  119. }
  120. static ssize_t
  121. nouveau_hwmon_set_max_temp(struct device *d, struct device_attribute *a,
  122. const char *buf, size_t count)
  123. {
  124. struct drm_device *dev = dev_get_drvdata(d);
  125. struct nouveau_drm *drm = nouveau_drm(dev);
  126. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  127. long value;
  128. if (kstrtol(buf, 10, &value) == -EINVAL)
  129. return count;
  130. therm->attr_set(therm, NVKM_THERM_ATTR_THRS_DOWN_CLK, value / 1000);
  131. return count;
  132. }
  133. static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, nouveau_hwmon_max_temp,
  134. nouveau_hwmon_set_max_temp,
  135. 0);
  136. static ssize_t
  137. nouveau_hwmon_max_temp_hyst(struct device *d, struct device_attribute *a,
  138. char *buf)
  139. {
  140. struct drm_device *dev = dev_get_drvdata(d);
  141. struct nouveau_drm *drm = nouveau_drm(dev);
  142. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  143. return snprintf(buf, PAGE_SIZE, "%d\n",
  144. therm->attr_get(therm, NVKM_THERM_ATTR_THRS_DOWN_CLK_HYST) * 1000);
  145. }
  146. static ssize_t
  147. nouveau_hwmon_set_max_temp_hyst(struct device *d, struct device_attribute *a,
  148. const char *buf, size_t count)
  149. {
  150. struct drm_device *dev = dev_get_drvdata(d);
  151. struct nouveau_drm *drm = nouveau_drm(dev);
  152. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  153. long value;
  154. if (kstrtol(buf, 10, &value) == -EINVAL)
  155. return count;
  156. therm->attr_set(therm, NVKM_THERM_ATTR_THRS_DOWN_CLK_HYST,
  157. value / 1000);
  158. return count;
  159. }
  160. static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
  161. nouveau_hwmon_max_temp_hyst,
  162. nouveau_hwmon_set_max_temp_hyst, 0);
  163. static ssize_t
  164. nouveau_hwmon_critical_temp(struct device *d, struct device_attribute *a,
  165. char *buf)
  166. {
  167. struct drm_device *dev = dev_get_drvdata(d);
  168. struct nouveau_drm *drm = nouveau_drm(dev);
  169. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  170. return snprintf(buf, PAGE_SIZE, "%d\n",
  171. therm->attr_get(therm, NVKM_THERM_ATTR_THRS_CRITICAL) * 1000);
  172. }
  173. static ssize_t
  174. nouveau_hwmon_set_critical_temp(struct device *d, struct device_attribute *a,
  175. const char *buf,
  176. size_t count)
  177. {
  178. struct drm_device *dev = dev_get_drvdata(d);
  179. struct nouveau_drm *drm = nouveau_drm(dev);
  180. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  181. long value;
  182. if (kstrtol(buf, 10, &value) == -EINVAL)
  183. return count;
  184. therm->attr_set(therm, NVKM_THERM_ATTR_THRS_CRITICAL, value / 1000);
  185. return count;
  186. }
  187. static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO | S_IWUSR,
  188. nouveau_hwmon_critical_temp,
  189. nouveau_hwmon_set_critical_temp,
  190. 0);
  191. static ssize_t
  192. nouveau_hwmon_critical_temp_hyst(struct device *d, struct device_attribute *a,
  193. char *buf)
  194. {
  195. struct drm_device *dev = dev_get_drvdata(d);
  196. struct nouveau_drm *drm = nouveau_drm(dev);
  197. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  198. return snprintf(buf, PAGE_SIZE, "%d\n",
  199. therm->attr_get(therm, NVKM_THERM_ATTR_THRS_CRITICAL_HYST) * 1000);
  200. }
  201. static ssize_t
  202. nouveau_hwmon_set_critical_temp_hyst(struct device *d,
  203. struct device_attribute *a,
  204. const char *buf,
  205. size_t count)
  206. {
  207. struct drm_device *dev = dev_get_drvdata(d);
  208. struct nouveau_drm *drm = nouveau_drm(dev);
  209. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  210. long value;
  211. if (kstrtol(buf, 10, &value) == -EINVAL)
  212. return count;
  213. therm->attr_set(therm, NVKM_THERM_ATTR_THRS_CRITICAL_HYST,
  214. value / 1000);
  215. return count;
  216. }
  217. static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO | S_IWUSR,
  218. nouveau_hwmon_critical_temp_hyst,
  219. nouveau_hwmon_set_critical_temp_hyst, 0);
  220. static ssize_t
  221. nouveau_hwmon_emergency_temp(struct device *d, struct device_attribute *a,
  222. char *buf)
  223. {
  224. struct drm_device *dev = dev_get_drvdata(d);
  225. struct nouveau_drm *drm = nouveau_drm(dev);
  226. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  227. return snprintf(buf, PAGE_SIZE, "%d\n",
  228. therm->attr_get(therm, NVKM_THERM_ATTR_THRS_SHUTDOWN) * 1000);
  229. }
  230. static ssize_t
  231. nouveau_hwmon_set_emergency_temp(struct device *d, struct device_attribute *a,
  232. const char *buf,
  233. size_t count)
  234. {
  235. struct drm_device *dev = dev_get_drvdata(d);
  236. struct nouveau_drm *drm = nouveau_drm(dev);
  237. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  238. long value;
  239. if (kstrtol(buf, 10, &value) == -EINVAL)
  240. return count;
  241. therm->attr_set(therm, NVKM_THERM_ATTR_THRS_SHUTDOWN, value / 1000);
  242. return count;
  243. }
  244. static SENSOR_DEVICE_ATTR(temp1_emergency, S_IRUGO | S_IWUSR,
  245. nouveau_hwmon_emergency_temp,
  246. nouveau_hwmon_set_emergency_temp,
  247. 0);
  248. static ssize_t
  249. nouveau_hwmon_emergency_temp_hyst(struct device *d, struct device_attribute *a,
  250. char *buf)
  251. {
  252. struct drm_device *dev = dev_get_drvdata(d);
  253. struct nouveau_drm *drm = nouveau_drm(dev);
  254. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  255. return snprintf(buf, PAGE_SIZE, "%d\n",
  256. therm->attr_get(therm, NVKM_THERM_ATTR_THRS_SHUTDOWN_HYST) * 1000);
  257. }
  258. static ssize_t
  259. nouveau_hwmon_set_emergency_temp_hyst(struct device *d,
  260. struct device_attribute *a,
  261. const char *buf,
  262. size_t count)
  263. {
  264. struct drm_device *dev = dev_get_drvdata(d);
  265. struct nouveau_drm *drm = nouveau_drm(dev);
  266. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  267. long value;
  268. if (kstrtol(buf, 10, &value) == -EINVAL)
  269. return count;
  270. therm->attr_set(therm, NVKM_THERM_ATTR_THRS_SHUTDOWN_HYST,
  271. value / 1000);
  272. return count;
  273. }
  274. static SENSOR_DEVICE_ATTR(temp1_emergency_hyst, S_IRUGO | S_IWUSR,
  275. nouveau_hwmon_emergency_temp_hyst,
  276. nouveau_hwmon_set_emergency_temp_hyst,
  277. 0);
  278. static ssize_t nouveau_hwmon_show_name(struct device *dev,
  279. struct device_attribute *attr,
  280. char *buf)
  281. {
  282. return sprintf(buf, "nouveau\n");
  283. }
  284. static SENSOR_DEVICE_ATTR(name, S_IRUGO, nouveau_hwmon_show_name, NULL, 0);
  285. static ssize_t nouveau_hwmon_show_update_rate(struct device *dev,
  286. struct device_attribute *attr,
  287. char *buf)
  288. {
  289. return sprintf(buf, "1000\n");
  290. }
  291. static SENSOR_DEVICE_ATTR(update_rate, S_IRUGO,
  292. nouveau_hwmon_show_update_rate,
  293. NULL, 0);
  294. static ssize_t
  295. nouveau_hwmon_show_fan1_input(struct device *d, struct device_attribute *attr,
  296. char *buf)
  297. {
  298. struct drm_device *dev = dev_get_drvdata(d);
  299. struct nouveau_drm *drm = nouveau_drm(dev);
  300. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  301. return snprintf(buf, PAGE_SIZE, "%d\n", nvkm_therm_fan_sense(therm));
  302. }
  303. static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, nouveau_hwmon_show_fan1_input,
  304. NULL, 0);
  305. static ssize_t
  306. nouveau_hwmon_get_pwm1_enable(struct device *d,
  307. struct device_attribute *a, char *buf)
  308. {
  309. struct drm_device *dev = dev_get_drvdata(d);
  310. struct nouveau_drm *drm = nouveau_drm(dev);
  311. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  312. int ret;
  313. ret = therm->attr_get(therm, NVKM_THERM_ATTR_FAN_MODE);
  314. if (ret < 0)
  315. return ret;
  316. return sprintf(buf, "%i\n", ret);
  317. }
  318. static ssize_t
  319. nouveau_hwmon_set_pwm1_enable(struct device *d, struct device_attribute *a,
  320. const char *buf, size_t count)
  321. {
  322. struct drm_device *dev = dev_get_drvdata(d);
  323. struct nouveau_drm *drm = nouveau_drm(dev);
  324. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  325. long value;
  326. int ret;
  327. ret = kstrtol(buf, 10, &value);
  328. if (ret)
  329. return ret;
  330. ret = therm->attr_set(therm, NVKM_THERM_ATTR_FAN_MODE, value);
  331. if (ret)
  332. return ret;
  333. else
  334. return count;
  335. }
  336. static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
  337. nouveau_hwmon_get_pwm1_enable,
  338. nouveau_hwmon_set_pwm1_enable, 0);
  339. static ssize_t
  340. nouveau_hwmon_get_pwm1(struct device *d, struct device_attribute *a, char *buf)
  341. {
  342. struct drm_device *dev = dev_get_drvdata(d);
  343. struct nouveau_drm *drm = nouveau_drm(dev);
  344. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  345. int ret;
  346. ret = therm->fan_get(therm);
  347. if (ret < 0)
  348. return ret;
  349. return sprintf(buf, "%i\n", ret);
  350. }
  351. static ssize_t
  352. nouveau_hwmon_set_pwm1(struct device *d, struct device_attribute *a,
  353. const char *buf, size_t count)
  354. {
  355. struct drm_device *dev = dev_get_drvdata(d);
  356. struct nouveau_drm *drm = nouveau_drm(dev);
  357. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  358. int ret = -ENODEV;
  359. long value;
  360. if (kstrtol(buf, 10, &value) == -EINVAL)
  361. return -EINVAL;
  362. ret = therm->fan_set(therm, value);
  363. if (ret)
  364. return ret;
  365. return count;
  366. }
  367. static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR,
  368. nouveau_hwmon_get_pwm1,
  369. nouveau_hwmon_set_pwm1, 0);
  370. static ssize_t
  371. nouveau_hwmon_get_pwm1_min(struct device *d,
  372. struct device_attribute *a, char *buf)
  373. {
  374. struct drm_device *dev = dev_get_drvdata(d);
  375. struct nouveau_drm *drm = nouveau_drm(dev);
  376. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  377. int ret;
  378. ret = therm->attr_get(therm, NVKM_THERM_ATTR_FAN_MIN_DUTY);
  379. if (ret < 0)
  380. return ret;
  381. return sprintf(buf, "%i\n", ret);
  382. }
  383. static ssize_t
  384. nouveau_hwmon_set_pwm1_min(struct device *d, struct device_attribute *a,
  385. const char *buf, size_t count)
  386. {
  387. struct drm_device *dev = dev_get_drvdata(d);
  388. struct nouveau_drm *drm = nouveau_drm(dev);
  389. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  390. long value;
  391. int ret;
  392. if (kstrtol(buf, 10, &value) == -EINVAL)
  393. return -EINVAL;
  394. ret = therm->attr_set(therm, NVKM_THERM_ATTR_FAN_MIN_DUTY, value);
  395. if (ret < 0)
  396. return ret;
  397. return count;
  398. }
  399. static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO | S_IWUSR,
  400. nouveau_hwmon_get_pwm1_min,
  401. nouveau_hwmon_set_pwm1_min, 0);
  402. static ssize_t
  403. nouveau_hwmon_get_pwm1_max(struct device *d,
  404. struct device_attribute *a, char *buf)
  405. {
  406. struct drm_device *dev = dev_get_drvdata(d);
  407. struct nouveau_drm *drm = nouveau_drm(dev);
  408. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  409. int ret;
  410. ret = therm->attr_get(therm, NVKM_THERM_ATTR_FAN_MAX_DUTY);
  411. if (ret < 0)
  412. return ret;
  413. return sprintf(buf, "%i\n", ret);
  414. }
  415. static ssize_t
  416. nouveau_hwmon_set_pwm1_max(struct device *d, struct device_attribute *a,
  417. const char *buf, size_t count)
  418. {
  419. struct drm_device *dev = dev_get_drvdata(d);
  420. struct nouveau_drm *drm = nouveau_drm(dev);
  421. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  422. long value;
  423. int ret;
  424. if (kstrtol(buf, 10, &value) == -EINVAL)
  425. return -EINVAL;
  426. ret = therm->attr_set(therm, NVKM_THERM_ATTR_FAN_MAX_DUTY, value);
  427. if (ret < 0)
  428. return ret;
  429. return count;
  430. }
  431. static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO | S_IWUSR,
  432. nouveau_hwmon_get_pwm1_max,
  433. nouveau_hwmon_set_pwm1_max, 0);
  434. static struct attribute *hwmon_default_attributes[] = {
  435. &sensor_dev_attr_name.dev_attr.attr,
  436. &sensor_dev_attr_update_rate.dev_attr.attr,
  437. NULL
  438. };
  439. static struct attribute *hwmon_temp_attributes[] = {
  440. &sensor_dev_attr_temp1_input.dev_attr.attr,
  441. &sensor_dev_attr_temp1_auto_point1_pwm.dev_attr.attr,
  442. &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
  443. &sensor_dev_attr_temp1_auto_point1_temp_hyst.dev_attr.attr,
  444. &sensor_dev_attr_temp1_max.dev_attr.attr,
  445. &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
  446. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  447. &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
  448. &sensor_dev_attr_temp1_emergency.dev_attr.attr,
  449. &sensor_dev_attr_temp1_emergency_hyst.dev_attr.attr,
  450. NULL
  451. };
  452. static struct attribute *hwmon_fan_rpm_attributes[] = {
  453. &sensor_dev_attr_fan1_input.dev_attr.attr,
  454. NULL
  455. };
  456. static struct attribute *hwmon_pwm_fan_attributes[] = {
  457. &sensor_dev_attr_pwm1_enable.dev_attr.attr,
  458. &sensor_dev_attr_pwm1.dev_attr.attr,
  459. &sensor_dev_attr_pwm1_min.dev_attr.attr,
  460. &sensor_dev_attr_pwm1_max.dev_attr.attr,
  461. NULL
  462. };
  463. static const struct attribute_group hwmon_default_attrgroup = {
  464. .attrs = hwmon_default_attributes,
  465. };
  466. static const struct attribute_group hwmon_temp_attrgroup = {
  467. .attrs = hwmon_temp_attributes,
  468. };
  469. static const struct attribute_group hwmon_fan_rpm_attrgroup = {
  470. .attrs = hwmon_fan_rpm_attributes,
  471. };
  472. static const struct attribute_group hwmon_pwm_fan_attrgroup = {
  473. .attrs = hwmon_pwm_fan_attributes,
  474. };
  475. #endif
  476. int
  477. nouveau_hwmon_init(struct drm_device *dev)
  478. {
  479. #if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
  480. struct nouveau_drm *drm = nouveau_drm(dev);
  481. struct nvkm_therm *therm = nvxx_therm(&drm->device);
  482. struct nouveau_hwmon *hwmon;
  483. struct device *hwmon_dev;
  484. int ret = 0;
  485. hwmon = drm->hwmon = kzalloc(sizeof(*hwmon), GFP_KERNEL);
  486. if (!hwmon)
  487. return -ENOMEM;
  488. hwmon->dev = dev;
  489. if (!therm || !therm->attr_get || !therm->attr_set)
  490. return -ENODEV;
  491. hwmon_dev = hwmon_device_register(&dev->pdev->dev);
  492. if (IS_ERR(hwmon_dev)) {
  493. ret = PTR_ERR(hwmon_dev);
  494. NV_ERROR(drm, "Unable to register hwmon device: %d\n", ret);
  495. return ret;
  496. }
  497. dev_set_drvdata(hwmon_dev, dev);
  498. /* set the default attributes */
  499. ret = sysfs_create_group(&hwmon_dev->kobj, &hwmon_default_attrgroup);
  500. if (ret)
  501. goto error;
  502. /* if the card has a working thermal sensor */
  503. if (nvkm_therm_temp_get(therm) >= 0) {
  504. ret = sysfs_create_group(&hwmon_dev->kobj, &hwmon_temp_attrgroup);
  505. if (ret)
  506. goto error;
  507. }
  508. /* if the card has a pwm fan */
  509. /*XXX: incorrect, need better detection for this, some boards have
  510. * the gpio entries for pwm fan control even when there's no
  511. * actual fan connected to it... therm table? */
  512. if (therm->fan_get && therm->fan_get(therm) >= 0) {
  513. ret = sysfs_create_group(&hwmon_dev->kobj,
  514. &hwmon_pwm_fan_attrgroup);
  515. if (ret)
  516. goto error;
  517. }
  518. /* if the card can read the fan rpm */
  519. if (nvkm_therm_fan_sense(therm) >= 0) {
  520. ret = sysfs_create_group(&hwmon_dev->kobj,
  521. &hwmon_fan_rpm_attrgroup);
  522. if (ret)
  523. goto error;
  524. }
  525. hwmon->hwmon = hwmon_dev;
  526. return 0;
  527. error:
  528. NV_ERROR(drm, "Unable to create some hwmon sysfs files: %d\n", ret);
  529. hwmon_device_unregister(hwmon_dev);
  530. hwmon->hwmon = NULL;
  531. return ret;
  532. #else
  533. return 0;
  534. #endif
  535. }
  536. void
  537. nouveau_hwmon_fini(struct drm_device *dev)
  538. {
  539. #if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
  540. struct nouveau_hwmon *hwmon = nouveau_hwmon(dev);
  541. if (hwmon->hwmon) {
  542. sysfs_remove_group(&hwmon->hwmon->kobj, &hwmon_default_attrgroup);
  543. sysfs_remove_group(&hwmon->hwmon->kobj, &hwmon_temp_attrgroup);
  544. sysfs_remove_group(&hwmon->hwmon->kobj, &hwmon_pwm_fan_attrgroup);
  545. sysfs_remove_group(&hwmon->hwmon->kobj, &hwmon_fan_rpm_attrgroup);
  546. hwmon_device_unregister(hwmon->hwmon);
  547. }
  548. nouveau_drm(dev)->hwmon = NULL;
  549. kfree(hwmon);
  550. #endif
  551. }