nouveau_pm.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  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. #include "drmP.h"
  25. #include "nouveau_drv.h"
  26. #include "nouveau_pm.h"
  27. #include "nouveau_gpio.h"
  28. #ifdef CONFIG_ACPI
  29. #include <linux/acpi.h>
  30. #endif
  31. #include <linux/power_supply.h>
  32. #include <linux/hwmon.h>
  33. #include <linux/hwmon-sysfs.h>
  34. static int
  35. nouveau_pwmfan_get(struct drm_device *dev)
  36. {
  37. struct drm_nouveau_private *dev_priv = dev->dev_private;
  38. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  39. struct gpio_func gpio;
  40. u32 divs, duty;
  41. int ret;
  42. if (!pm->pwm_get)
  43. return -ENODEV;
  44. ret = nouveau_gpio_find(dev, 0, DCB_GPIO_PWM_FAN, 0xff, &gpio);
  45. if (ret == 0) {
  46. ret = pm->pwm_get(dev, gpio.line, &divs, &duty);
  47. if (ret == 0) {
  48. divs = max(divs, duty);
  49. if (dev_priv->card_type <= NV_40 || (gpio.log[0] & 1))
  50. duty = divs - duty;
  51. return (duty * 100) / divs;
  52. }
  53. return nouveau_gpio_func_get(dev, gpio.func) * 100;
  54. }
  55. return -ENODEV;
  56. }
  57. static int
  58. nouveau_pwmfan_set(struct drm_device *dev, int percent)
  59. {
  60. struct drm_nouveau_private *dev_priv = dev->dev_private;
  61. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  62. struct gpio_func gpio;
  63. u32 divs, duty;
  64. int ret;
  65. if (!pm->pwm_set)
  66. return -ENODEV;
  67. ret = nouveau_gpio_find(dev, 0, DCB_GPIO_PWM_FAN, 0xff, &gpio);
  68. if (ret == 0) {
  69. divs = pm->fan.pwm_divisor;
  70. if (pm->fan.pwm_freq) {
  71. /*XXX: PNVIO clock more than likely... */
  72. divs = 135000 / pm->fan.pwm_freq;
  73. if (dev_priv->chipset < 0xa3)
  74. divs /= 4;
  75. }
  76. duty = ((divs * percent) + 99) / 100;
  77. if (dev_priv->card_type <= NV_40 || (gpio.log[0] & 1))
  78. duty = divs - duty;
  79. ret = pm->pwm_set(dev, gpio.line, divs, duty);
  80. if (!ret)
  81. pm->fan.percent = percent;
  82. return ret;
  83. }
  84. return -ENODEV;
  85. }
  86. static int
  87. nouveau_pm_perflvl_aux(struct drm_device *dev, struct nouveau_pm_level *perflvl,
  88. struct nouveau_pm_level *a, struct nouveau_pm_level *b)
  89. {
  90. struct drm_nouveau_private *dev_priv = dev->dev_private;
  91. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  92. int ret;
  93. /*XXX: not on all boards, we should control based on temperature
  94. * on recent boards.. or maybe on some other factor we don't
  95. * know about?
  96. */
  97. if (a->fanspeed && b->fanspeed && b->fanspeed > a->fanspeed) {
  98. ret = nouveau_pwmfan_set(dev, perflvl->fanspeed);
  99. if (ret && ret != -ENODEV) {
  100. NV_ERROR(dev, "fanspeed set failed: %d\n", ret);
  101. return ret;
  102. }
  103. }
  104. if (pm->voltage.supported && pm->voltage_set) {
  105. if (perflvl->volt_min && b->volt_min > a->volt_min) {
  106. ret = pm->voltage_set(dev, perflvl->volt_min);
  107. if (ret) {
  108. NV_ERROR(dev, "voltage set failed: %d\n", ret);
  109. return ret;
  110. }
  111. }
  112. }
  113. return 0;
  114. }
  115. static int
  116. nouveau_pm_perflvl_set(struct drm_device *dev, struct nouveau_pm_level *perflvl)
  117. {
  118. struct drm_nouveau_private *dev_priv = dev->dev_private;
  119. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  120. void *state;
  121. int ret;
  122. if (perflvl == pm->cur)
  123. return 0;
  124. ret = nouveau_pm_perflvl_aux(dev, perflvl, pm->cur, perflvl);
  125. if (ret)
  126. return ret;
  127. state = pm->clocks_pre(dev, perflvl);
  128. if (IS_ERR(state)) {
  129. ret = PTR_ERR(state);
  130. goto error;
  131. }
  132. ret = pm->clocks_set(dev, state);
  133. if (ret)
  134. goto error;
  135. ret = nouveau_pm_perflvl_aux(dev, perflvl, perflvl, pm->cur);
  136. if (ret)
  137. return ret;
  138. pm->cur = perflvl;
  139. return 0;
  140. error:
  141. /* restore the fan speed and voltage before leaving */
  142. nouveau_pm_perflvl_aux(dev, perflvl, perflvl, pm->cur);
  143. return ret;
  144. }
  145. static int
  146. nouveau_pm_profile_set(struct drm_device *dev, const char *profile)
  147. {
  148. struct drm_nouveau_private *dev_priv = dev->dev_private;
  149. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  150. struct nouveau_pm_level *perflvl = NULL;
  151. u64 start_time;
  152. int ret = 0;
  153. long pl;
  154. /* safety precaution, for now */
  155. if (nouveau_perflvl_wr != 7777)
  156. return -EPERM;
  157. if (!strncmp(profile, "boot", 4))
  158. perflvl = &pm->boot;
  159. else {
  160. int i;
  161. if (kstrtol(profile, 10, &pl) == -EINVAL)
  162. return -EINVAL;
  163. for (i = 0; i < pm->nr_perflvl; i++) {
  164. if (pm->perflvl[i].id == pl) {
  165. perflvl = &pm->perflvl[i];
  166. break;
  167. }
  168. }
  169. if (!perflvl)
  170. return -EINVAL;
  171. }
  172. NV_INFO(dev, "setting performance level: %s", profile);
  173. start_time = nv04_timer_read(dev);
  174. ret = nouveau_pm_perflvl_set(dev, perflvl);
  175. if (!ret) {
  176. NV_INFO(dev, "> reclocking took %lluns\n\n",
  177. (nv04_timer_read(dev) - start_time));
  178. } else {
  179. NV_INFO(dev, "> reclocking failed\n\n");
  180. }
  181. return ret;
  182. }
  183. static int
  184. nouveau_pm_perflvl_get(struct drm_device *dev, struct nouveau_pm_level *perflvl)
  185. {
  186. struct drm_nouveau_private *dev_priv = dev->dev_private;
  187. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  188. int ret;
  189. memset(perflvl, 0, sizeof(*perflvl));
  190. ret = pm->clocks_get(dev, perflvl);
  191. if (ret)
  192. return ret;
  193. if (pm->voltage.supported && pm->voltage_get) {
  194. ret = pm->voltage_get(dev);
  195. if (ret > 0) {
  196. perflvl->volt_min = ret;
  197. perflvl->volt_max = ret;
  198. }
  199. }
  200. ret = nouveau_pwmfan_get(dev);
  201. if (ret > 0)
  202. perflvl->fanspeed = ret;
  203. return 0;
  204. }
  205. static void
  206. nouveau_pm_perflvl_info(struct nouveau_pm_level *perflvl, char *ptr, int len)
  207. {
  208. char c[16], s[16], v[32], f[16], t[16], m[16];
  209. c[0] = '\0';
  210. if (perflvl->core)
  211. snprintf(c, sizeof(c), " core %dMHz", perflvl->core / 1000);
  212. s[0] = '\0';
  213. if (perflvl->shader)
  214. snprintf(s, sizeof(s), " shader %dMHz", perflvl->shader / 1000);
  215. m[0] = '\0';
  216. if (perflvl->memory)
  217. snprintf(m, sizeof(m), " memory %dMHz", perflvl->memory / 1000);
  218. v[0] = '\0';
  219. if (perflvl->volt_min && perflvl->volt_min != perflvl->volt_max) {
  220. snprintf(v, sizeof(v), " voltage %dmV-%dmV",
  221. perflvl->volt_min / 1000, perflvl->volt_max / 1000);
  222. } else
  223. if (perflvl->volt_min) {
  224. snprintf(v, sizeof(v), " voltage %dmV",
  225. perflvl->volt_min / 1000);
  226. }
  227. f[0] = '\0';
  228. if (perflvl->fanspeed)
  229. snprintf(f, sizeof(f), " fanspeed %d%%", perflvl->fanspeed);
  230. t[0] = '\0';
  231. if (perflvl->timing)
  232. snprintf(t, sizeof(t), " timing %d", perflvl->timing->id);
  233. snprintf(ptr, len, "%s%s%s%s%s%s\n", c, s, m, t, v, f);
  234. }
  235. static ssize_t
  236. nouveau_pm_get_perflvl_info(struct device *d,
  237. struct device_attribute *a, char *buf)
  238. {
  239. struct nouveau_pm_level *perflvl = (struct nouveau_pm_level *)a;
  240. char *ptr = buf;
  241. int len = PAGE_SIZE;
  242. snprintf(ptr, len, "%d:", perflvl->id);
  243. ptr += strlen(buf);
  244. len -= strlen(buf);
  245. nouveau_pm_perflvl_info(perflvl, ptr, len);
  246. return strlen(buf);
  247. }
  248. static ssize_t
  249. nouveau_pm_get_perflvl(struct device *d, struct device_attribute *a, char *buf)
  250. {
  251. struct drm_device *dev = pci_get_drvdata(to_pci_dev(d));
  252. struct drm_nouveau_private *dev_priv = dev->dev_private;
  253. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  254. struct nouveau_pm_level cur;
  255. int len = PAGE_SIZE, ret;
  256. char *ptr = buf;
  257. if (!pm->cur)
  258. snprintf(ptr, len, "setting: boot\n");
  259. else if (pm->cur == &pm->boot)
  260. snprintf(ptr, len, "setting: boot\nc:");
  261. else
  262. snprintf(ptr, len, "setting: static %d\nc:", pm->cur->id);
  263. ptr += strlen(buf);
  264. len -= strlen(buf);
  265. ret = nouveau_pm_perflvl_get(dev, &cur);
  266. if (ret == 0)
  267. nouveau_pm_perflvl_info(&cur, ptr, len);
  268. return strlen(buf);
  269. }
  270. static ssize_t
  271. nouveau_pm_set_perflvl(struct device *d, struct device_attribute *a,
  272. const char *buf, size_t count)
  273. {
  274. struct drm_device *dev = pci_get_drvdata(to_pci_dev(d));
  275. int ret;
  276. ret = nouveau_pm_profile_set(dev, buf);
  277. if (ret)
  278. return ret;
  279. return strlen(buf);
  280. }
  281. static DEVICE_ATTR(performance_level, S_IRUGO | S_IWUSR,
  282. nouveau_pm_get_perflvl, nouveau_pm_set_perflvl);
  283. static int
  284. nouveau_sysfs_init(struct drm_device *dev)
  285. {
  286. struct drm_nouveau_private *dev_priv = dev->dev_private;
  287. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  288. struct device *d = &dev->pdev->dev;
  289. int ret, i;
  290. ret = device_create_file(d, &dev_attr_performance_level);
  291. if (ret)
  292. return ret;
  293. for (i = 0; i < pm->nr_perflvl; i++) {
  294. struct nouveau_pm_level *perflvl = &pm->perflvl[i];
  295. perflvl->dev_attr.attr.name = perflvl->name;
  296. perflvl->dev_attr.attr.mode = S_IRUGO;
  297. perflvl->dev_attr.show = nouveau_pm_get_perflvl_info;
  298. perflvl->dev_attr.store = NULL;
  299. sysfs_attr_init(&perflvl->dev_attr.attr);
  300. ret = device_create_file(d, &perflvl->dev_attr);
  301. if (ret) {
  302. NV_ERROR(dev, "failed pervlvl %d sysfs: %d\n",
  303. perflvl->id, i);
  304. perflvl->dev_attr.attr.name = NULL;
  305. nouveau_pm_fini(dev);
  306. return ret;
  307. }
  308. }
  309. return 0;
  310. }
  311. static void
  312. nouveau_sysfs_fini(struct drm_device *dev)
  313. {
  314. struct drm_nouveau_private *dev_priv = dev->dev_private;
  315. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  316. struct device *d = &dev->pdev->dev;
  317. int i;
  318. device_remove_file(d, &dev_attr_performance_level);
  319. for (i = 0; i < pm->nr_perflvl; i++) {
  320. struct nouveau_pm_level *pl = &pm->perflvl[i];
  321. if (!pl->dev_attr.attr.name)
  322. break;
  323. device_remove_file(d, &pl->dev_attr);
  324. }
  325. }
  326. #if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
  327. static ssize_t
  328. nouveau_hwmon_show_temp(struct device *d, struct device_attribute *a, char *buf)
  329. {
  330. struct drm_device *dev = dev_get_drvdata(d);
  331. struct drm_nouveau_private *dev_priv = dev->dev_private;
  332. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  333. return snprintf(buf, PAGE_SIZE, "%d\n", pm->temp_get(dev)*1000);
  334. }
  335. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, nouveau_hwmon_show_temp,
  336. NULL, 0);
  337. static ssize_t
  338. nouveau_hwmon_max_temp(struct device *d, struct device_attribute *a, char *buf)
  339. {
  340. struct drm_device *dev = dev_get_drvdata(d);
  341. struct drm_nouveau_private *dev_priv = dev->dev_private;
  342. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  343. struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
  344. return snprintf(buf, PAGE_SIZE, "%d\n", temp->down_clock*1000);
  345. }
  346. static ssize_t
  347. nouveau_hwmon_set_max_temp(struct device *d, struct device_attribute *a,
  348. const char *buf, size_t count)
  349. {
  350. struct drm_device *dev = dev_get_drvdata(d);
  351. struct drm_nouveau_private *dev_priv = dev->dev_private;
  352. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  353. struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
  354. long value;
  355. if (kstrtol(buf, 10, &value) == -EINVAL)
  356. return count;
  357. temp->down_clock = value/1000;
  358. nouveau_temp_safety_checks(dev);
  359. return count;
  360. }
  361. static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, nouveau_hwmon_max_temp,
  362. nouveau_hwmon_set_max_temp,
  363. 0);
  364. static ssize_t
  365. nouveau_hwmon_critical_temp(struct device *d, struct device_attribute *a,
  366. char *buf)
  367. {
  368. struct drm_device *dev = dev_get_drvdata(d);
  369. struct drm_nouveau_private *dev_priv = dev->dev_private;
  370. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  371. struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
  372. return snprintf(buf, PAGE_SIZE, "%d\n", temp->critical*1000);
  373. }
  374. static ssize_t
  375. nouveau_hwmon_set_critical_temp(struct device *d, struct device_attribute *a,
  376. const char *buf,
  377. size_t count)
  378. {
  379. struct drm_device *dev = dev_get_drvdata(d);
  380. struct drm_nouveau_private *dev_priv = dev->dev_private;
  381. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  382. struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
  383. long value;
  384. if (kstrtol(buf, 10, &value) == -EINVAL)
  385. return count;
  386. temp->critical = value/1000;
  387. nouveau_temp_safety_checks(dev);
  388. return count;
  389. }
  390. static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO | S_IWUSR,
  391. nouveau_hwmon_critical_temp,
  392. nouveau_hwmon_set_critical_temp,
  393. 0);
  394. static ssize_t nouveau_hwmon_show_name(struct device *dev,
  395. struct device_attribute *attr,
  396. char *buf)
  397. {
  398. return sprintf(buf, "nouveau\n");
  399. }
  400. static SENSOR_DEVICE_ATTR(name, S_IRUGO, nouveau_hwmon_show_name, NULL, 0);
  401. static ssize_t nouveau_hwmon_show_update_rate(struct device *dev,
  402. struct device_attribute *attr,
  403. char *buf)
  404. {
  405. return sprintf(buf, "1000\n");
  406. }
  407. static SENSOR_DEVICE_ATTR(update_rate, S_IRUGO,
  408. nouveau_hwmon_show_update_rate,
  409. NULL, 0);
  410. static ssize_t
  411. nouveau_hwmon_show_fan0_input(struct device *d, struct device_attribute *attr,
  412. char *buf)
  413. {
  414. struct drm_device *dev = dev_get_drvdata(d);
  415. struct drm_nouveau_private *dev_priv = dev->dev_private;
  416. struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
  417. struct gpio_func gpio;
  418. u32 cycles, cur, prev;
  419. u64 start;
  420. int ret;
  421. ret = nouveau_gpio_find(dev, 0, DCB_GPIO_FAN_SENSE, 0xff, &gpio);
  422. if (ret)
  423. return ret;
  424. /* Monitor the GPIO input 0x3b for 250ms.
  425. * When the fan spins, it changes the value of GPIO FAN_SENSE.
  426. * We get 4 changes (0 -> 1 -> 0 -> 1 -> [...]) per complete rotation.
  427. */
  428. start = ptimer->read(dev);
  429. prev = nouveau_gpio_sense(dev, 0, gpio.line);
  430. cycles = 0;
  431. do {
  432. cur = nouveau_gpio_sense(dev, 0, gpio.line);
  433. if (prev != cur) {
  434. cycles++;
  435. prev = cur;
  436. }
  437. usleep_range(500, 1000); /* supports 0 < rpm < 7500 */
  438. } while (ptimer->read(dev) - start < 250000000);
  439. /* interpolate to get rpm */
  440. return sprintf(buf, "%i\n", cycles / 4 * 4 * 60);
  441. }
  442. static SENSOR_DEVICE_ATTR(fan0_input, S_IRUGO, nouveau_hwmon_show_fan0_input,
  443. NULL, 0);
  444. static ssize_t
  445. nouveau_hwmon_get_pwm0(struct device *d, struct device_attribute *a, char *buf)
  446. {
  447. struct drm_device *dev = dev_get_drvdata(d);
  448. int ret;
  449. ret = nouveau_pwmfan_get(dev);
  450. if (ret < 0)
  451. return ret;
  452. return sprintf(buf, "%i\n", ret);
  453. }
  454. static ssize_t
  455. nouveau_hwmon_set_pwm0(struct device *d, struct device_attribute *a,
  456. const char *buf, size_t count)
  457. {
  458. struct drm_device *dev = dev_get_drvdata(d);
  459. struct drm_nouveau_private *dev_priv = dev->dev_private;
  460. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  461. int ret = -ENODEV;
  462. long value;
  463. if (nouveau_perflvl_wr != 7777)
  464. return -EPERM;
  465. if (kstrtol(buf, 10, &value) == -EINVAL)
  466. return -EINVAL;
  467. if (value < pm->fan.min_duty)
  468. value = pm->fan.min_duty;
  469. if (value > pm->fan.max_duty)
  470. value = pm->fan.max_duty;
  471. ret = nouveau_pwmfan_set(dev, value);
  472. if (ret)
  473. return ret;
  474. return count;
  475. }
  476. static SENSOR_DEVICE_ATTR(pwm0, S_IRUGO | S_IWUSR,
  477. nouveau_hwmon_get_pwm0,
  478. nouveau_hwmon_set_pwm0, 0);
  479. static ssize_t
  480. nouveau_hwmon_get_pwm0_min(struct device *d,
  481. struct device_attribute *a, char *buf)
  482. {
  483. struct drm_device *dev = dev_get_drvdata(d);
  484. struct drm_nouveau_private *dev_priv = dev->dev_private;
  485. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  486. return sprintf(buf, "%i\n", pm->fan.min_duty);
  487. }
  488. static ssize_t
  489. nouveau_hwmon_set_pwm0_min(struct device *d, struct device_attribute *a,
  490. const char *buf, size_t count)
  491. {
  492. struct drm_device *dev = dev_get_drvdata(d);
  493. struct drm_nouveau_private *dev_priv = dev->dev_private;
  494. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  495. long value;
  496. if (kstrtol(buf, 10, &value) == -EINVAL)
  497. return -EINVAL;
  498. if (value < 0)
  499. value = 0;
  500. if (pm->fan.max_duty - value < 10)
  501. value = pm->fan.max_duty - 10;
  502. if (value < 10)
  503. pm->fan.min_duty = 10;
  504. else
  505. pm->fan.min_duty = value;
  506. return count;
  507. }
  508. static SENSOR_DEVICE_ATTR(pwm0_min, S_IRUGO | S_IWUSR,
  509. nouveau_hwmon_get_pwm0_min,
  510. nouveau_hwmon_set_pwm0_min, 0);
  511. static ssize_t
  512. nouveau_hwmon_get_pwm0_max(struct device *d,
  513. struct device_attribute *a, char *buf)
  514. {
  515. struct drm_device *dev = dev_get_drvdata(d);
  516. struct drm_nouveau_private *dev_priv = dev->dev_private;
  517. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  518. return sprintf(buf, "%i\n", pm->fan.max_duty);
  519. }
  520. static ssize_t
  521. nouveau_hwmon_set_pwm0_max(struct device *d, struct device_attribute *a,
  522. const char *buf, size_t count)
  523. {
  524. struct drm_device *dev = dev_get_drvdata(d);
  525. struct drm_nouveau_private *dev_priv = dev->dev_private;
  526. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  527. long value;
  528. if (kstrtol(buf, 10, &value) == -EINVAL)
  529. return -EINVAL;
  530. if (value < 0)
  531. value = 0;
  532. if (value - pm->fan.min_duty < 10)
  533. value = pm->fan.min_duty + 10;
  534. if (value > 100)
  535. pm->fan.max_duty = 100;
  536. else
  537. pm->fan.max_duty = value;
  538. return count;
  539. }
  540. static SENSOR_DEVICE_ATTR(pwm0_max, S_IRUGO | S_IWUSR,
  541. nouveau_hwmon_get_pwm0_max,
  542. nouveau_hwmon_set_pwm0_max, 0);
  543. static struct attribute *hwmon_attributes[] = {
  544. &sensor_dev_attr_temp1_input.dev_attr.attr,
  545. &sensor_dev_attr_temp1_max.dev_attr.attr,
  546. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  547. &sensor_dev_attr_name.dev_attr.attr,
  548. &sensor_dev_attr_update_rate.dev_attr.attr,
  549. NULL
  550. };
  551. static struct attribute *hwmon_fan_rpm_attributes[] = {
  552. &sensor_dev_attr_fan0_input.dev_attr.attr,
  553. NULL
  554. };
  555. static struct attribute *hwmon_pwm_fan_attributes[] = {
  556. &sensor_dev_attr_pwm0.dev_attr.attr,
  557. &sensor_dev_attr_pwm0_min.dev_attr.attr,
  558. &sensor_dev_attr_pwm0_max.dev_attr.attr,
  559. NULL
  560. };
  561. static const struct attribute_group hwmon_attrgroup = {
  562. .attrs = hwmon_attributes,
  563. };
  564. static const struct attribute_group hwmon_fan_rpm_attrgroup = {
  565. .attrs = hwmon_fan_rpm_attributes,
  566. };
  567. static const struct attribute_group hwmon_pwm_fan_attrgroup = {
  568. .attrs = hwmon_pwm_fan_attributes,
  569. };
  570. #endif
  571. static int
  572. nouveau_hwmon_init(struct drm_device *dev)
  573. {
  574. struct drm_nouveau_private *dev_priv = dev->dev_private;
  575. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  576. #if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
  577. struct device *hwmon_dev;
  578. int ret = 0;
  579. if (!pm->temp_get)
  580. return -ENODEV;
  581. hwmon_dev = hwmon_device_register(&dev->pdev->dev);
  582. if (IS_ERR(hwmon_dev)) {
  583. ret = PTR_ERR(hwmon_dev);
  584. NV_ERROR(dev,
  585. "Unable to register hwmon device: %d\n", ret);
  586. return ret;
  587. }
  588. dev_set_drvdata(hwmon_dev, dev);
  589. /* default sysfs entries */
  590. ret = sysfs_create_group(&dev->pdev->dev.kobj, &hwmon_attrgroup);
  591. if (ret) {
  592. if (ret)
  593. goto error;
  594. }
  595. /* if the card has a pwm fan */
  596. /*XXX: incorrect, need better detection for this, some boards have
  597. * the gpio entries for pwm fan control even when there's no
  598. * actual fan connected to it... therm table? */
  599. if (nouveau_pwmfan_get(dev) >= 0) {
  600. ret = sysfs_create_group(&dev->pdev->dev.kobj,
  601. &hwmon_pwm_fan_attrgroup);
  602. if (ret)
  603. goto error;
  604. }
  605. /* if the card can read the fan rpm */
  606. if (nouveau_gpio_func_valid(dev, DCB_GPIO_FAN_SENSE)) {
  607. ret = sysfs_create_group(&dev->pdev->dev.kobj,
  608. &hwmon_fan_rpm_attrgroup);
  609. if (ret)
  610. goto error;
  611. }
  612. pm->hwmon = hwmon_dev;
  613. return 0;
  614. error:
  615. NV_ERROR(dev, "Unable to create some hwmon sysfs files: %d\n", ret);
  616. hwmon_device_unregister(hwmon_dev);
  617. pm->hwmon = NULL;
  618. return ret;
  619. #else
  620. pm->hwmon = NULL;
  621. return 0;
  622. #endif
  623. }
  624. static void
  625. nouveau_hwmon_fini(struct drm_device *dev)
  626. {
  627. #if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
  628. struct drm_nouveau_private *dev_priv = dev->dev_private;
  629. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  630. if (pm->hwmon) {
  631. sysfs_remove_group(&dev->pdev->dev.kobj, &hwmon_attrgroup);
  632. sysfs_remove_group(&dev->pdev->dev.kobj,
  633. &hwmon_pwm_fan_attrgroup);
  634. sysfs_remove_group(&dev->pdev->dev.kobj,
  635. &hwmon_fan_rpm_attrgroup);
  636. hwmon_device_unregister(pm->hwmon);
  637. }
  638. #endif
  639. }
  640. #if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
  641. static int
  642. nouveau_pm_acpi_event(struct notifier_block *nb, unsigned long val, void *data)
  643. {
  644. struct drm_nouveau_private *dev_priv =
  645. container_of(nb, struct drm_nouveau_private, engine.pm.acpi_nb);
  646. struct drm_device *dev = dev_priv->dev;
  647. struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
  648. if (strcmp(entry->device_class, "ac_adapter") == 0) {
  649. bool ac = power_supply_is_system_supplied();
  650. NV_DEBUG(dev, "power supply changed: %s\n", ac ? "AC" : "DC");
  651. }
  652. return NOTIFY_OK;
  653. }
  654. #endif
  655. int
  656. nouveau_pm_init(struct drm_device *dev)
  657. {
  658. struct drm_nouveau_private *dev_priv = dev->dev_private;
  659. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  660. char info[256];
  661. int ret, i;
  662. nouveau_mem_timing_init(dev);
  663. nouveau_volt_init(dev);
  664. nouveau_perf_init(dev);
  665. nouveau_temp_init(dev);
  666. NV_INFO(dev, "%d available performance level(s)\n", pm->nr_perflvl);
  667. for (i = 0; i < pm->nr_perflvl; i++) {
  668. nouveau_pm_perflvl_info(&pm->perflvl[i], info, sizeof(info));
  669. NV_INFO(dev, "%d:%s", pm->perflvl[i].id, info);
  670. }
  671. /* determine current ("boot") performance level */
  672. ret = nouveau_pm_perflvl_get(dev, &pm->boot);
  673. if (ret == 0) {
  674. strncpy(pm->boot.name, "boot", 4);
  675. pm->cur = &pm->boot;
  676. nouveau_pm_perflvl_info(&pm->boot, info, sizeof(info));
  677. NV_INFO(dev, "c:%s", info);
  678. }
  679. /* switch performance levels now if requested */
  680. if (nouveau_perflvl != NULL) {
  681. ret = nouveau_pm_profile_set(dev, nouveau_perflvl);
  682. if (ret) {
  683. NV_ERROR(dev, "error setting perflvl \"%s\": %d\n",
  684. nouveau_perflvl, ret);
  685. }
  686. }
  687. /* determine the current fan speed */
  688. pm->fan.percent = nouveau_pwmfan_get(dev);
  689. nouveau_sysfs_init(dev);
  690. nouveau_hwmon_init(dev);
  691. #if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
  692. pm->acpi_nb.notifier_call = nouveau_pm_acpi_event;
  693. register_acpi_notifier(&pm->acpi_nb);
  694. #endif
  695. return 0;
  696. }
  697. void
  698. nouveau_pm_fini(struct drm_device *dev)
  699. {
  700. struct drm_nouveau_private *dev_priv = dev->dev_private;
  701. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  702. if (pm->cur != &pm->boot)
  703. nouveau_pm_perflvl_set(dev, &pm->boot);
  704. nouveau_temp_fini(dev);
  705. nouveau_perf_fini(dev);
  706. nouveau_volt_fini(dev);
  707. nouveau_mem_timing_fini(dev);
  708. #if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
  709. unregister_acpi_notifier(&pm->acpi_nb);
  710. #endif
  711. nouveau_hwmon_fini(dev);
  712. nouveau_sysfs_fini(dev);
  713. }
  714. void
  715. nouveau_pm_resume(struct drm_device *dev)
  716. {
  717. struct drm_nouveau_private *dev_priv = dev->dev_private;
  718. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  719. struct nouveau_pm_level *perflvl;
  720. if (!pm->cur || pm->cur == &pm->boot)
  721. return;
  722. perflvl = pm->cur;
  723. pm->cur = &pm->boot;
  724. nouveau_pm_perflvl_set(dev, perflvl);
  725. nouveau_pwmfan_set(dev, pm->fan.percent);
  726. }