nouveau_hwmon.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  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 "nouveau_drv.h"
  31. #include "nouveau_hwmon.h"
  32. #include <nvkm/subdev/iccsense.h>
  33. #include <nvkm/subdev/volt.h>
  34. #if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
  35. static ssize_t
  36. nouveau_hwmon_show_temp1_auto_point1_pwm(struct device *d,
  37. struct device_attribute *a, char *buf)
  38. {
  39. return snprintf(buf, PAGE_SIZE, "%d\n", 100);
  40. }
  41. static SENSOR_DEVICE_ATTR(temp1_auto_point1_pwm, 0444,
  42. nouveau_hwmon_show_temp1_auto_point1_pwm, NULL, 0);
  43. static ssize_t
  44. nouveau_hwmon_temp1_auto_point1_temp(struct device *d,
  45. struct device_attribute *a, char *buf)
  46. {
  47. struct drm_device *dev = dev_get_drvdata(d);
  48. struct nouveau_drm *drm = nouveau_drm(dev);
  49. struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
  50. return snprintf(buf, PAGE_SIZE, "%d\n",
  51. therm->attr_get(therm, NVKM_THERM_ATTR_THRS_FAN_BOOST) * 1000);
  52. }
  53. static ssize_t
  54. nouveau_hwmon_set_temp1_auto_point1_temp(struct device *d,
  55. struct device_attribute *a,
  56. const char *buf, size_t count)
  57. {
  58. struct drm_device *dev = dev_get_drvdata(d);
  59. struct nouveau_drm *drm = nouveau_drm(dev);
  60. struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
  61. long value;
  62. if (kstrtol(buf, 10, &value))
  63. return -EINVAL;
  64. therm->attr_set(therm, NVKM_THERM_ATTR_THRS_FAN_BOOST,
  65. value / 1000);
  66. return count;
  67. }
  68. static SENSOR_DEVICE_ATTR(temp1_auto_point1_temp, 0644,
  69. nouveau_hwmon_temp1_auto_point1_temp,
  70. nouveau_hwmon_set_temp1_auto_point1_temp, 0);
  71. static ssize_t
  72. nouveau_hwmon_temp1_auto_point1_temp_hyst(struct device *d,
  73. struct device_attribute *a, char *buf)
  74. {
  75. struct drm_device *dev = dev_get_drvdata(d);
  76. struct nouveau_drm *drm = nouveau_drm(dev);
  77. struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
  78. return snprintf(buf, PAGE_SIZE, "%d\n",
  79. therm->attr_get(therm, NVKM_THERM_ATTR_THRS_FAN_BOOST_HYST) * 1000);
  80. }
  81. static ssize_t
  82. nouveau_hwmon_set_temp1_auto_point1_temp_hyst(struct device *d,
  83. struct device_attribute *a,
  84. const char *buf, size_t count)
  85. {
  86. struct drm_device *dev = dev_get_drvdata(d);
  87. struct nouveau_drm *drm = nouveau_drm(dev);
  88. struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
  89. long value;
  90. if (kstrtol(buf, 10, &value))
  91. return -EINVAL;
  92. therm->attr_set(therm, NVKM_THERM_ATTR_THRS_FAN_BOOST_HYST,
  93. value / 1000);
  94. return count;
  95. }
  96. static SENSOR_DEVICE_ATTR(temp1_auto_point1_temp_hyst, 0644,
  97. nouveau_hwmon_temp1_auto_point1_temp_hyst,
  98. nouveau_hwmon_set_temp1_auto_point1_temp_hyst, 0);
  99. static ssize_t
  100. nouveau_hwmon_get_pwm1_max(struct device *d,
  101. struct device_attribute *a, char *buf)
  102. {
  103. struct drm_device *dev = dev_get_drvdata(d);
  104. struct nouveau_drm *drm = nouveau_drm(dev);
  105. struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
  106. int ret;
  107. ret = therm->attr_get(therm, NVKM_THERM_ATTR_FAN_MAX_DUTY);
  108. if (ret < 0)
  109. return ret;
  110. return sprintf(buf, "%i\n", ret);
  111. }
  112. static ssize_t
  113. nouveau_hwmon_get_pwm1_min(struct device *d,
  114. 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->client.device);
  119. int ret;
  120. ret = therm->attr_get(therm, NVKM_THERM_ATTR_FAN_MIN_DUTY);
  121. if (ret < 0)
  122. return ret;
  123. return sprintf(buf, "%i\n", ret);
  124. }
  125. static ssize_t
  126. nouveau_hwmon_set_pwm1_min(struct device *d, struct device_attribute *a,
  127. const char *buf, size_t count)
  128. {
  129. struct drm_device *dev = dev_get_drvdata(d);
  130. struct nouveau_drm *drm = nouveau_drm(dev);
  131. struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
  132. long value;
  133. int ret;
  134. if (kstrtol(buf, 10, &value))
  135. return -EINVAL;
  136. ret = therm->attr_set(therm, NVKM_THERM_ATTR_FAN_MIN_DUTY, value);
  137. if (ret < 0)
  138. return ret;
  139. return count;
  140. }
  141. static SENSOR_DEVICE_ATTR(pwm1_min, 0644,
  142. nouveau_hwmon_get_pwm1_min,
  143. nouveau_hwmon_set_pwm1_min, 0);
  144. static ssize_t
  145. nouveau_hwmon_set_pwm1_max(struct device *d, struct device_attribute *a,
  146. const char *buf, size_t count)
  147. {
  148. struct drm_device *dev = dev_get_drvdata(d);
  149. struct nouveau_drm *drm = nouveau_drm(dev);
  150. struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
  151. long value;
  152. int ret;
  153. if (kstrtol(buf, 10, &value))
  154. return -EINVAL;
  155. ret = therm->attr_set(therm, NVKM_THERM_ATTR_FAN_MAX_DUTY, value);
  156. if (ret < 0)
  157. return ret;
  158. return count;
  159. }
  160. static SENSOR_DEVICE_ATTR(pwm1_max, 0644,
  161. nouveau_hwmon_get_pwm1_max,
  162. nouveau_hwmon_set_pwm1_max, 0);
  163. static struct attribute *pwm_fan_sensor_attrs[] = {
  164. &sensor_dev_attr_pwm1_min.dev_attr.attr,
  165. &sensor_dev_attr_pwm1_max.dev_attr.attr,
  166. NULL
  167. };
  168. static const struct attribute_group pwm_fan_sensor_group = {
  169. .attrs = pwm_fan_sensor_attrs,
  170. };
  171. static struct attribute *temp1_auto_point_sensor_attrs[] = {
  172. &sensor_dev_attr_temp1_auto_point1_pwm.dev_attr.attr,
  173. &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
  174. &sensor_dev_attr_temp1_auto_point1_temp_hyst.dev_attr.attr,
  175. NULL
  176. };
  177. static const struct attribute_group temp1_auto_point_sensor_group = {
  178. .attrs = temp1_auto_point_sensor_attrs,
  179. };
  180. #define N_ATTR_GROUPS 3
  181. static const u32 nouveau_config_chip[] = {
  182. HWMON_C_UPDATE_INTERVAL,
  183. 0
  184. };
  185. static const u32 nouveau_config_in[] = {
  186. HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_LABEL,
  187. 0
  188. };
  189. static const u32 nouveau_config_temp[] = {
  190. HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_HYST |
  191. HWMON_T_CRIT | HWMON_T_CRIT_HYST | HWMON_T_EMERGENCY |
  192. HWMON_T_EMERGENCY_HYST,
  193. 0
  194. };
  195. static const u32 nouveau_config_fan[] = {
  196. HWMON_F_INPUT,
  197. 0
  198. };
  199. static const u32 nouveau_config_pwm[] = {
  200. HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
  201. 0
  202. };
  203. static const u32 nouveau_config_power[] = {
  204. HWMON_P_INPUT | HWMON_P_CAP_MAX | HWMON_P_CRIT,
  205. 0
  206. };
  207. static const struct hwmon_channel_info nouveau_chip = {
  208. .type = hwmon_chip,
  209. .config = nouveau_config_chip,
  210. };
  211. static const struct hwmon_channel_info nouveau_temp = {
  212. .type = hwmon_temp,
  213. .config = nouveau_config_temp,
  214. };
  215. static const struct hwmon_channel_info nouveau_fan = {
  216. .type = hwmon_fan,
  217. .config = nouveau_config_fan,
  218. };
  219. static const struct hwmon_channel_info nouveau_in = {
  220. .type = hwmon_in,
  221. .config = nouveau_config_in,
  222. };
  223. static const struct hwmon_channel_info nouveau_pwm = {
  224. .type = hwmon_pwm,
  225. .config = nouveau_config_pwm,
  226. };
  227. static const struct hwmon_channel_info nouveau_power = {
  228. .type = hwmon_power,
  229. .config = nouveau_config_power,
  230. };
  231. static const struct hwmon_channel_info *nouveau_info[] = {
  232. &nouveau_chip,
  233. &nouveau_temp,
  234. &nouveau_fan,
  235. &nouveau_in,
  236. &nouveau_pwm,
  237. &nouveau_power,
  238. NULL
  239. };
  240. static umode_t
  241. nouveau_chip_is_visible(const void *data, u32 attr, int channel)
  242. {
  243. switch (attr) {
  244. case hwmon_chip_update_interval:
  245. return 0444;
  246. default:
  247. return 0;
  248. }
  249. }
  250. static umode_t
  251. nouveau_power_is_visible(const void *data, u32 attr, int channel)
  252. {
  253. struct nouveau_drm *drm = nouveau_drm((struct drm_device *)data);
  254. struct nvkm_iccsense *iccsense = nvxx_iccsense(&drm->client.device);
  255. if (!iccsense || !iccsense->data_valid || list_empty(&iccsense->rails))
  256. return 0;
  257. switch (attr) {
  258. case hwmon_power_input:
  259. return 0444;
  260. case hwmon_power_max:
  261. if (iccsense->power_w_max)
  262. return 0444;
  263. return 0;
  264. case hwmon_power_crit:
  265. if (iccsense->power_w_crit)
  266. return 0444;
  267. return 0;
  268. default:
  269. return 0;
  270. }
  271. }
  272. static umode_t
  273. nouveau_temp_is_visible(const void *data, u32 attr, int channel)
  274. {
  275. struct nouveau_drm *drm = nouveau_drm((struct drm_device *)data);
  276. struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
  277. if (!therm || !therm->attr_get || nvkm_therm_temp_get(therm) < 0)
  278. return 0;
  279. switch (attr) {
  280. case hwmon_temp_input:
  281. return 0444;
  282. case hwmon_temp_max:
  283. case hwmon_temp_max_hyst:
  284. case hwmon_temp_crit:
  285. case hwmon_temp_crit_hyst:
  286. case hwmon_temp_emergency:
  287. case hwmon_temp_emergency_hyst:
  288. return 0644;
  289. default:
  290. return 0;
  291. }
  292. }
  293. static umode_t
  294. nouveau_pwm_is_visible(const void *data, u32 attr, int channel)
  295. {
  296. struct nouveau_drm *drm = nouveau_drm((struct drm_device *)data);
  297. struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
  298. if (!therm || !therm->attr_get || !therm->fan_get ||
  299. therm->fan_get(therm) < 0)
  300. return 0;
  301. switch (attr) {
  302. case hwmon_pwm_enable:
  303. case hwmon_pwm_input:
  304. return 0644;
  305. default:
  306. return 0;
  307. }
  308. }
  309. static umode_t
  310. nouveau_input_is_visible(const void *data, u32 attr, int channel)
  311. {
  312. struct nouveau_drm *drm = nouveau_drm((struct drm_device *)data);
  313. struct nvkm_volt *volt = nvxx_volt(&drm->client.device);
  314. if (!volt || nvkm_volt_get(volt) < 0)
  315. return 0;
  316. switch (attr) {
  317. case hwmon_in_input:
  318. case hwmon_in_label:
  319. case hwmon_in_min:
  320. case hwmon_in_max:
  321. return 0444;
  322. default:
  323. return 0;
  324. }
  325. }
  326. static umode_t
  327. nouveau_fan_is_visible(const void *data, u32 attr, int channel)
  328. {
  329. struct nouveau_drm *drm = nouveau_drm((struct drm_device *)data);
  330. struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
  331. if (!therm || !therm->attr_get || nvkm_therm_fan_sense(therm) < 0)
  332. return 0;
  333. switch (attr) {
  334. case hwmon_fan_input:
  335. return 0444;
  336. default:
  337. return 0;
  338. }
  339. }
  340. static int
  341. nouveau_chip_read(struct device *dev, u32 attr, int channel, long *val)
  342. {
  343. switch (attr) {
  344. case hwmon_chip_update_interval:
  345. *val = 1000;
  346. break;
  347. default:
  348. return -EOPNOTSUPP;
  349. }
  350. return 0;
  351. }
  352. static int
  353. nouveau_temp_read(struct device *dev, u32 attr, int channel, long *val)
  354. {
  355. struct drm_device *drm_dev = dev_get_drvdata(dev);
  356. struct nouveau_drm *drm = nouveau_drm(drm_dev);
  357. struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
  358. int ret;
  359. if (!therm || !therm->attr_get)
  360. return -EOPNOTSUPP;
  361. switch (attr) {
  362. case hwmon_temp_input:
  363. if (drm_dev->switch_power_state != DRM_SWITCH_POWER_ON)
  364. return -EINVAL;
  365. ret = nvkm_therm_temp_get(therm);
  366. *val = ret < 0 ? ret : (ret * 1000);
  367. break;
  368. case hwmon_temp_max:
  369. *val = therm->attr_get(therm, NVKM_THERM_ATTR_THRS_DOWN_CLK)
  370. * 1000;
  371. break;
  372. case hwmon_temp_max_hyst:
  373. *val = therm->attr_get(therm, NVKM_THERM_ATTR_THRS_DOWN_CLK_HYST)
  374. * 1000;
  375. break;
  376. case hwmon_temp_crit:
  377. *val = therm->attr_get(therm, NVKM_THERM_ATTR_THRS_CRITICAL)
  378. * 1000;
  379. break;
  380. case hwmon_temp_crit_hyst:
  381. *val = therm->attr_get(therm, NVKM_THERM_ATTR_THRS_CRITICAL_HYST)
  382. * 1000;
  383. break;
  384. case hwmon_temp_emergency:
  385. *val = therm->attr_get(therm, NVKM_THERM_ATTR_THRS_SHUTDOWN)
  386. * 1000;
  387. break;
  388. case hwmon_temp_emergency_hyst:
  389. *val = therm->attr_get(therm, NVKM_THERM_ATTR_THRS_SHUTDOWN_HYST)
  390. * 1000;
  391. break;
  392. default:
  393. return -EOPNOTSUPP;
  394. }
  395. return 0;
  396. }
  397. static int
  398. nouveau_fan_read(struct device *dev, u32 attr, int channel, long *val)
  399. {
  400. struct drm_device *drm_dev = dev_get_drvdata(dev);
  401. struct nouveau_drm *drm = nouveau_drm(drm_dev);
  402. struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
  403. if (!therm)
  404. return -EOPNOTSUPP;
  405. switch (attr) {
  406. case hwmon_fan_input:
  407. if (drm_dev->switch_power_state != DRM_SWITCH_POWER_ON)
  408. return -EINVAL;
  409. *val = nvkm_therm_fan_sense(therm);
  410. break;
  411. default:
  412. return -EOPNOTSUPP;
  413. }
  414. return 0;
  415. }
  416. static int
  417. nouveau_in_read(struct device *dev, u32 attr, int channel, long *val)
  418. {
  419. struct drm_device *drm_dev = dev_get_drvdata(dev);
  420. struct nouveau_drm *drm = nouveau_drm(drm_dev);
  421. struct nvkm_volt *volt = nvxx_volt(&drm->client.device);
  422. int ret;
  423. if (!volt)
  424. return -EOPNOTSUPP;
  425. switch (attr) {
  426. case hwmon_in_input:
  427. if (drm_dev->switch_power_state != DRM_SWITCH_POWER_ON)
  428. return -EINVAL;
  429. ret = nvkm_volt_get(volt);
  430. *val = ret < 0 ? ret : (ret / 1000);
  431. break;
  432. case hwmon_in_min:
  433. *val = volt->min_uv > 0 ? (volt->min_uv / 1000) : -ENODEV;
  434. break;
  435. case hwmon_in_max:
  436. *val = volt->max_uv > 0 ? (volt->max_uv / 1000) : -ENODEV;
  437. break;
  438. default:
  439. return -EOPNOTSUPP;
  440. }
  441. return 0;
  442. }
  443. static int
  444. nouveau_pwm_read(struct device *dev, u32 attr, int channel, long *val)
  445. {
  446. struct drm_device *drm_dev = dev_get_drvdata(dev);
  447. struct nouveau_drm *drm = nouveau_drm(drm_dev);
  448. struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
  449. if (!therm || !therm->attr_get || !therm->fan_get)
  450. return -EOPNOTSUPP;
  451. switch (attr) {
  452. case hwmon_pwm_enable:
  453. *val = therm->attr_get(therm, NVKM_THERM_ATTR_FAN_MODE);
  454. break;
  455. case hwmon_pwm_input:
  456. if (drm_dev->switch_power_state != DRM_SWITCH_POWER_ON)
  457. return -EINVAL;
  458. *val = therm->fan_get(therm);
  459. break;
  460. default:
  461. return -EOPNOTSUPP;
  462. }
  463. return 0;
  464. }
  465. static int
  466. nouveau_power_read(struct device *dev, u32 attr, int channel, long *val)
  467. {
  468. struct drm_device *drm_dev = dev_get_drvdata(dev);
  469. struct nouveau_drm *drm = nouveau_drm(drm_dev);
  470. struct nvkm_iccsense *iccsense = nvxx_iccsense(&drm->client.device);
  471. if (!iccsense)
  472. return -EOPNOTSUPP;
  473. switch (attr) {
  474. case hwmon_power_input:
  475. if (drm_dev->switch_power_state != DRM_SWITCH_POWER_ON)
  476. return -EINVAL;
  477. *val = nvkm_iccsense_read_all(iccsense);
  478. break;
  479. case hwmon_power_max:
  480. *val = iccsense->power_w_max;
  481. break;
  482. case hwmon_power_crit:
  483. *val = iccsense->power_w_crit;
  484. break;
  485. default:
  486. return -EOPNOTSUPP;
  487. }
  488. return 0;
  489. }
  490. static int
  491. nouveau_temp_write(struct device *dev, u32 attr, int channel, long val)
  492. {
  493. struct drm_device *drm_dev = dev_get_drvdata(dev);
  494. struct nouveau_drm *drm = nouveau_drm(drm_dev);
  495. struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
  496. if (!therm || !therm->attr_set)
  497. return -EOPNOTSUPP;
  498. switch (attr) {
  499. case hwmon_temp_max:
  500. return therm->attr_set(therm, NVKM_THERM_ATTR_THRS_DOWN_CLK,
  501. val / 1000);
  502. case hwmon_temp_max_hyst:
  503. return therm->attr_set(therm, NVKM_THERM_ATTR_THRS_DOWN_CLK_HYST,
  504. val / 1000);
  505. case hwmon_temp_crit:
  506. return therm->attr_set(therm, NVKM_THERM_ATTR_THRS_CRITICAL,
  507. val / 1000);
  508. case hwmon_temp_crit_hyst:
  509. return therm->attr_set(therm, NVKM_THERM_ATTR_THRS_CRITICAL_HYST,
  510. val / 1000);
  511. case hwmon_temp_emergency:
  512. return therm->attr_set(therm, NVKM_THERM_ATTR_THRS_SHUTDOWN,
  513. val / 1000);
  514. case hwmon_temp_emergency_hyst:
  515. return therm->attr_set(therm, NVKM_THERM_ATTR_THRS_SHUTDOWN_HYST,
  516. val / 1000);
  517. default:
  518. return -EOPNOTSUPP;
  519. }
  520. }
  521. static int
  522. nouveau_pwm_write(struct device *dev, u32 attr, int channel, long val)
  523. {
  524. struct drm_device *drm_dev = dev_get_drvdata(dev);
  525. struct nouveau_drm *drm = nouveau_drm(drm_dev);
  526. struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
  527. if (!therm || !therm->attr_set)
  528. return -EOPNOTSUPP;
  529. switch (attr) {
  530. case hwmon_pwm_input:
  531. return therm->fan_set(therm, val);
  532. case hwmon_pwm_enable:
  533. return therm->attr_set(therm, NVKM_THERM_ATTR_FAN_MODE, val);
  534. default:
  535. return -EOPNOTSUPP;
  536. }
  537. }
  538. static umode_t
  539. nouveau_is_visible(const void *data, enum hwmon_sensor_types type, u32 attr,
  540. int channel)
  541. {
  542. switch (type) {
  543. case hwmon_chip:
  544. return nouveau_chip_is_visible(data, attr, channel);
  545. case hwmon_temp:
  546. return nouveau_temp_is_visible(data, attr, channel);
  547. case hwmon_fan:
  548. return nouveau_fan_is_visible(data, attr, channel);
  549. case hwmon_in:
  550. return nouveau_input_is_visible(data, attr, channel);
  551. case hwmon_pwm:
  552. return nouveau_pwm_is_visible(data, attr, channel);
  553. case hwmon_power:
  554. return nouveau_power_is_visible(data, attr, channel);
  555. default:
  556. return 0;
  557. }
  558. }
  559. static const char input_label[] = "GPU core";
  560. static int
  561. nouveau_read_string(struct device *dev, enum hwmon_sensor_types type, u32 attr,
  562. int channel, const char **buf)
  563. {
  564. if (type == hwmon_in && attr == hwmon_in_label) {
  565. *buf = input_label;
  566. return 0;
  567. }
  568. return -EOPNOTSUPP;
  569. }
  570. static int
  571. nouveau_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
  572. int channel, long *val)
  573. {
  574. switch (type) {
  575. case hwmon_chip:
  576. return nouveau_chip_read(dev, attr, channel, val);
  577. case hwmon_temp:
  578. return nouveau_temp_read(dev, attr, channel, val);
  579. case hwmon_fan:
  580. return nouveau_fan_read(dev, attr, channel, val);
  581. case hwmon_in:
  582. return nouveau_in_read(dev, attr, channel, val);
  583. case hwmon_pwm:
  584. return nouveau_pwm_read(dev, attr, channel, val);
  585. case hwmon_power:
  586. return nouveau_power_read(dev, attr, channel, val);
  587. default:
  588. return -EOPNOTSUPP;
  589. }
  590. }
  591. static int
  592. nouveau_write(struct device *dev, enum hwmon_sensor_types type, u32 attr,
  593. int channel, long val)
  594. {
  595. switch (type) {
  596. case hwmon_temp:
  597. return nouveau_temp_write(dev, attr, channel, val);
  598. case hwmon_pwm:
  599. return nouveau_pwm_write(dev, attr, channel, val);
  600. default:
  601. return -EOPNOTSUPP;
  602. }
  603. }
  604. static const struct hwmon_ops nouveau_hwmon_ops = {
  605. .is_visible = nouveau_is_visible,
  606. .read = nouveau_read,
  607. .read_string = nouveau_read_string,
  608. .write = nouveau_write,
  609. };
  610. static const struct hwmon_chip_info nouveau_chip_info = {
  611. .ops = &nouveau_hwmon_ops,
  612. .info = nouveau_info,
  613. };
  614. #endif
  615. int
  616. nouveau_hwmon_init(struct drm_device *dev)
  617. {
  618. #if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
  619. struct nouveau_drm *drm = nouveau_drm(dev);
  620. struct nvkm_iccsense *iccsense = nvxx_iccsense(&drm->client.device);
  621. struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
  622. struct nvkm_volt *volt = nvxx_volt(&drm->client.device);
  623. const struct attribute_group *special_groups[N_ATTR_GROUPS];
  624. struct nouveau_hwmon *hwmon;
  625. struct device *hwmon_dev;
  626. int ret = 0;
  627. int i = 0;
  628. if (!iccsense && !therm && !volt) {
  629. NV_DEBUG(drm, "Skipping hwmon registration\n");
  630. return 0;
  631. }
  632. hwmon = drm->hwmon = kzalloc(sizeof(*hwmon), GFP_KERNEL);
  633. if (!hwmon)
  634. return -ENOMEM;
  635. hwmon->dev = dev;
  636. if (therm && therm->attr_get && therm->attr_set) {
  637. if (nvkm_therm_temp_get(therm) >= 0)
  638. special_groups[i++] = &temp1_auto_point_sensor_group;
  639. if (therm->fan_get && therm->fan_get(therm) >= 0)
  640. special_groups[i++] = &pwm_fan_sensor_group;
  641. }
  642. special_groups[i] = NULL;
  643. hwmon_dev = hwmon_device_register_with_info(dev->dev, "nouveau", dev,
  644. &nouveau_chip_info,
  645. special_groups);
  646. if (IS_ERR(hwmon_dev)) {
  647. ret = PTR_ERR(hwmon_dev);
  648. NV_ERROR(drm, "Unable to register hwmon device: %d\n", ret);
  649. return ret;
  650. }
  651. hwmon->hwmon = hwmon_dev;
  652. return 0;
  653. #else
  654. return 0;
  655. #endif
  656. }
  657. void
  658. nouveau_hwmon_fini(struct drm_device *dev)
  659. {
  660. #if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
  661. struct nouveau_hwmon *hwmon = nouveau_hwmon(dev);
  662. if (!hwmon)
  663. return;
  664. if (hwmon->hwmon)
  665. hwmon_device_unregister(hwmon->hwmon);
  666. nouveau_drm(dev)->hwmon = NULL;
  667. kfree(hwmon);
  668. #endif
  669. }