Add the support for audio clients to VGA-switcheroo for handling the
HDMI audio controller together with VGA switching.  The id of the
audio controller should be given explicity at registration time unlike
the video controller.

Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=43155

Signed-off-by: Takashi Iwai <tiwai at suse.de>
---
 drivers/gpu/vga/vga_switcheroo.c |   84 ++++++++++++++++++++++++++++++--------
 include/linux/vga_switcheroo.h   |   10 +++++
 2 files changed, 78 insertions(+), 16 deletions(-)

diff --git a/drivers/gpu/vga/vga_switcheroo.c b/drivers/gpu/vga/vga_switcheroo.c
index c91573f..df25ea6 100644
--- a/drivers/gpu/vga/vga_switcheroo.c
+++ b/drivers/gpu/vga/vga_switcheroo.c
@@ -57,6 +57,11 @@ struct vgasr_priv {
        struct vga_switcheroo_handler *handler;
 };

+#define ID_BIT_AUDIO           0x100
+#define client_is_audio(c)     ((c)->id & ID_BIT_AUDIO)
+#define client_is_vga(c)       ((c)->id == -1 || !client_is_audio(c))
+#define client_id(c)           ((c)->id & ~ID_BIT_AUDIO)
+
 static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv);
 static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv);

@@ -96,6 +101,8 @@ static void vga_switcheroo_enable(void)
        vgasr_priv.handler->init();

        list_for_each_entry(client, &vgasr_priv.clients, list) {
+               if (client->id != -1)
+                       continue;
                ret = vgasr_priv.handler->get_client_id(client->pdev);
                if (ret < 0)
                        return;
@@ -106,10 +113,11 @@ static void vga_switcheroo_enable(void)
        vgasr_priv.active = true;
 }

