xref: /openbmc/linux/sound/pci/cs5535audio/cs5535audio_pm.c (revision 96de0e252cedffad61b3cb5e05662c591898e69a)
1 /*
2  * Power management for audio on multifunction CS5535 companion device
3  * Copyright (C) Jaya Kumar
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18  *
19  */
20 
21 #include <linux/init.h>
22 #include <linux/slab.h>
23 #include <linux/pci.h>
24 #include <linux/delay.h>
25 #include <sound/driver.h>
26 #include <sound/core.h>
27 #include <sound/control.h>
28 #include <sound/initval.h>
29 #include <sound/asoundef.h>
30 #include <sound/pcm.h>
31 #include <sound/ac97_codec.h>
32 #include "cs5535audio.h"
33 
34 static void snd_cs5535audio_stop_hardware(struct cs5535audio *cs5535au)
35 {
36 	/*
37 	we depend on snd_ac97_suspend to tell the
38 	AC97 codec to shutdown. the amd spec suggests
39 	that the LNK_SHUTDOWN be done at the same time
40 	that the codec power-down is issued. instead,
41 	we do it just after rather than at the same
42 	time. excluding codec specific build_ops->suspend
43 	ac97 powerdown hits:
44 	0x8000 EAPD
45 	0x4000 Headphone amplifier
46 	0x0300 ADC & DAC
47 	0x0400 Analog Mixer powerdown (Vref on)
48 	I am not sure if this is the best that we can do.
49 	The remainder to be investigated are:
50 	- analog mixer (vref off) 0x0800
51 	- AC-link powerdown 0x1000
52 	- codec internal clock 0x2000
53 	*/
54 
55 	/* set LNK_SHUTDOWN to shutdown AC link */
56 	cs_writel(cs5535au, ACC_CODEC_CNTL, ACC_CODEC_CNTL_LNK_SHUTDOWN);
57 
58 }
59 
60 int snd_cs5535audio_suspend(struct pci_dev *pci, pm_message_t state)
61 {
62 	struct snd_card *card = pci_get_drvdata(pci);
63 	struct cs5535audio *cs5535au = card->private_data;
64 	int i;
65 
66 	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
67 	snd_pcm_suspend_all(cs5535au->pcm);
68 	snd_ac97_suspend(cs5535au->ac97);
69 	for (i = 0; i < NUM_CS5535AUDIO_DMAS; i++) {
70 		struct cs5535audio_dma *dma = &cs5535au->dmas[i];
71 		if (dma && dma->substream)
72 			dma->saved_prd = dma->ops->read_prd(cs5535au);
73 	}
74 	/* save important regs, then disable aclink in hw */
75 	snd_cs5535audio_stop_hardware(cs5535au);
76 
77 	if (pci_save_state(pci)) {
78 		printk(KERN_ERR "cs5535audio: pci_save_state failed!\n");
79 		return -EIO;
80 	}
81 	pci_disable_device(pci);
82 	pci_set_power_state(pci, pci_choose_state(pci, state));
83 	return 0;
84 }
85 
86 int snd_cs5535audio_resume(struct pci_dev *pci)
87 {
88 	struct snd_card *card = pci_get_drvdata(pci);
89 	struct cs5535audio *cs5535au = card->private_data;
90 	u32 tmp;
91 	int timeout;
92 	int i;
93 
94 	pci_set_power_state(pci, PCI_D0);
95 	if (pci_restore_state(pci) < 0) {
96 		printk(KERN_ERR "cs5535audio: pci_restore_state failed, "
97 		       "disabling device\n");
98 		snd_card_disconnect(card);
99 		return -EIO;
100 	}
101 	if (pci_enable_device(pci) < 0) {
102 		printk(KERN_ERR "cs5535audio: pci_enable_device failed, "
103 		       "disabling device\n");
104 		snd_card_disconnect(card);
105 		return -EIO;
106 	}
107 	pci_set_master(pci);
108 
109 	/* set LNK_WRM_RST to reset AC link */
110 	cs_writel(cs5535au, ACC_CODEC_CNTL, ACC_CODEC_CNTL_LNK_WRM_RST);
111 
112 	timeout = 50;
113 	do {
114 		tmp = cs_readl(cs5535au, ACC_CODEC_STATUS);
115 		if (tmp & PRM_RDY_STS)
116 			break;
117 		udelay(1);
118 	} while (--timeout);
119 
120 	if (!timeout)
121 		snd_printk(KERN_ERR "Failure getting AC Link ready\n");
122 
123 	/* set up rate regs, dma. actual initiation is done in trig */
124 	for (i = 0; i < NUM_CS5535AUDIO_DMAS; i++) {
125 		struct cs5535audio_dma *dma = &cs5535au->dmas[i];
126 		if (dma && dma->substream) {
127 			dma->substream->ops->prepare(dma->substream);
128 			dma->ops->setup_prd(cs5535au, dma->saved_prd);
129 		}
130 	}
131 
132 	/* we depend on ac97 to perform the codec power up */
133 	snd_ac97_resume(cs5535au->ac97);
134 	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
135 
136 	return 0;
137 }
138 
139