nouveau_hwmon.c 19 KB

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