blob: c17a19fe59ed1c9cfa65ffbb92a0749e6fa9b500 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Takashi Iwai2c484df2005-06-30 18:54:04 +02002/*
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.
Takashi Iwai2c484df2005-06-30 18:54:04 +02008 */
9
10#include <linux/init.h>
Rob Herring23019a72012-03-20 14:33:19 -050011#include <linux/io.h>
Takashi Iwai2c484df2005-06-30 18:54:04 +020012#include <linux/module.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010013#include <linux/platform_device.h>
Daniel Mackd65a1452013-08-12 10:42:39 +020014#include <linux/dmaengine.h>
Daniel Mack95acb0052018-06-27 21:33:53 +020015#include <linux/dma-mapping.h>
Takashi Iwai2c484df2005-06-30 18:54:04 +020016
Takashi Iwai2c484df2005-06-30 18:54:04 +020017#include <sound/core.h>
18#include <sound/pcm.h>
19#include <sound/ac97_codec.h>
20#include <sound/initval.h>
Dmitry Baryshkov9c636342008-09-10 05:01:17 +040021#include <sound/pxa2xx-lib.h>
Daniel Mackd65a1452013-08-12 10:42:39 +020022#include <sound/dmaengine_pcm.h>
Takashi Iwai2c484df2005-06-30 18:54:04 +020023
Eric Miao1f017a92008-11-28 14:19:33 +080024#include <mach/regs-ac97.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010025#include <mach/audio.h>
Takashi Iwai2c484df2005-06-30 18:54:04 +020026
Robert Jarzmik6f8acad2017-09-02 21:54:06 +020027static void pxa2xx_ac97_legacy_reset(struct snd_ac97 *ac97)
Takashi Iwai2c484df2005-06-30 18:54:04 +020028{
Robert Jarzmik6f8acad2017-09-02 21:54:06 +020029 if (!pxa2xx_ac97_try_cold_reset())
30 pxa2xx_ac97_try_warm_reset();
Takashi Iwai2c484df2005-06-30 18:54:04 +020031
Robert Jarzmik6f8acad2017-09-02 21:54:06 +020032 pxa2xx_ac97_finish_reset();
33}
34
35static 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
47static void pxa2xx_ac97_legacy_write(struct snd_ac97 *ac97,
48 unsigned short reg, unsigned short val)
49{
zuoqilinf4a85e02021-06-10 10:40:53 +080050 pxa2xx_ac97_write(ac97->num, reg, val);
Takashi Iwai2c484df2005-06-30 18:54:04 +020051}
52
Takashi Iwai74d2bae2020-01-03 09:16:40 +010053static const struct snd_ac97_bus_ops pxa2xx_ac97_ops = {
Robert Jarzmik6f8acad2017-09-02 21:54:06 +020054 .read = pxa2xx_ac97_legacy_read,
55 .write = pxa2xx_ac97_legacy_write,
56 .reset = pxa2xx_ac97_legacy_reset,
Takashi Iwai2c484df2005-06-30 18:54:04 +020057};
58
Takashi Iwaid18f8372005-11-17 15:10:38 +010059static struct snd_pcm *pxa2xx_ac97_pcm;
60static struct snd_ac97 *pxa2xx_ac97_ac97;
Takashi Iwai2c484df2005-06-30 18:54:04 +020061
Daniel Mack95acb0052018-06-27 21:33:53 +020062static int pxa2xx_ac97_pcm_open(struct snd_pcm_substream *substream)
Takashi Iwai2c484df2005-06-30 18:54:04 +020063{
Takashi Iwaid18f8372005-11-17 15:10:38 +010064 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai2c484df2005-06-30 18:54:04 +020065 pxa2xx_audio_ops_t *platform_ops;
Daniel Mack95acb0052018-06-27 21:33:53 +020066 int ret, i;
67
Daniel Macka7160672018-06-27 21:33:54 +020068 ret = pxa2xx_pcm_open(substream);
Daniel Mack95acb0052018-06-27 21:33:53 +020069 if (ret)
70 return ret;
Takashi Iwai2c484df2005-06-30 18:54:04 +020071
72 runtime->hw.channels_min = 2;
73 runtime->hw.channels_max = 2;
74
Daniel Mack95acb0052018-06-27 21:33:53 +020075 i = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
76 AC97_RATES_FRONT_DAC : AC97_RATES_ADC;
77 runtime->hw.rates = pxa2xx_ac97_ac97->rates[i];
Takashi Iwai2c484df2005-06-30 18:54:04 +020078 snd_pcm_limit_hw_rates(runtime);
79
Daniel Mack95acb0052018-06-27 21:33:53 +020080 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)
Daniel Macka7160672018-06-27 21:33:54 +020084 pxa2xx_pcm_close(substream);
Daniel Mack95acb0052018-06-27 21:33:53 +020085 }
86
87 return ret;
Takashi Iwai2c484df2005-06-30 18:54:04 +020088}
89
Daniel Mack95acb0052018-06-27 21:33:53 +020090static int pxa2xx_ac97_pcm_close(struct snd_pcm_substream *substream)
Takashi Iwai2c484df2005-06-30 18:54:04 +020091{
92 pxa2xx_audio_ops_t *platform_ops;
93
Daniel Mack95acb0052018-06-27 21:33:53 +020094 platform_ops = substream->pcm->card->dev->platform_data;
Takashi Iwai2c484df2005-06-30 18:54:04 +020095 if (platform_ops && platform_ops->shutdown)
96 platform_ops->shutdown(substream, platform_ops->priv);
Daniel Mack95acb0052018-06-27 21:33:53 +020097
98 return 0;
Takashi Iwai2c484df2005-06-30 18:54:04 +020099}
100
Takashi Iwaid18f8372005-11-17 15:10:38 +0100101static int pxa2xx_ac97_pcm_prepare(struct snd_pcm_substream *substream)
Takashi Iwai2c484df2005-06-30 18:54:04 +0200102{
Takashi Iwaid18f8372005-11-17 15:10:38 +0100103 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai2c484df2005-06-30 18:54:04 +0200104 int reg = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
105 AC97_PCM_FRONT_DAC_RATE : AC97_PCM_LR_ADC_RATE;
Daniel Mack95acb0052018-06-27 21:33:53 +0200106 int ret;
107
Daniel Macka7160672018-06-27 21:33:54 +0200108 ret = pxa2xx_pcm_prepare(substream);
Daniel Mack95acb0052018-06-27 21:33:53 +0200109 if (ret < 0)
110 return ret;
111
Takashi Iwai2c484df2005-06-30 18:54:04 +0200112 return snd_ac97_set_rate(pxa2xx_ac97_ac97, reg, runtime->rate);
113}
114
Takashi Iwaid34e4e02012-08-09 15:47:15 +0200115#ifdef CONFIG_PM_SLEEP
Takashi Iwai2c484df2005-06-30 18:54:04 +0200116
Takashi Iwai284e7ca2012-07-02 11:22:40 +0200117static int pxa2xx_ac97_do_suspend(struct snd_card *card)
Takashi Iwai2c484df2005-06-30 18:54:04 +0200118{
Takashi Iwai792a6c52005-11-17 17:19:25 +0100119 pxa2xx_audio_ops_t *platform_ops = card->dev->platform_data;
120
121 snd_power_change_state(card, SNDRV_CTL_POWER_D3cold);
Takashi Iwai792a6c52005-11-17 17:19:25 +0100122 snd_ac97_suspend(pxa2xx_ac97_ac97);
123 if (platform_ops && platform_ops->suspend)
124 platform_ops->suspend(platform_ops->priv);
Takashi Iwai2c484df2005-06-30 18:54:04 +0200125
Dmitry Baryshkov9c636342008-09-10 05:01:17 +0400126 return pxa2xx_ac97_hw_suspend();
Takashi Iwai2c484df2005-06-30 18:54:04 +0200127}
128
Takashi Iwaid18f8372005-11-17 15:10:38 +0100129static int pxa2xx_ac97_do_resume(struct snd_card *card)
Takashi Iwai2c484df2005-06-30 18:54:04 +0200130{
Takashi Iwai792a6c52005-11-17 17:19:25 +0100131 pxa2xx_audio_ops_t *platform_ops = card->dev->platform_data;
Dmitry Baryshkov9c636342008-09-10 05:01:17 +0400132 int rc;
Takashi Iwai792a6c52005-11-17 17:19:25 +0100133
Dmitry Baryshkov9c636342008-09-10 05:01:17 +0400134 rc = pxa2xx_ac97_hw_resume();
135 if (rc)
136 return rc;
137
Takashi Iwai792a6c52005-11-17 17:19:25 +0100138 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);
Takashi Iwai2c484df2005-06-30 18:54:04 +0200142
143 return 0;
144}
145
Mike Rapoport2ba9fd02009-07-29 11:59:23 +0300146static int pxa2xx_ac97_suspend(struct device *dev)
Takashi Iwai2c484df2005-06-30 18:54:04 +0200147{
Mike Rapoport2ba9fd02009-07-29 11:59:23 +0300148 struct snd_card *card = dev_get_drvdata(dev);
Takashi Iwai2c484df2005-06-30 18:54:04 +0200149 int ret = 0;
150
Russell King9480e302005-10-28 09:52:56 -0700151 if (card)
Takashi Iwai284e7ca2012-07-02 11:22:40 +0200152 ret = pxa2xx_ac97_do_suspend(card);
Takashi Iwai2c484df2005-06-30 18:54:04 +0200153
154 return ret;
155}
156
Mike Rapoport2ba9fd02009-07-29 11:59:23 +0300157static int pxa2xx_ac97_resume(struct device *dev)
Takashi Iwai2c484df2005-06-30 18:54:04 +0200158{
Mike Rapoport2ba9fd02009-07-29 11:59:23 +0300159 struct snd_card *card = dev_get_drvdata(dev);
Takashi Iwai2c484df2005-06-30 18:54:04 +0200160 int ret = 0;
161
Russell King9480e302005-10-28 09:52:56 -0700162 if (card)
Dirk Opfera55bfdc2005-08-08 16:29:43 +0200163 ret = pxa2xx_ac97_do_resume(card);
Takashi Iwai2c484df2005-06-30 18:54:04 +0200164
165 return ret;
166}
167
Takashi Iwai284e7ca2012-07-02 11:22:40 +0200168static SIMPLE_DEV_PM_OPS(pxa2xx_ac97_pm_ops, pxa2xx_ac97_suspend, pxa2xx_ac97_resume);
Takashi Iwai2c484df2005-06-30 18:54:04 +0200169#endif
170
Daniel Mack7afd1b02018-06-27 21:33:55 +0200171static const struct snd_pcm_ops pxa2xx_ac97_pcm_ops = {
Daniel Mack95acb0052018-06-27 21:33:53 +0200172 .open = pxa2xx_ac97_pcm_open,
173 .close = pxa2xx_ac97_pcm_close,
Daniel Macka7160672018-06-27 21:33:54 +0200174 .hw_params = pxa2xx_pcm_hw_params,
Daniel Mack95acb0052018-06-27 21:33:53 +0200175 .prepare = pxa2xx_ac97_pcm_prepare,
176 .trigger = pxa2xx_pcm_trigger,
177 .pointer = pxa2xx_pcm_pointer,
Daniel Mack95acb0052018-06-27 21:33:53 +0200178};
179
180
181static int pxa2xx_ac97_pcm_new(struct snd_card *card)
182{
183 struct snd_pcm *pcm;
Takashi Iwai7f2da3d2021-08-02 09:28:05 +0200184 int ret;
Daniel Mack95acb0052018-06-27 21:33:53 +0200185
186 ret = snd_pcm_new(card, "PXA2xx-PCM", 0, 1, 1, &pcm);
187 if (ret)
188 goto out;
189
Daniel Mack95acb0052018-06-27 21:33:53 +0200190 ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32));
191 if (ret)
192 goto out;
193
Takashi Iwai7f2da3d2021-08-02 09:28:05 +0200194 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);
Daniel Mack95acb0052018-06-27 21:33:53 +0200197 if (ret)
198 goto out;
199
200 pxa2xx_ac97_pcm = pcm;
201 ret = 0;
202
203 out:
204 return ret;
205}
206
Bill Pembertone21596b2012-12-06 12:35:12 -0500207static int pxa2xx_ac97_probe(struct platform_device *dev)
Takashi Iwai2c484df2005-06-30 18:54:04 +0200208{
Takashi Iwaid18f8372005-11-17 15:10:38 +0100209 struct snd_card *card;
210 struct snd_ac97_bus *ac97_bus;
211 struct snd_ac97_template ac97_template;
Takashi Iwai2c484df2005-06-30 18:54:04 +0200212 int ret;
Marek Vasut4ac04782009-07-30 02:55:01 +0200213 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 }
Takashi Iwai2c484df2005-06-30 18:54:04 +0200220
Takashi Iwai4a875582014-01-29 14:25:18 +0100221 ret = snd_card_new(&dev->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
222 THIS_MODULE, 0, &card);
Takashi Iwaibd7dd772008-12-28 16:45:02 +0100223 if (ret < 0)
Takashi Iwai2c484df2005-06-30 18:54:04 +0200224 goto err;
225
Joe Perches75b1a8f2021-01-04 09:17:34 -0800226 strscpy(card->driver, dev->dev.driver->name, sizeof(card->driver));
Takashi Iwai2c484df2005-06-30 18:54:04 +0200227
Daniel Mack95acb0052018-06-27 21:33:53 +0200228 ret = pxa2xx_ac97_pcm_new(card);
Takashi Iwai2c484df2005-06-30 18:54:04 +0200229 if (ret)
230 goto err;
231
Dmitry Baryshkov9c636342008-09-10 05:01:17 +0400232 ret = pxa2xx_ac97_hw_probe(dev);
233 if (ret)
Takashi Iwai2c484df2005-06-30 18:54:04 +0200234 goto err;
235
Takashi Iwai2c484df2005-06-30 18:54:04 +0200236 ret = snd_ac97_bus(card, 0, &pxa2xx_ac97_ops, NULL, &ac97_bus);
237 if (ret)
Dmitry Baryshkov9c636342008-09-10 05:01:17 +0400238 goto err_remove;
Takashi Iwai2c484df2005-06-30 18:54:04 +0200239 memset(&ac97_template, 0, sizeof(ac97_template));
240 ret = snd_ac97_mixer(ac97_bus, &ac97_template, &pxa2xx_ac97_ac97);
241 if (ret)
Dmitry Baryshkov9c636342008-09-10 05:01:17 +0400242 goto err_remove;
Takashi Iwai2c484df2005-06-30 18:54:04 +0200243
244 snprintf(card->shortname, sizeof(card->shortname),
245 "%s", snd_ac97_get_short_name(pxa2xx_ac97_ac97));
246 snprintf(card->longname, sizeof(card->longname),
Russell King3ae5eae2005-11-09 22:32:44 +0000247 "%s (%s)", dev->dev.driver->name, card->mixername);
Takashi Iwai2c484df2005-06-30 18:54:04 +0200248
Robert Schwebel367da152009-09-04 21:16:36 +0200249 if (pdata && pdata->codec_pdata[0])
Marek Vasute2365bf2009-08-21 20:02:52 +0200250 snd_ac97_dev_add_pdata(ac97_bus->codec[0], pdata->codec_pdata[0]);
Takashi Iwai2c484df2005-06-30 18:54:04 +0200251 ret = snd_card_register(card);
252 if (ret == 0) {
Russell King3ae5eae2005-11-09 22:32:44 +0000253 platform_set_drvdata(dev, card);
Takashi Iwai2c484df2005-06-30 18:54:04 +0200254 return 0;
255 }
256
Dmitry Baryshkov9c636342008-09-10 05:01:17 +0400257err_remove:
258 pxa2xx_ac97_hw_remove(dev);
259err:
Takashi Iwai2c484df2005-06-30 18:54:04 +0200260 if (card)
261 snd_card_free(card);
Marek Vasut4ac04782009-07-30 02:55:01 +0200262err_dev:
Takashi Iwai2c484df2005-06-30 18:54:04 +0200263 return ret;
264}
265
Bill Pembertone21596b2012-12-06 12:35:12 -0500266static int pxa2xx_ac97_remove(struct platform_device *dev)
Takashi Iwai2c484df2005-06-30 18:54:04 +0200267{
Takashi Iwaid18f8372005-11-17 15:10:38 +0100268 struct snd_card *card = platform_get_drvdata(dev);
Takashi Iwai2c484df2005-06-30 18:54:04 +0200269
270 if (card) {
271 snd_card_free(card);
Dmitry Baryshkov9c636342008-09-10 05:01:17 +0400272 pxa2xx_ac97_hw_remove(dev);
Takashi Iwai2c484df2005-06-30 18:54:04 +0200273 }
274
275 return 0;
276}
277
Russell King3ae5eae2005-11-09 22:32:44 +0000278static struct platform_driver pxa2xx_ac97_driver = {
Takashi Iwai2c484df2005-06-30 18:54:04 +0200279 .probe = pxa2xx_ac97_probe,
Bill Pembertone21596b2012-12-06 12:35:12 -0500280 .remove = pxa2xx_ac97_remove,
Russell King3ae5eae2005-11-09 22:32:44 +0000281 .driver = {
282 .name = "pxa2xx-ac97",
Takashi Iwaid34e4e02012-08-09 15:47:15 +0200283#ifdef CONFIG_PM_SLEEP
Mike Rapoport2ba9fd02009-07-29 11:59:23 +0300284 .pm = &pxa2xx_ac97_pm_ops,
285#endif
Russell King3ae5eae2005-11-09 22:32:44 +0000286 },
Takashi Iwai2c484df2005-06-30 18:54:04 +0200287};
288
Axel Lina09452e2011-11-27 16:36:04 +0800289module_platform_driver(pxa2xx_ac97_driver);
Takashi Iwai2c484df2005-06-30 18:54:04 +0200290
291MODULE_AUTHOR("Nicolas Pitre");
292MODULE_DESCRIPTION("AC97 driver for the Intel PXA2xx chip");
293MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +0200294MODULE_ALIAS("platform:pxa2xx-ac97");