vga_switcheroo.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. /*
  2. * Copyright (c) 2010 Red Hat Inc.
  3. * Author : Dave Airlie <airlied@redhat.com>
  4. *
  5. *
  6. * Licensed under GPLv2
  7. *
  8. * vga_switcheroo.c - Support for laptop with dual GPU using one set of outputs
  9. Switcher interface - methods require for ATPX and DCM
  10. - switchto - this throws the output MUX switch
  11. - discrete_set_power - sets the power state for the discrete card
  12. GPU driver interface
  13. - set_gpu_state - this should do the equiv of s/r for the card
  14. - this should *not* set the discrete power state
  15. - switch_check - check if the device is in a position to switch now
  16. */
  17. #include <linux/module.h>
  18. #include <linux/seq_file.h>
  19. #include <linux/uaccess.h>
  20. #include <linux/fs.h>
  21. #include <linux/debugfs.h>
  22. #include <linux/fb.h>
  23. #include <linux/pci.h>
  24. #include <linux/console.h>
  25. #include <linux/vga_switcheroo.h>
  26. #include <linux/pm_runtime.h>
  27. #include <linux/vgaarb.h>
  28. struct vga_switcheroo_client {
  29. struct pci_dev *pdev;
  30. struct fb_info *fb_info;
  31. int pwr_state;
  32. const struct vga_switcheroo_client_ops *ops;
  33. int id;
  34. bool active;
  35. bool driver_power_control;
  36. struct list_head list;
  37. };
  38. static DEFINE_MUTEX(vgasr_mutex);
  39. struct vgasr_priv {
  40. bool active;
  41. bool delayed_switch_active;
  42. enum vga_switcheroo_client_id delayed_client_id;
  43. struct dentry *debugfs_root;
  44. struct dentry *switch_file;
  45. int registered_clients;
  46. struct list_head clients;
  47. struct vga_switcheroo_handler *handler;
  48. };
  49. #define ID_BIT_AUDIO 0x100
  50. #define client_is_audio(c) ((c)->id & ID_BIT_AUDIO)
  51. #define client_is_vga(c) ((c)->id == -1 || !client_is_audio(c))
  52. #define client_id(c) ((c)->id & ~ID_BIT_AUDIO)
  53. static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv);
  54. static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv);
  55. /* only one switcheroo per system */
  56. static struct vgasr_priv vgasr_priv = {
  57. .clients = LIST_HEAD_INIT(vgasr_priv.clients),
  58. };
  59. static bool vga_switcheroo_ready(void)
  60. {
  61. /* we're ready if we get two clients + handler */
  62. return !vgasr_priv.active &&
  63. vgasr_priv.registered_clients == 2 && vgasr_priv.handler;
  64. }
  65. static void vga_switcheroo_enable(void)
  66. {
  67. int ret;
  68. struct vga_switcheroo_client *client;
  69. /* call the handler to init */
  70. if (vgasr_priv.handler->init)
  71. vgasr_priv.handler->init();
  72. list_for_each_entry(client, &vgasr_priv.clients, list) {
  73. if (client->id != -1)
  74. continue;
  75. ret = vgasr_priv.handler->get_client_id(client->pdev);
  76. if (ret < 0)
  77. return;
  78. client->id = ret;
  79. }
  80. vga_switcheroo_debugfs_init(&vgasr_priv);
  81. vgasr_priv.active = true;
  82. }
  83. int vga_switcheroo_register_handler(struct vga_switcheroo_handler *handler)
  84. {
  85. mutex_lock(&vgasr_mutex);
  86. if (vgasr_priv.handler) {
  87. mutex_unlock(&vgasr_mutex);
  88. return -EINVAL;
  89. }
  90. vgasr_priv.handler = handler;
  91. if (vga_switcheroo_ready()) {
  92. printk(KERN_INFO "vga_switcheroo: enabled\n");
  93. vga_switcheroo_enable();
  94. }
  95. mutex_unlock(&vgasr_mutex);
  96. return 0;
  97. }
  98. EXPORT_SYMBOL(vga_switcheroo_register_handler);
  99. void vga_switcheroo_unregister_handler(void)
  100. {
  101. mutex_lock(&vgasr_mutex);
  102. vgasr_priv.handler = NULL;
  103. if (vgasr_priv.active) {
  104. pr_info("vga_switcheroo: disabled\n");
  105. vga_switcheroo_debugfs_fini(&vgasr_priv);
  106. vgasr_priv.active = false;
  107. }
  108. mutex_unlock(&vgasr_mutex);
  109. }
  110. EXPORT_SYMBOL(vga_switcheroo_unregister_handler);
  111. static int register_client(struct pci_dev *pdev,
  112. const struct vga_switcheroo_client_ops *ops,
  113. int id, bool active, bool driver_power_control)
  114. {
  115. struct vga_switcheroo_client *client;
  116. client = kzalloc(sizeof(*client), GFP_KERNEL);
  117. if (!client)
  118. return -ENOMEM;
  119. client->pwr_state = VGA_SWITCHEROO_ON;
  120. client->pdev = pdev;
  121. client->ops = ops;
  122. client->id = id;
  123. client->active = active;
  124. client->driver_power_control = driver_power_control;
  125. mutex_lock(&vgasr_mutex);
  126. list_add_tail(&client->list, &vgasr_priv.clients);
  127. if (client_is_vga(client))
  128. vgasr_priv.registered_clients++;
  129. if (vga_switcheroo_ready()) {
  130. printk(KERN_INFO "vga_switcheroo: enabled\n");
  131. vga_switcheroo_enable();
  132. }
  133. mutex_unlock(&vgasr_mutex);
  134. return 0;
  135. }
  136. int vga_switcheroo_register_client(struct pci_dev *pdev,
  137. const struct vga_switcheroo_client_ops *ops,
  138. bool driver_power_control)
  139. {
  140. return register_client(pdev, ops, -1,
  141. pdev == vga_default_device(), driver_power_control);
  142. }
  143. EXPORT_SYMBOL(vga_switcheroo_register_client);
  144. int vga_switcheroo_register_audio_client(struct pci_dev *pdev,
  145. const struct vga_switcheroo_client_ops *ops,
  146. int id, bool active)
  147. {
  148. return register_client(pdev, ops, id | ID_BIT_AUDIO, active, false);
  149. }
  150. EXPORT_SYMBOL(vga_switcheroo_register_audio_client);
  151. static struct vga_switcheroo_client *
  152. find_client_from_pci(struct list_head *head, struct pci_dev *pdev)
  153. {
  154. struct vga_switcheroo_client *client;
  155. list_for_each_entry(client, head, list)
  156. if (client->pdev == pdev)
  157. return client;
  158. return NULL;
  159. }
  160. static struct vga_switcheroo_client *
  161. find_client_from_id(struct list_head *head, int client_id)
  162. {
  163. struct vga_switcheroo_client *client;
  164. list_for_each_entry(client, head, list)
  165. if (client->id == client_id)
  166. return client;
  167. return NULL;
  168. }
  169. static struct vga_switcheroo_client *
  170. find_active_client(struct list_head *head)
  171. {
  172. struct vga_switcheroo_client *client;
  173. list_for_each_entry(client, head, list)
  174. if (client->active && client_is_vga(client))
  175. return client;
  176. return NULL;
  177. }
  178. int vga_switcheroo_get_client_state(struct pci_dev *pdev)
  179. {
  180. struct vga_switcheroo_client *client;
  181. client = find_client_from_pci(&vgasr_priv.clients, pdev);
  182. if (!client)
  183. return VGA_SWITCHEROO_NOT_FOUND;
  184. if (!vgasr_priv.active)
  185. return VGA_SWITCHEROO_INIT;
  186. return client->pwr_state;
  187. }
  188. EXPORT_SYMBOL(vga_switcheroo_get_client_state);
  189. void vga_switcheroo_unregister_client(struct pci_dev *pdev)
  190. {
  191. struct vga_switcheroo_client *client;
  192. mutex_lock(&vgasr_mutex);
  193. client = find_client_from_pci(&vgasr_priv.clients, pdev);
  194. if (client) {
  195. if (client_is_vga(client))
  196. vgasr_priv.registered_clients--;
  197. list_del(&client->list);
  198. kfree(client);
  199. }
  200. if (vgasr_priv.active && vgasr_priv.registered_clients < 2) {
  201. printk(KERN_INFO "vga_switcheroo: disabled\n");
  202. vga_switcheroo_debugfs_fini(&vgasr_priv);
  203. vgasr_priv.active = false;
  204. }
  205. mutex_unlock(&vgasr_mutex);
  206. }
  207. EXPORT_SYMBOL(vga_switcheroo_unregister_client);
  208. void vga_switcheroo_client_fb_set(struct pci_dev *pdev,
  209. struct fb_info *info)
  210. {
  211. struct vga_switcheroo_client *client;
  212. mutex_lock(&vgasr_mutex);
  213. client = find_client_from_pci(&vgasr_priv.clients, pdev);
  214. if (client)
  215. client->fb_info = info;
  216. mutex_unlock(&vgasr_mutex);
  217. }
  218. EXPORT_SYMBOL(vga_switcheroo_client_fb_set);
  219. static int vga_switcheroo_show(struct seq_file *m, void *v)
  220. {
  221. struct vga_switcheroo_client *client;
  222. int i = 0;
  223. mutex_lock(&vgasr_mutex);
  224. list_for_each_entry(client, &vgasr_priv.clients, list) {
  225. seq_printf(m, "%d:%s%s:%c:%s%s:%s\n", i,
  226. client_id(client) == VGA_SWITCHEROO_DIS ? "DIS" : "IGD",
  227. client_is_vga(client) ? "" : "-Audio",
  228. client->active ? '+' : ' ',
  229. client->driver_power_control ? "Dyn" : "",
  230. client->pwr_state ? "Pwr" : "Off",
  231. pci_name(client->pdev));
  232. i++;
  233. }
  234. mutex_unlock(&vgasr_mutex);
  235. return 0;
  236. }
  237. static int vga_switcheroo_debugfs_open(struct inode *inode, struct file *file)
  238. {
  239. return single_open(file, vga_switcheroo_show, NULL);
  240. }
  241. static int vga_switchon(struct vga_switcheroo_client *client)
  242. {
  243. if (client->driver_power_control)
  244. return 0;
  245. if (vgasr_priv.handler->power_state)
  246. vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_ON);
  247. /* call the driver callback to turn on device */
  248. client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_ON);
  249. client->pwr_state = VGA_SWITCHEROO_ON;
  250. return 0;
  251. }
  252. static int vga_switchoff(struct vga_switcheroo_client *client)
  253. {
  254. if (client->driver_power_control)
  255. return 0;
  256. /* call the driver callback to turn off device */
  257. client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_OFF);
  258. if (vgasr_priv.handler->power_state)
  259. vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_OFF);
  260. client->pwr_state = VGA_SWITCHEROO_OFF;
  261. return 0;
  262. }
  263. static void set_audio_state(int id, int state)
  264. {
  265. struct vga_switcheroo_client *client;
  266. client = find_client_from_id(&vgasr_priv.clients, id | ID_BIT_AUDIO);
  267. if (client && client->pwr_state != state) {
  268. client->ops->set_gpu_state(client->pdev, state);
  269. client->pwr_state = state;
  270. }
  271. }
  272. /* stage one happens before delay */
  273. static int vga_switchto_stage1(struct vga_switcheroo_client *new_client)
  274. {
  275. struct vga_switcheroo_client *active;
  276. active = find_active_client(&vgasr_priv.clients);
  277. if (!active)
  278. return 0;
  279. if (new_client->pwr_state == VGA_SWITCHEROO_OFF)
  280. vga_switchon(new_client);
  281. vga_set_default_device(new_client->pdev);
  282. return 0;
  283. }
  284. /* post delay */
  285. static int vga_switchto_stage2(struct vga_switcheroo_client *new_client)
  286. {
  287. int ret;
  288. struct vga_switcheroo_client *active;
  289. active = find_active_client(&vgasr_priv.clients);
  290. if (!active)
  291. return 0;
  292. active->active = false;
  293. set_audio_state(active->id, VGA_SWITCHEROO_OFF);
  294. if (new_client->fb_info) {
  295. struct fb_event event;
  296. console_lock();
  297. event.info = new_client->fb_info;
  298. fb_notifier_call_chain(FB_EVENT_REMAP_ALL_CONSOLE, &event);
  299. console_unlock();
  300. }
  301. ret = vgasr_priv.handler->switchto(new_client->id);
  302. if (ret)
  303. return ret;
  304. if (new_client->ops->reprobe)
  305. new_client->ops->reprobe(new_client->pdev);
  306. if (active->pwr_state == VGA_SWITCHEROO_ON)
  307. vga_switchoff(active);
  308. set_audio_state(new_client->id, VGA_SWITCHEROO_ON);
  309. new_client->active = true;
  310. return 0;
  311. }
  312. static bool check_can_switch(void)
  313. {
  314. struct vga_switcheroo_client *client;
  315. list_for_each_entry(client, &vgasr_priv.clients, list) {
  316. if (!client->ops->can_switch(client->pdev)) {
  317. printk(KERN_ERR "vga_switcheroo: client %x refused switch\n", client->id);
  318. return false;
  319. }
  320. }
  321. return true;
  322. }
  323. static ssize_t
  324. vga_switcheroo_debugfs_write(struct file *filp, const char __user *ubuf,
  325. size_t cnt, loff_t *ppos)
  326. {
  327. char usercmd[64];
  328. int ret;
  329. bool delay = false, can_switch;
  330. bool just_mux = false;
  331. int client_id = -1;
  332. struct vga_switcheroo_client *client = NULL;
  333. if (cnt > 63)
  334. cnt = 63;
  335. if (copy_from_user(usercmd, ubuf, cnt))
  336. return -EFAULT;
  337. mutex_lock(&vgasr_mutex);
  338. if (!vgasr_priv.active) {
  339. cnt = -EINVAL;
  340. goto out;
  341. }
  342. /* pwr off the device not in use */
  343. if (strncmp(usercmd, "OFF", 3) == 0) {
  344. list_for_each_entry(client, &vgasr_priv.clients, list) {
  345. if (client->active || client_is_audio(client))
  346. continue;
  347. if (client->driver_power_control)
  348. continue;
  349. set_audio_state(client->id, VGA_SWITCHEROO_OFF);
  350. if (client->pwr_state == VGA_SWITCHEROO_ON)
  351. vga_switchoff(client);
  352. }
  353. goto out;
  354. }
  355. /* pwr on the device not in use */
  356. if (strncmp(usercmd, "ON", 2) == 0) {
  357. list_for_each_entry(client, &vgasr_priv.clients, list) {
  358. if (client->active || client_is_audio(client))
  359. continue;
  360. if (client->driver_power_control)
  361. continue;
  362. if (client->pwr_state == VGA_SWITCHEROO_OFF)
  363. vga_switchon(client);
  364. set_audio_state(client->id, VGA_SWITCHEROO_ON);
  365. }
  366. goto out;
  367. }
  368. /* request a delayed switch - test can we switch now */
  369. if (strncmp(usercmd, "DIGD", 4) == 0) {
  370. client_id = VGA_SWITCHEROO_IGD;
  371. delay = true;
  372. }
  373. if (strncmp(usercmd, "DDIS", 4) == 0) {
  374. client_id = VGA_SWITCHEROO_DIS;
  375. delay = true;
  376. }
  377. if (strncmp(usercmd, "IGD", 3) == 0)
  378. client_id = VGA_SWITCHEROO_IGD;
  379. if (strncmp(usercmd, "DIS", 3) == 0)
  380. client_id = VGA_SWITCHEROO_DIS;
  381. if (strncmp(usercmd, "MIGD", 4) == 0) {
  382. just_mux = true;
  383. client_id = VGA_SWITCHEROO_IGD;
  384. }
  385. if (strncmp(usercmd, "MDIS", 4) == 0) {
  386. just_mux = true;
  387. client_id = VGA_SWITCHEROO_DIS;
  388. }
  389. if (client_id == -1)
  390. goto out;
  391. client = find_client_from_id(&vgasr_priv.clients, client_id);
  392. if (!client)
  393. goto out;
  394. vgasr_priv.delayed_switch_active = false;
  395. if (just_mux) {
  396. ret = vgasr_priv.handler->switchto(client_id);
  397. goto out;
  398. }
  399. if (client->active)
  400. goto out;
  401. /* okay we want a switch - test if devices are willing to switch */
  402. can_switch = check_can_switch();
  403. if (can_switch == false && delay == false)
  404. goto out;
  405. if (can_switch) {
  406. ret = vga_switchto_stage1(client);
  407. if (ret)
  408. printk(KERN_ERR "vga_switcheroo: switching failed stage 1 %d\n", ret);
  409. ret = vga_switchto_stage2(client);
  410. if (ret)
  411. printk(KERN_ERR "vga_switcheroo: switching failed stage 2 %d\n", ret);
  412. } else {
  413. printk(KERN_INFO "vga_switcheroo: setting delayed switch to client %d\n", client->id);
  414. vgasr_priv.delayed_switch_active = true;
  415. vgasr_priv.delayed_client_id = client_id;
  416. ret = vga_switchto_stage1(client);
  417. if (ret)
  418. printk(KERN_ERR "vga_switcheroo: delayed switching stage 1 failed %d\n", ret);
  419. }
  420. out:
  421. mutex_unlock(&vgasr_mutex);
  422. return cnt;
  423. }
  424. static const struct file_operations vga_switcheroo_debugfs_fops = {
  425. .owner = THIS_MODULE,
  426. .open = vga_switcheroo_debugfs_open,
  427. .write = vga_switcheroo_debugfs_write,
  428. .read = seq_read,
  429. .llseek = seq_lseek,
  430. .release = single_release,
  431. };
  432. static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv)
  433. {
  434. if (priv->switch_file) {
  435. debugfs_remove(priv->switch_file);
  436. priv->switch_file = NULL;
  437. }
  438. if (priv->debugfs_root) {
  439. debugfs_remove(priv->debugfs_root);
  440. priv->debugfs_root = NULL;
  441. }
  442. }
  443. static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv)
  444. {
  445. /* already initialised */
  446. if (priv->debugfs_root)
  447. return 0;
  448. priv->debugfs_root = debugfs_create_dir("vgaswitcheroo", NULL);
  449. if (!priv->debugfs_root) {
  450. printk(KERN_ERR "vga_switcheroo: Cannot create /sys/kernel/debug/vgaswitcheroo\n");
  451. goto fail;
  452. }
  453. priv->switch_file = debugfs_create_file("switch", 0644,
  454. priv->debugfs_root, NULL, &vga_switcheroo_debugfs_fops);
  455. if (!priv->switch_file) {
  456. printk(KERN_ERR "vga_switcheroo: cannot create /sys/kernel/debug/vgaswitcheroo/switch\n");
  457. goto fail;
  458. }
  459. return 0;
  460. fail:
  461. vga_switcheroo_debugfs_fini(priv);
  462. return -1;
  463. }
  464. int vga_switcheroo_process_delayed_switch(void)
  465. {
  466. struct vga_switcheroo_client *client;
  467. int ret;
  468. int err = -EINVAL;
  469. mutex_lock(&vgasr_mutex);
  470. if (!vgasr_priv.delayed_switch_active)
  471. goto err;
  472. printk(KERN_INFO "vga_switcheroo: processing delayed switch to %d\n", vgasr_priv.delayed_client_id);
  473. client = find_client_from_id(&vgasr_priv.clients,
  474. vgasr_priv.delayed_client_id);
  475. if (!client || !check_can_switch())
  476. goto err;
  477. ret = vga_switchto_stage2(client);
  478. if (ret)
  479. printk(KERN_ERR "vga_switcheroo: delayed switching failed stage 2 %d\n", ret);
  480. vgasr_priv.delayed_switch_active = false;
  481. err = 0;
  482. err:
  483. mutex_unlock(&vgasr_mutex);
  484. return err;
  485. }
  486. EXPORT_SYMBOL(vga_switcheroo_process_delayed_switch);
  487. static void vga_switcheroo_power_switch(struct pci_dev *pdev, enum vga_switcheroo_state state)
  488. {
  489. struct vga_switcheroo_client *client;
  490. if (!vgasr_priv.handler->power_state)
  491. return;
  492. client = find_client_from_pci(&vgasr_priv.clients, pdev);
  493. if (!client)
  494. return;
  495. if (!client->driver_power_control)
  496. return;
  497. vgasr_priv.handler->power_state(client->id, state);
  498. }
  499. /* force a PCI device to a certain state - mainly to turn off audio clients */
  500. void vga_switcheroo_set_dynamic_switch(struct pci_dev *pdev, enum vga_switcheroo_state dynamic)
  501. {
  502. struct vga_switcheroo_client *client;
  503. client = find_client_from_pci(&vgasr_priv.clients, pdev);
  504. if (!client)
  505. return;
  506. if (!client->driver_power_control)
  507. return;
  508. client->pwr_state = dynamic;
  509. set_audio_state(client->id, dynamic);
  510. }
  511. EXPORT_SYMBOL(vga_switcheroo_set_dynamic_switch);
  512. /* switcheroo power domain */
  513. static int vga_switcheroo_runtime_suspend(struct device *dev)
  514. {
  515. struct pci_dev *pdev = to_pci_dev(dev);
  516. int ret;
  517. ret = dev->bus->pm->runtime_suspend(dev);
  518. if (ret)
  519. return ret;
  520. vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_OFF);
  521. return 0;
  522. }
  523. static int vga_switcheroo_runtime_resume(struct device *dev)
  524. {
  525. struct pci_dev *pdev = to_pci_dev(dev);
  526. int ret;
  527. vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_ON);
  528. ret = dev->bus->pm->runtime_resume(dev);
  529. if (ret)
  530. return ret;
  531. return 0;
  532. }
  533. /* this version is for the case where the power switch is separate
  534. to the device being powered down. */
  535. int vga_switcheroo_init_domain_pm_ops(struct device *dev, struct dev_pm_domain *domain)
  536. {
  537. /* copy over all the bus versions */
  538. if (dev->bus && dev->bus->pm) {
  539. domain->ops = *dev->bus->pm;
  540. domain->ops.runtime_suspend = vga_switcheroo_runtime_suspend;
  541. domain->ops.runtime_resume = vga_switcheroo_runtime_resume;
  542. dev->pm_domain = domain;
  543. return 0;
  544. }
  545. dev->pm_domain = NULL;
  546. return -EINVAL;
  547. }
  548. EXPORT_SYMBOL(vga_switcheroo_init_domain_pm_ops);
  549. static int vga_switcheroo_runtime_resume_hdmi_audio(struct device *dev)
  550. {
  551. struct pci_dev *pdev = to_pci_dev(dev);
  552. int ret;
  553. struct vga_switcheroo_client *client, *found = NULL;
  554. /* we need to check if we have to switch back on the video
  555. device so the audio device can come back */
  556. list_for_each_entry(client, &vgasr_priv.clients, list) {
  557. if (PCI_SLOT(client->pdev->devfn) == PCI_SLOT(pdev->devfn) && client_is_vga(client)) {
  558. found = client;
  559. ret = pm_runtime_get_sync(&client->pdev->dev);
  560. if (ret) {
  561. if (ret != 1)
  562. return ret;
  563. }
  564. break;
  565. }
  566. }
  567. ret = dev->bus->pm->runtime_resume(dev);
  568. /* put the reference for the gpu */
  569. if (found) {
  570. pm_runtime_mark_last_busy(&found->pdev->dev);
  571. pm_runtime_put_autosuspend(&found->pdev->dev);
  572. }
  573. return ret;
  574. }
  575. int vga_switcheroo_init_domain_pm_optimus_hdmi_audio(struct device *dev, struct dev_pm_domain *domain)
  576. {
  577. /* copy over all the bus versions */
  578. if (dev->bus && dev->bus->pm) {
  579. domain->ops = *dev->bus->pm;
  580. domain->ops.runtime_resume = vga_switcheroo_runtime_resume_hdmi_audio;
  581. dev->pm_domain = domain;
  582. return 0;
  583. }
  584. dev->pm_domain = NULL;
  585. return -EINVAL;
  586. }
  587. EXPORT_SYMBOL(vga_switcheroo_init_domain_pm_optimus_hdmi_audio);