Merge tag 'docs-5.15-2' of git://git.lwn.net/linux
[platform/kernel/linux-starfive.git] / sound / arm / pxa2xx-ac97.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * linux/sound/pxa2xx-ac97.c -- AC97 support for the Intel PXA2xx chip.
4  *
5  * Author:      Nicolas Pitre
6  * Created:     Dec 02, 2004
7  * Copyright:   MontaVista Software Inc.
8  */
9
10 #include <linux/init.h>
11 #include <linux/io.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/dmaengine.h>
15 #include <linux/dma-mapping.h>
16
17 #include <sound/core.h>
18 #include <sound/pcm.h>
19 #include <sound/ac97_codec.h>
20 #include <sound/initval.h>
21 #include <sound/pxa2xx-lib.h>
22 #include <sound/dmaengine_pcm.h>
23
24 #include <mach/regs-ac97.h>
25 #include <mach/audio.h>
26
27 static void pxa2xx_ac97_legacy_reset(struct snd_ac97 *ac97)
28 {
29         if (!pxa2xx_ac97_try_cold_reset())
30                 pxa2xx_ac97_try_warm_reset();
31
32         pxa2xx_ac97_finish_reset();
33 }
34
35 static unsigned short pxa2xx_ac97_legacy_read(struct snd_ac97 *ac97,
36                                               unsigned short reg)
37 {
38         int ret;
39
40         ret = pxa2xx_ac97_read(ac97->num, reg);
41         if (ret < 0)
42                 return 0;
43         else
44                 return (unsigned short)(ret & 0xffff);
45 }
46
47 static void pxa2xx_ac97_legacy_write(struct snd_ac97 *ac97,
48                                      unsigned short reg, unsigned short val)
49 {
50         pxa2xx_ac97_write(ac97->num, reg, val);
51 }
52
53 static const struct snd_ac97_bus_ops pxa2xx_ac97_ops = {
54         .read   = pxa2xx_ac97_legacy_read,
55         .write  = pxa2xx_ac97_legacy_write,
56         .reset  = pxa2xx_ac97_legacy_reset,
57 };
58
59 static struct snd_pcm *pxa2xx_ac97_pcm;
60 static struct snd_ac97 *pxa2xx_ac97_ac97;
61
62 static int pxa2xx_ac97_pcm_open(struct snd_pcm_substream *substream)
63 {
64         struct snd_pcm_runtime *runtime = substream->runtime;
65         pxa2xx_audio_ops_t *platform_ops;
66         int ret, i;
67
68         ret = pxa2xx_pcm_open(substream);
69         if (ret)
70                 return ret;
71
72         runtime->hw.channels_min = 2;
73         runtime->hw.channels_max = 2;
74
75         i = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
76                 AC97_RATES_FRONT_DAC : AC97_RATES_ADC;
77         runtime->hw.rates = pxa2xx_ac97_ac97->rates[i];
78         snd_pcm_limit_hw_rates(runtime);
79
80         platform_ops = substream->pcm->card->dev->platform_data;
81         if (platform_ops && platform_ops->startup) {
82                 ret = platform_ops->startup(substream, platform_ops->priv);
83                 if (ret < 0)
84                         pxa2xx_pcm_close(substream);
85         }
86
87         return ret;
88 }
89
90 static int pxa2xx_ac97_pcm_close(struct snd_pcm_substream *substream)
91 {
92         pxa2xx_audio_ops_t *platform_ops;
93
94         platform_ops = substream->pcm->card->dev->platform_data;
95         if (platform_ops && platform_ops->shutdown)
96                 platform_ops->shutdown(substream, platform_ops->priv);
97
98         return 0;
99 }
100
101 static int pxa2xx_ac97_pcm_prepare(struct snd_pcm_substream *substream)
102 {
103         struct snd_pcm_runtime *runtime = substream->runtime;
104         int reg = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
105                   AC97_PCM_FRONT_DAC_RATE : AC97_PCM_LR_ADC_RATE;
106         int ret;
107
108         ret = pxa2xx_pcm_prepare(substream);
109         if (ret < 0)
110                 return ret;
111
112         return snd_ac97_set_rate(pxa2xx_ac97_ac97, reg, runtime->rate);
113 }
114
115 #ifdef CONFIG_PM_SLEEP
116
117 static int pxa2xx_ac97_do_suspend(struct snd_card *card)
118 {
119         pxa2xx_audio_ops_t *platform_ops = card->dev->platform_data;
120
121         snd_power_change_state(card, SNDRV_CTL_POWER_D3cold);
122         snd_ac97_suspend(pxa2xx_ac97_ac97);
123         if (platform_ops && platform_ops->suspend)
124                 platform_ops->suspend(platform_ops->priv);
125
126         return pxa2xx_ac97_hw_suspend();
127 }
128
129 static int pxa2xx_ac97_do_resume(struct snd_card *card)
130 {
131         pxa2xx_audio_ops_t *platform_ops = card->dev->platform_data;
132         int rc;
133
134         rc = pxa2xx_ac97_hw_resume();
135         if (rc)
136                 return rc;
137
138         if (platform_ops && platform_ops->resume)
139                 platform_ops->resume(platform_ops->priv);
140         snd_ac97_resume(pxa2xx_ac97_ac97);
141         snd_power_change_state(card, SNDRV_CTL_POWER_D0);
142
143         return 0;
144 }
145
146 static int pxa2xx_ac97_suspend(struct device *dev)
147 {
148         struct snd_card *card = dev_get_drvdata(dev);
149         int ret = 0;
150
151         if (card)
152                 ret = pxa2xx_ac97_do_suspend(card);
153
154         return ret;
155 }
156
157 static int pxa2xx_ac97_resume(struct device *dev)
158 {
159         struct snd_card *card = dev_get_drvdata(dev);
160         int ret = 0;
161
162         if (card)
163                 ret = pxa2xx_ac97_do_resume(card);
164
165         return ret;
166 }
167
168 static SIMPLE_DEV_PM_OPS(pxa2xx_ac97_pm_ops, pxa2xx_ac97_suspend, pxa2xx_ac97_resume);
169 #endif
170
171 static const struct snd_pcm_ops pxa2xx_ac97_pcm_ops = {
172         .open           = pxa2xx_ac97_pcm_open,
173         .close          = pxa2xx_ac97_pcm_close,
174         .hw_params      = pxa2xx_pcm_hw_params,
175         .prepare        = pxa2xx_ac97_pcm_prepare,
176         .trigger        = pxa2xx_pcm_trigger,
177         .pointer        = pxa2xx_pcm_pointer,
178 };
179
180
181 static int pxa2xx_ac97_pcm_new(struct snd_card *card)
182 {
183         struct snd_pcm *pcm;
184         int ret;
185
186         ret = snd_pcm_new(card, "PXA2xx-PCM", 0, 1, 1, &pcm);
187         if (ret)
188                 goto out;
189
190         ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32));
191         if (ret)
192                 goto out;
193
194         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &pxa2xx_ac97_pcm_ops);
195         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &pxa2xx_ac97_pcm_ops);
196         ret = pxa2xx_pcm_preallocate_dma_buffer(pcm);
197         if (ret)
198                 goto out;
199
200         pxa2xx_ac97_pcm = pcm;
201         ret = 0;
202
203  out:
204         return ret;
205 }
206
207 static int pxa2xx_ac97_probe(struct platform_device *dev)
208 {
209         struct snd_card *card;
210         struct snd_ac97_bus *ac97_bus;
211         struct snd_ac97_template ac97_template;
212         int ret;
213         pxa2xx_audio_ops_t *pdata = dev->dev.platform_data;
214
215         if (dev->id >= 0) {
216                 dev_err(&dev->dev, "PXA2xx has only one AC97 port.\n");
217                 ret = -ENXIO;
218                 goto err_dev;
219         }
220
221         ret = snd_card_new(&dev->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
222                            THIS_MODULE, 0, &card);
223         if (ret < 0)
224                 goto err;
225
226         strscpy(card->driver, dev->dev.driver->name, sizeof(card->driver));
227
228         ret = pxa2xx_ac97_pcm_new(card);
229         if (ret)
230                 goto err;
231
232         ret = pxa2xx_ac97_hw_probe(dev);
233         if (ret)
234                 goto err;
235
236         ret = snd_ac97_bus(card, 0, &pxa2xx_ac97_ops, NULL, &ac97_bus);
237         if (ret)
238                 goto err_remove;
239         memset(&ac97_template, 0, sizeof(ac97_template));
240         ret = snd_ac97_mixer(ac97_bus, &ac97_template, &pxa2xx_ac97_ac97);
241         if (ret)
242                 goto err_remove;
243
244         snprintf(card->shortname, sizeof(card->shortname),
245                  "%s", snd_ac97_get_short_name(pxa2xx_ac97_ac97));
246         snprintf(card->longname, sizeof(card->longname),
247                  "%s (%s)", dev->dev.driver->name, card->mixername);
248
249         if (pdata && pdata->codec_pdata[0])
250                 snd_ac97_dev_add_pdata(ac97_bus->codec[0], pdata->codec_pdata[0]);
251         ret = snd_card_register(card);
252         if (ret == 0) {
253                 platform_set_drvdata(dev, card);
254                 return 0;
255         }
256
257 err_remove:
258         pxa2xx_ac97_hw_remove(dev);
259 err:
260         if (card)
261                 snd_card_free(card);
262 err_dev:
263         return ret;
264 }
265
266 static int pxa2xx_ac97_remove(struct platform_device *dev)
267 {
268         struct snd_card *card = platform_get_drvdata(dev);
269
270         if (card) {
271                 snd_card_free(card);
272                 pxa2xx_ac97_hw_remove(dev);
273         }
274
275         return 0;
276 }
277
278 static struct platform_driver pxa2xx_ac97_driver = {
279         .probe          = pxa2xx_ac97_probe,
280         .remove         = pxa2xx_ac97_remove,
281         .driver         = {
282                 .name   = "pxa2xx-ac97",
283 #ifdef CONFIG_PM_SLEEP
284                 .pm     = &pxa2xx_ac97_pm_ops,
285 #endif
286         },
287 };
288
289 module_platform_driver(pxa2xx_ac97_driver);
290
291 MODULE_AUTHOR("Nicolas Pitre");
292 MODULE_DESCRIPTION("AC97 driver for the Intel PXA2xx chip");
293 MODULE_LICENSE("GPL");
294 MODULE_ALIAS("platform:pxa2xx-ac97");