-int vga_switcheroo_register_client(struct pci_dev *pdev,
-                                  void (*set_gpu_state)(struct pci_dev *pdev, 
enum vga_switcheroo_state),
-                                  void (*reprobe)(struct pci_dev *pdev),
-                                  bool (*can_switch)(struct pci_dev *pdev))
+static int register_client(struct pci_dev *pdev,
+       void (*set_gpu_state)(struct pci_dev *pdev, enum vga_switcheroo_state),
+       void (*reprobe)(struct pci_dev *pdev),
+       bool (*can_switch)(struct pci_dev *pdev),
+       int id, bool active)
 {
        struct vga_switcheroo_client *client;

@@ -122,24 +130,48 @@ int vga_switcheroo_register_client(struct pci_dev *pdev,
        client->set_gpu_state = set_gpu_state;
        client->reprobe = reprobe;
        client->can_switch = can_switch;
-       client->id = -1;
-       if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
-               client->active = true;
+       client->id = id;
+       client->active = active;

        mutex_lock(&vgasr_mutex);
        list_add_tail(&client->list, &vgasr_priv.clients);
-       vgasr_priv.registered_clients++;
+       if (client_is_vga(client))
+               vgasr_priv.registered_clients++;

        /* if we get two clients + handler */
-       if (vgasr_priv.registered_clients == 2 && vgasr_priv.handler) {
+       if (!vgasr_priv.active &&
+           vgasr_priv.registered_clients == 2 && vgasr_priv.handler) {
                printk(KERN_INFO "vga_switcheroo: enabled\n");
                vga_switcheroo_enable();
        }
        mutex_unlock(&vgasr_mutex);
        return 0;
 }
+
+int vga_switcheroo_register_client(struct pci_dev *pdev,
+                                  void (*set_gpu_state)(struct pci_dev *pdev, 
enum vga_switcheroo_state),
+                                  void (*reprobe)(struct pci_dev *pdev),
+                                  bool (*can_switch)(struct pci_dev *pdev))
+{
+       bool active = false;
+       if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
+               active = true;
+       return register_client(pdev, set_gpu_state, reprobe, can_switch,
+                              -1, active);
+}
 EXPORT_SYMBOL(vga_switcheroo_register_client);

+int vga_switcheroo_register_audio_client(struct pci_dev *pdev,
+       void (*set_gpu_state)(struct pci_dev *pdev, enum vga_switcheroo_state),
+       void (*reprobe)(struct pci_dev *pdev),
+       bool (*can_switch)(struct pci_dev *pdev),
+       int id, bool active)
+{
+       return register_client(pdev, set_gpu_state, reprobe, can_switch,
+                              id | ID_BIT_AUDIO, active);
+}
+EXPORT_SYMBOL(vga_switcheroo_register_audio_client);
+
 static struct vga_switcheroo_client *
 find_client_from_pci(struct list_head *head, struct pci_dev *pdev)
 {
@@ -165,7 +197,7 @@ find_active_client(struct list_head *head)
 {
        struct vga_switcheroo_client *client;
        list_for_each_entry(client, head, list)
-               if (client->active == true)
+               if (client->active && client_is_vga(client))
                        return client;
        return NULL;
 }
@@ -177,13 +209,16 @@ void vga_switcheroo_unregister_client(struct pci_dev 
*pdev)
        mutex_lock(&vgasr_mutex);
        client = find_client_from_pci(&vgasr_priv.clients, pdev);
        if (client) {
+               if (client_is_vga(client))
+                       vgasr_priv.registered_clients--;
                list_del(&client->list);
                kfree(client);
-               vgasr_priv.registered_clients--;
        }
-       printk(KERN_INFO "vga_switcheroo: disabled\n");
-       vga_switcheroo_debugfs_fini(&vgasr_priv);
-       vgasr_priv.active = false;
+       if (vgasr_priv.active && vgasr_priv.registered_clients < 2) {
+               printk(KERN_INFO "vga_switcheroo: disabled\n");
+               vga_switcheroo_debugfs_fini(&vgasr_priv);
+               vgasr_priv.active = false;
+       }
        mutex_unlock(&vgasr_mutex);
 }
 EXPORT_SYMBOL(vga_switcheroo_unregister_client);
@@ -207,8 +242,9 @@ static int vga_switcheroo_show(struct seq_file *m, void *v)
        int i = 0;
        mutex_lock(&vgasr_mutex);
        list_for_each_entry(client, &vgasr_priv.clients, list) {
-               seq_printf(m, "%d:%s:%c:%s:%s\n", i,
-                          client->id == VGA_SWITCHEROO_DIS ? "DIS" : "IGD",
+               seq_printf(m, "%d:%s%s:%c:%s:%s\n", i,
+                          client_id(client) == VGA_SWITCHEROO_DIS ? "DIS" : 
"IGD",
+                          client_is_vga(client) ? "" : "-Audio",
                           client->active ? '+' : ' ',
                           client->pwr_state ? "Pwr" : "Off",
                           pci_name(client->pdev));
@@ -243,6 +279,17 @@ static int vga_switchoff(struct vga_switcheroo_client 
*client)
        return 0;
 }

+static void set_audio_state(int id, int state)
+{
+       struct vga_switcheroo_client *client;
+
+       client = find_client_from_id(&vgasr_priv.clients, id | ID_BIT_AUDIO);
+       if (client && client->pwr_state != state) {
+               client->set_gpu_state(client->pdev, state);
+               client->pwr_state = state;
+       }
+}
+
 /* stage one happens before delay */
 static int vga_switchto_stage1(struct vga_switcheroo_client *new_client)
 {
@@ -258,6 +305,9 @@ static int vga_switchto_stage1(struct vga_switcheroo_client 
*new_client)
        /* swap shadow resource to denote boot VGA device has changed so X 
starts on new device */
        active->pdev->resource[PCI_ROM_RESOURCE].flags &= 
~IORESOURCE_ROM_SHADOW;
        new_client->pdev->resource[PCI_ROM_RESOURCE].flags |= 
IORESOURCE_ROM_SHADOW;
+
+       set_audio_state(new_client->id, VGA_SWITCHEROO_ON);
+
        return 0;
 }

@@ -286,6 +336,8 @@ static int vga_switchto_stage2(struct vga_switcheroo_client 
*new_client)
        if (new_client->reprobe)
                new_client->reprobe(new_client->pdev);

+       set_audio_state(active->id, VGA_SWITCHEROO_OFF);
+
        if (active->pwr_state == VGA_SWITCHEROO_ON)
                vga_switchoff(active);

diff --git a/include/linux/vga_switcheroo.h b/include/linux/vga_switcheroo.h
index 4b9a7f5..fcbe175 100644
--- a/include/linux/vga_switcheroo.h
+++ b/include/linux/vga_switcheroo.h
@@ -35,6 +35,11 @@ int vga_switcheroo_register_client(struct pci_dev *dev,
                                   void (*set_gpu_state)(struct pci_dev *dev, 
enum vga_switcheroo_state),
                                   void (*reprobe)(struct pci_dev *dev),
                                   bool (*can_switch)(struct pci_dev *dev));
+int vga_switcheroo_register_audio_client(struct pci_dev *pdev,
+       void (*set_gpu_state)(struct pci_dev *pdev, enum vga_switcheroo_state),
+       void (*reprobe)(struct pci_dev *pdev),
+       bool (*can_switch)(struct pci_dev *pdev),
+       int id, bool active);

 void vga_switcheroo_client_fb_set(struct pci_dev *dev,
                                  struct fb_info *info);
@@ -53,6 +58,11 @@ static inline int vga_switcheroo_register_client(struct 
pci_dev *dev,
                                          bool (*can_switch)(struct pci_dev 
*dev)) { return 0; }
 static inline void vga_switcheroo_client_fb_set(struct pci_dev *dev, struct 
fb_info *info) {}
 static inline int vga_switcheroo_register_handler(struct 
vga_switcheroo_handler *handler) { return 0; }
+static inline int vga_switcheroo_register_audio_client(struct pci_dev *pdev,
+       void (*set_gpu_state)(struct pci_dev *pdev, enum vga_switcheroo_state),
+       void (*reprobe)(struct pci_dev *pdev),
+       bool (*can_switch)(struct pci_dev *pdev),
+       int id, bool active) { return 0; }
 static inline void vga_switcheroo_unregister_handler(void) {}
 static inline int vga_switcheroo_process_delayed_switch(void) { return 0; }

-- 
1.7.9.2

Reply via email to