blob: 66ecbd4d034e805263359760aa7796893b4649cd [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +01002/*
Hans-Christian Egtvedt128ed6a2009-04-02 08:21:12 +02003 * Driver for Atmel AC97C
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +01004 *
5 * Copyright (C) 2005-2009 Atmel Corporation
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +01006 */
7#include <linux/clk.h>
8#include <linux/delay.h>
9#include <linux/bitmap.h>
Hans-Christian Egtvedt128ed6a2009-04-02 08:21:12 +020010#include <linux/device.h>
Sedji Gaouaou71773952010-03-01 12:19:18 +010011#include <linux/atmel_pdc.h>
Andy Shevchenko5f10e842017-09-01 19:15:06 +030012#include <linux/gpio/consumer.h>
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +010013#include <linux/init.h>
14#include <linux/interrupt.h>
15#include <linux/module.h>
16#include <linux/platform_device.h>
17#include <linux/mutex.h>
Viresh Kumare2b35f32012-02-01 16:12:27 +053018#include <linux/types.h>
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +010019#include <linux/io.h>
Alexander Steinb2d89572014-12-29 13:08:39 +010020#include <linux/of.h>
Alexander Steinb2d89572014-12-29 13:08:39 +010021#include <linux/of_device.h>
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +010022
23#include <sound/core.h>
24#include <sound/initval.h>
25#include <sound/pcm.h>
26#include <sound/pcm_params.h>
27#include <sound/ac97_codec.h>
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +010028#include <sound/memalloc.h>
29
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +010030#include "ac97c.h"
31
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +010032/* Serialize access to opened variable */
33static DEFINE_MUTEX(opened_mutex);
34
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +010035struct atmel_ac97c {
36 struct clk *pclk;
37 struct platform_device *pdev;
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +010038
39 struct snd_pcm_substream *playback_substream;
40 struct snd_pcm_substream *capture_substream;
41 struct snd_card *card;
42 struct snd_pcm *pcm;
43 struct snd_ac97 *ac97;
44 struct snd_ac97_bus *ac97_bus;
45
46 u64 cur_format;
47 unsigned int cur_rate;
Sedji Gaouaou71773952010-03-01 12:19:18 +010048 int playback_period, capture_period;
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +010049 /* Serialize access to opened variable */
50 spinlock_t lock;
51 void __iomem *regs;
Hans-Christian Egtvedtdf163582009-04-02 08:21:14 +020052 int irq;
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +010053 int opened;
Andy Shevchenko5f10e842017-09-01 19:15:06 +030054 struct gpio_desc *reset_pin;
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +010055};
56
57#define get_chip(card) ((struct atmel_ac97c *)(card)->private_data)
58
59#define ac97c_writel(chip, reg, val) \
60 __raw_writel((val), (chip)->regs + AC97C_##reg)
61#define ac97c_readl(chip, reg) \
62 __raw_readl((chip)->regs + AC97C_##reg)
63
Bhumika Goyal85ab37382017-08-17 14:45:50 +053064static const struct snd_pcm_hardware atmel_ac97c_hw = {
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +010065 .info = (SNDRV_PCM_INFO_MMAP
66 | SNDRV_PCM_INFO_MMAP_VALID
67 | SNDRV_PCM_INFO_INTERLEAVED
68 | SNDRV_PCM_INFO_BLOCK_TRANSFER
69 | SNDRV_PCM_INFO_JOINT_DUPLEX
70 | SNDRV_PCM_INFO_RESUME
71 | SNDRV_PCM_INFO_PAUSE),
72 .formats = (SNDRV_PCM_FMTBIT_S16_BE
73 | SNDRV_PCM_FMTBIT_S16_LE),
74 .rates = (SNDRV_PCM_RATE_CONTINUOUS),
75 .rate_min = 4000,
76 .rate_max = 48000,
77 .channels_min = 1,
78 .channels_max = 2,
Hans-Christian Egtvedtc42eec02009-04-02 08:21:13 +020079 .buffer_bytes_max = 2 * 2 * 64 * 2048,
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +010080 .period_bytes_min = 4096,
81 .period_bytes_max = 4096,
Hans-Christian Egtvedtc42eec02009-04-02 08:21:13 +020082 .periods_min = 6,
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +010083 .periods_max = 64,
84};
85
86static int atmel_ac97c_playback_open(struct snd_pcm_substream *substream)
87{
88 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
89 struct snd_pcm_runtime *runtime = substream->runtime;
90
91 mutex_lock(&opened_mutex);
92 chip->opened++;
93 runtime->hw = atmel_ac97c_hw;
94 if (chip->cur_rate) {
95 runtime->hw.rate_min = chip->cur_rate;
96 runtime->hw.rate_max = chip->cur_rate;
97 }
98 if (chip->cur_format)
Eldad Zack74c34ca2013-04-23 01:00:41 +020099 runtime->hw.formats = pcm_format_to_bits(chip->cur_format);
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100100 mutex_unlock(&opened_mutex);
101 chip->playback_substream = substream;
102 return 0;
103}
104
105static int atmel_ac97c_capture_open(struct snd_pcm_substream *substream)
106{
107 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
108 struct snd_pcm_runtime *runtime = substream->runtime;
109
110 mutex_lock(&opened_mutex);
111 chip->opened++;
112 runtime->hw = atmel_ac97c_hw;
113 if (chip->cur_rate) {
114 runtime->hw.rate_min = chip->cur_rate;
115 runtime->hw.rate_max = chip->cur_rate;
116 }
117 if (chip->cur_format)
Eldad Zack74c34ca2013-04-23 01:00:41 +0200118 runtime->hw.formats = pcm_format_to_bits(chip->cur_format);
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100119 mutex_unlock(&opened_mutex);
120 chip->capture_substream = substream;
121 return 0;
122}
123
124static int atmel_ac97c_playback_close(struct snd_pcm_substream *substream)
125{
126 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
127
128 mutex_lock(&opened_mutex);
129 chip->opened--;
130 if (!chip->opened) {
131 chip->cur_rate = 0;
132 chip->cur_format = 0;
133 }
134 mutex_unlock(&opened_mutex);
135
136 chip->playback_substream = NULL;
137
138 return 0;
139}
140
141static int atmel_ac97c_capture_close(struct snd_pcm_substream *substream)
142{
143 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
144
145 mutex_lock(&opened_mutex);
146 chip->opened--;
147 if (!chip->opened) {
148 chip->cur_rate = 0;
149 chip->cur_format = 0;
150 }
151 mutex_unlock(&opened_mutex);
152
153 chip->capture_substream = NULL;
154
155 return 0;
156}
157
158static int atmel_ac97c_playback_hw_params(struct snd_pcm_substream *substream,
159 struct snd_pcm_hw_params *hw_params)
160{
161 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
Andy Shevchenko020c5262017-05-09 19:18:36 +0300162
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100163 /* Set restrictions to params. */
164 mutex_lock(&opened_mutex);
165 chip->cur_rate = params_rate(hw_params);
166 chip->cur_format = params_format(hw_params);
167 mutex_unlock(&opened_mutex);
168
Takashi Iwai37b9b9a2019-12-09 10:48:35 +0100169 return 0;
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100170}
171
172static int atmel_ac97c_capture_hw_params(struct snd_pcm_substream *substream,
173 struct snd_pcm_hw_params *hw_params)
174{
175 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100176
177 /* Set restrictions to params. */
178 mutex_lock(&opened_mutex);
179 chip->cur_rate = params_rate(hw_params);
180 chip->cur_format = params_format(hw_params);
181 mutex_unlock(&opened_mutex);
182
Takashi Iwai37b9b9a2019-12-09 10:48:35 +0100183 return 0;
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100184}
185
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100186static int atmel_ac97c_playback_prepare(struct snd_pcm_substream *substream)
187{
188 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
189 struct snd_pcm_runtime *runtime = substream->runtime;
Sedji Gaouaou71773952010-03-01 12:19:18 +0100190 int block_size = frames_to_bytes(runtime, runtime->period_size);
Hans-Christian Egtvedt128ed6a2009-04-02 08:21:12 +0200191 unsigned long word = ac97c_readl(chip, OCA);
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100192 int retval;
193
Sedji Gaouaou71773952010-03-01 12:19:18 +0100194 chip->playback_period = 0;
Hans-Christian Egtvedt128ed6a2009-04-02 08:21:12 +0200195 word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT));
196
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100197 /* assign channels to AC97C channel A */
198 switch (runtime->channels) {
199 case 1:
200 word |= AC97C_CH_ASSIGN(PCM_LEFT, A);
201 break;
202 case 2:
203 word |= AC97C_CH_ASSIGN(PCM_LEFT, A)
204 | AC97C_CH_ASSIGN(PCM_RIGHT, A);
205 break;
206 default:
207 /* TODO: support more than two channels */
208 return -EINVAL;
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100209 }
210 ac97c_writel(chip, OCA, word);
211
212 /* configure sample format and size */
Sedji Gaouaouec2755a2010-02-25 18:59:40 +0100213 word = ac97c_readl(chip, CAMR);
214 if (chip->opened <= 1)
215 word = AC97C_CMR_DMAEN | AC97C_CMR_SIZE_16;
216 else
217 word |= AC97C_CMR_DMAEN | AC97C_CMR_SIZE_16;
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100218
219 switch (runtime->format) {
220 case SNDRV_PCM_FORMAT_S16_LE:
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100221 break;
Takashi Iwai4d6cd8f2020-07-09 13:17:48 +0200222 case SNDRV_PCM_FORMAT_S16_BE:
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100223 word &= ~(AC97C_CMR_CEM_LITTLE);
224 break;
Hans-Christian Egtvedt128ed6a2009-04-02 08:21:12 +0200225 default:
226 word = ac97c_readl(chip, OCA);
227 word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT));
228 ac97c_writel(chip, OCA, word);
229 return -EINVAL;
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100230 }
231
Hans-Christian Egtvedtdf163582009-04-02 08:21:14 +0200232 /* Enable underrun interrupt on channel A */
233 word |= AC97C_CSR_UNRUN;
234
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100235 ac97c_writel(chip, CAMR, word);
236
Hans-Christian Egtvedtdf163582009-04-02 08:21:14 +0200237 /* Enable channel A event interrupt */
238 word = ac97c_readl(chip, IMR);
239 word |= AC97C_SR_CAEVT;
240 ac97c_writel(chip, IER, word);
241
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100242 /* set variable rate if needed */
243 if (runtime->rate != 48000) {
244 word = ac97c_readl(chip, MR);
245 word |= AC97C_MR_VRA;
246 ac97c_writel(chip, MR, word);
247 } else {
248 word = ac97c_readl(chip, MR);
249 word &= ~(AC97C_MR_VRA);
250 ac97c_writel(chip, MR, word);
251 }
252
253 retval = snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE,
254 runtime->rate);
255 if (retval)
256 dev_dbg(&chip->pdev->dev, "could not set rate %d Hz\n",
257 runtime->rate);
258
Andy Shevchenko020c5262017-05-09 19:18:36 +0300259 /* Initialize and start the PDC */
260 writel(runtime->dma_addr, chip->regs + ATMEL_PDC_TPR);
261 writel(block_size / 2, chip->regs + ATMEL_PDC_TCR);
262 writel(runtime->dma_addr + block_size, chip->regs + ATMEL_PDC_TNPR);
263 writel(block_size / 2, chip->regs + ATMEL_PDC_TNCR);
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100264
265 return retval;
266}
267
268static int atmel_ac97c_capture_prepare(struct snd_pcm_substream *substream)
269{
270 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
271 struct snd_pcm_runtime *runtime = substream->runtime;
Sedji Gaouaou71773952010-03-01 12:19:18 +0100272 int block_size = frames_to_bytes(runtime, runtime->period_size);
Hans-Christian Egtvedt128ed6a2009-04-02 08:21:12 +0200273 unsigned long word = ac97c_readl(chip, ICA);
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100274 int retval;
275
Sedji Gaouaou71773952010-03-01 12:19:18 +0100276 chip->capture_period = 0;
Hans-Christian Egtvedt128ed6a2009-04-02 08:21:12 +0200277 word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT));
278
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100279 /* assign channels to AC97C channel A */
280 switch (runtime->channels) {
281 case 1:
282 word |= AC97C_CH_ASSIGN(PCM_LEFT, A);
283 break;
284 case 2:
285 word |= AC97C_CH_ASSIGN(PCM_LEFT, A)
286 | AC97C_CH_ASSIGN(PCM_RIGHT, A);
287 break;
288 default:
289 /* TODO: support more than two channels */
290 return -EINVAL;
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100291 }
292 ac97c_writel(chip, ICA, word);
293
294 /* configure sample format and size */
Sedji Gaouaouec2755a2010-02-25 18:59:40 +0100295 word = ac97c_readl(chip, CAMR);
296 if (chip->opened <= 1)
297 word = AC97C_CMR_DMAEN | AC97C_CMR_SIZE_16;
298 else
299 word |= AC97C_CMR_DMAEN | AC97C_CMR_SIZE_16;
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100300
301 switch (runtime->format) {
302 case SNDRV_PCM_FORMAT_S16_LE:
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100303 break;
Takashi Iwai4d6cd8f2020-07-09 13:17:48 +0200304 case SNDRV_PCM_FORMAT_S16_BE:
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100305 word &= ~(AC97C_CMR_CEM_LITTLE);
306 break;
Hans-Christian Egtvedt128ed6a2009-04-02 08:21:12 +0200307 default:
308 word = ac97c_readl(chip, ICA);
309 word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT));
310 ac97c_writel(chip, ICA, word);
311 return -EINVAL;
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100312 }
313
Hans-Christian Egtvedtdf163582009-04-02 08:21:14 +0200314 /* Enable overrun interrupt on channel A */
315 word |= AC97C_CSR_OVRUN;
316
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100317 ac97c_writel(chip, CAMR, word);
318
Hans-Christian Egtvedtdf163582009-04-02 08:21:14 +0200319 /* Enable channel A event interrupt */
320 word = ac97c_readl(chip, IMR);
321 word |= AC97C_SR_CAEVT;
322 ac97c_writel(chip, IER, word);
323
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100324 /* set variable rate if needed */
325 if (runtime->rate != 48000) {
326 word = ac97c_readl(chip, MR);
327 word |= AC97C_MR_VRA;
328 ac97c_writel(chip, MR, word);
329 } else {
330 word = ac97c_readl(chip, MR);
331 word &= ~(AC97C_MR_VRA);
332 ac97c_writel(chip, MR, word);
333 }
334
335 retval = snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE,
336 runtime->rate);
337 if (retval)
338 dev_dbg(&chip->pdev->dev, "could not set rate %d Hz\n",
339 runtime->rate);
340
Andy Shevchenko020c5262017-05-09 19:18:36 +0300341 /* Initialize and start the PDC */
342 writel(runtime->dma_addr, chip->regs + ATMEL_PDC_RPR);
343 writel(block_size / 2, chip->regs + ATMEL_PDC_RCR);
344 writel(runtime->dma_addr + block_size, chip->regs + ATMEL_PDC_RNPR);
345 writel(block_size / 2, chip->regs + ATMEL_PDC_RNCR);
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100346
347 return retval;
348}
349
350static int
351atmel_ac97c_playback_trigger(struct snd_pcm_substream *substream, int cmd)
352{
353 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
Sedji Gaouaou71773952010-03-01 12:19:18 +0100354 unsigned long camr, ptcr = 0;
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100355
356 camr = ac97c_readl(chip, CAMR);
357
358 switch (cmd) {
Gustavo A. R. Silvac0dbbda2020-07-08 15:32:36 -0500359 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
360 case SNDRV_PCM_TRIGGER_RESUME:
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100361 case SNDRV_PCM_TRIGGER_START:
Andy Shevchenko020c5262017-05-09 19:18:36 +0300362 ptcr = ATMEL_PDC_TXTEN;
Sedji Gaouaouec2755a2010-02-25 18:59:40 +0100363 camr |= AC97C_CMR_CENA | AC97C_CSR_ENDTX;
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100364 break;
Gustavo A. R. Silvac0dbbda2020-07-08 15:32:36 -0500365 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
366 case SNDRV_PCM_TRIGGER_SUSPEND:
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100367 case SNDRV_PCM_TRIGGER_STOP:
Andy Shevchenko020c5262017-05-09 19:18:36 +0300368 ptcr |= ATMEL_PDC_TXTDIS;
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100369 if (chip->opened <= 1)
370 camr &= ~AC97C_CMR_CENA;
371 break;
372 default:
Andy Shevchenko020c5262017-05-09 19:18:36 +0300373 return -EINVAL;
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100374 }
375
376 ac97c_writel(chip, CAMR, camr);
Andy Shevchenko020c5262017-05-09 19:18:36 +0300377 writel(ptcr, chip->regs + ATMEL_PDC_PTCR);
378 return 0;
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100379}
380
381static int
382atmel_ac97c_capture_trigger(struct snd_pcm_substream *substream, int cmd)
383{
384 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
Sedji Gaouaou71773952010-03-01 12:19:18 +0100385 unsigned long camr, ptcr = 0;
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100386
387 camr = ac97c_readl(chip, CAMR);
Sedji Gaouaou71773952010-03-01 12:19:18 +0100388 ptcr = readl(chip->regs + ATMEL_PDC_PTSR);
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100389
390 switch (cmd) {
Gustavo A. R. Silvac0dbbda2020-07-08 15:32:36 -0500391 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
392 case SNDRV_PCM_TRIGGER_RESUME:
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100393 case SNDRV_PCM_TRIGGER_START:
Andy Shevchenko020c5262017-05-09 19:18:36 +0300394 ptcr = ATMEL_PDC_RXTEN;
Sedji Gaouaouec2755a2010-02-25 18:59:40 +0100395 camr |= AC97C_CMR_CENA | AC97C_CSR_ENDRX;
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100396 break;
Gustavo A. R. Silvac0dbbda2020-07-08 15:32:36 -0500397 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
398 case SNDRV_PCM_TRIGGER_SUSPEND:
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100399 case SNDRV_PCM_TRIGGER_STOP:
Andy Shevchenko020c5262017-05-09 19:18:36 +0300400 ptcr |= ATMEL_PDC_RXTDIS;
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100401 if (chip->opened <= 1)
402 camr &= ~AC97C_CMR_CENA;
403 break;
404 default:
Andy Shevchenko020c5262017-05-09 19:18:36 +0300405 return -EINVAL;
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100406 }
407
408 ac97c_writel(chip, CAMR, camr);
Andy Shevchenko020c5262017-05-09 19:18:36 +0300409 writel(ptcr, chip->regs + ATMEL_PDC_PTCR);
410 return 0;
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100411}
412
413static snd_pcm_uframes_t
414atmel_ac97c_playback_pointer(struct snd_pcm_substream *substream)
415{
416 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
417 struct snd_pcm_runtime *runtime = substream->runtime;
418 snd_pcm_uframes_t frames;
419 unsigned long bytes;
420
Andy Shevchenko020c5262017-05-09 19:18:36 +0300421 bytes = readl(chip->regs + ATMEL_PDC_TPR);
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100422 bytes -= runtime->dma_addr;
423
424 frames = bytes_to_frames(runtime, bytes);
425 if (frames >= runtime->buffer_size)
426 frames -= runtime->buffer_size;
427 return frames;
428}
429
430static snd_pcm_uframes_t
431atmel_ac97c_capture_pointer(struct snd_pcm_substream *substream)
432{
433 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
434 struct snd_pcm_runtime *runtime = substream->runtime;
435 snd_pcm_uframes_t frames;
436 unsigned long bytes;
437
Andy Shevchenko020c5262017-05-09 19:18:36 +0300438 bytes = readl(chip->regs + ATMEL_PDC_RPR);
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100439 bytes -= runtime->dma_addr;
440
441 frames = bytes_to_frames(runtime, bytes);
442 if (frames >= runtime->buffer_size)
443 frames -= runtime->buffer_size;
444 return frames;
445}
446
Arvind Yadav10863732017-08-18 13:15:11 +0530447static const struct snd_pcm_ops atmel_ac97_playback_ops = {
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100448 .open = atmel_ac97c_playback_open,
449 .close = atmel_ac97c_playback_close,
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100450 .hw_params = atmel_ac97c_playback_hw_params,
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100451 .prepare = atmel_ac97c_playback_prepare,
452 .trigger = atmel_ac97c_playback_trigger,
453 .pointer = atmel_ac97c_playback_pointer,
454};
455
Arvind Yadav10863732017-08-18 13:15:11 +0530456static const struct snd_pcm_ops atmel_ac97_capture_ops = {
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100457 .open = atmel_ac97c_capture_open,
458 .close = atmel_ac97c_capture_close,
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100459 .hw_params = atmel_ac97c_capture_hw_params,
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100460 .prepare = atmel_ac97c_capture_prepare,
461 .trigger = atmel_ac97c_capture_trigger,
462 .pointer = atmel_ac97c_capture_pointer,
463};
464
Hans-Christian Egtvedtdf163582009-04-02 08:21:14 +0200465static irqreturn_t atmel_ac97c_interrupt(int irq, void *dev)
466{
467 struct atmel_ac97c *chip = (struct atmel_ac97c *)dev;
468 irqreturn_t retval = IRQ_NONE;
469 u32 sr = ac97c_readl(chip, SR);
470 u32 casr = ac97c_readl(chip, CASR);
471 u32 cosr = ac97c_readl(chip, COSR);
Sedji Gaouaou71773952010-03-01 12:19:18 +0100472 u32 camr = ac97c_readl(chip, CAMR);
Hans-Christian Egtvedtdf163582009-04-02 08:21:14 +0200473
474 if (sr & AC97C_SR_CAEVT) {
Sedji Gaouaou71773952010-03-01 12:19:18 +0100475 struct snd_pcm_runtime *runtime;
476 int offset, next_period, block_size;
Yegor Yefremovf5341162010-06-08 08:57:13 +0200477 dev_dbg(&chip->pdev->dev, "channel A event%s%s%s%s%s%s\n",
Pierre-Louis Bossarta971b422020-09-02 16:21:29 -0500478 (casr & AC97C_CSR_OVRUN) ? " OVRUN" : "",
479 (casr & AC97C_CSR_RXRDY) ? " RXRDY" : "",
480 (casr & AC97C_CSR_UNRUN) ? " UNRUN" : "",
481 (casr & AC97C_CSR_TXEMPTY) ? " TXEMPTY" : "",
482 (casr & AC97C_CSR_TXRDY) ? " TXRDY" : "",
483 !casr ? " NONE" : "");
Andy Shevchenko020c5262017-05-09 19:18:36 +0300484 if ((casr & camr) & AC97C_CSR_ENDTX) {
485 runtime = chip->playback_substream->runtime;
486 block_size = frames_to_bytes(runtime, runtime->period_size);
487 chip->playback_period++;
Sedji Gaouaou71773952010-03-01 12:19:18 +0100488
Andy Shevchenko020c5262017-05-09 19:18:36 +0300489 if (chip->playback_period == runtime->periods)
490 chip->playback_period = 0;
491 next_period = chip->playback_period + 1;
492 if (next_period == runtime->periods)
493 next_period = 0;
Sedji Gaouaou71773952010-03-01 12:19:18 +0100494
Andy Shevchenko020c5262017-05-09 19:18:36 +0300495 offset = block_size * next_period;
Sedji Gaouaou71773952010-03-01 12:19:18 +0100496
Andy Shevchenko020c5262017-05-09 19:18:36 +0300497 writel(runtime->dma_addr + offset, chip->regs + ATMEL_PDC_TNPR);
498 writel(block_size / 2, chip->regs + ATMEL_PDC_TNCR);
Sedji Gaouaou71773952010-03-01 12:19:18 +0100499
Andy Shevchenko020c5262017-05-09 19:18:36 +0300500 snd_pcm_period_elapsed(chip->playback_substream);
501 }
502 if ((casr & camr) & AC97C_CSR_ENDRX) {
503 runtime = chip->capture_substream->runtime;
504 block_size = frames_to_bytes(runtime, runtime->period_size);
505 chip->capture_period++;
Sedji Gaouaou71773952010-03-01 12:19:18 +0100506
Andy Shevchenko020c5262017-05-09 19:18:36 +0300507 if (chip->capture_period == runtime->periods)
508 chip->capture_period = 0;
509 next_period = chip->capture_period + 1;
510 if (next_period == runtime->periods)
511 next_period = 0;
Sedji Gaouaou71773952010-03-01 12:19:18 +0100512
Andy Shevchenko020c5262017-05-09 19:18:36 +0300513 offset = block_size * next_period;
Sedji Gaouaou71773952010-03-01 12:19:18 +0100514
Andy Shevchenko020c5262017-05-09 19:18:36 +0300515 writel(runtime->dma_addr + offset, chip->regs + ATMEL_PDC_RNPR);
516 writel(block_size / 2, chip->regs + ATMEL_PDC_RNCR);
517 snd_pcm_period_elapsed(chip->capture_substream);
Sedji Gaouaou71773952010-03-01 12:19:18 +0100518 }
Hans-Christian Egtvedtdf163582009-04-02 08:21:14 +0200519 retval = IRQ_HANDLED;
520 }
521
522 if (sr & AC97C_SR_COEVT) {
523 dev_info(&chip->pdev->dev, "codec channel event%s%s%s%s%s\n",
Pierre-Louis Bossarta971b422020-09-02 16:21:29 -0500524 (cosr & AC97C_CSR_OVRUN) ? " OVRUN" : "",
525 (cosr & AC97C_CSR_RXRDY) ? " RXRDY" : "",
526 (cosr & AC97C_CSR_TXEMPTY) ? " TXEMPTY" : "",
527 (cosr & AC97C_CSR_TXRDY) ? " TXRDY" : "",
528 !cosr ? " NONE" : "");
Hans-Christian Egtvedtdf163582009-04-02 08:21:14 +0200529 retval = IRQ_HANDLED;
530 }
531
532 if (retval == IRQ_NONE) {
533 dev_err(&chip->pdev->dev, "spurious interrupt sr 0x%08x "
534 "casr 0x%08x cosr 0x%08x\n", sr, casr, cosr);
535 }
536
537 return retval;
538}
539
Arvind Yadav3ca85902017-08-23 17:39:26 +0530540static const struct ac97_pcm at91_ac97_pcm_defs[] = {
Sedji Gaouaou71773952010-03-01 12:19:18 +0100541 /* Playback */
542 {
543 .exclusive = 1,
544 .r = { {
545 .slots = ((1 << AC97_SLOT_PCM_LEFT)
546 | (1 << AC97_SLOT_PCM_RIGHT)),
547 } },
548 },
549 /* PCM in */
550 {
551 .stream = 1,
552 .exclusive = 1,
553 .r = { {
554 .slots = ((1 << AC97_SLOT_PCM_LEFT)
555 | (1 << AC97_SLOT_PCM_RIGHT)),
556 } }
557 },
558 /* Mic in */
559 {
560 .stream = 1,
561 .exclusive = 1,
562 .r = { {
563 .slots = (1<<AC97_SLOT_MIC),
564 } }
565 },
566};
567
Bill Pemberton61dc6742012-12-06 12:35:13 -0500568static int atmel_ac97c_pcm_new(struct atmel_ac97c *chip)
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100569{
570 struct snd_pcm *pcm;
571 struct snd_pcm_hardware hw = atmel_ac97c_hw;
Andy Shevchenko020c5262017-05-09 19:18:36 +0300572 int retval;
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100573
Andy Shevchenko020c5262017-05-09 19:18:36 +0300574 retval = snd_ac97_pcm_assign(chip->ac97_bus,
575 ARRAY_SIZE(at91_ac97_pcm_defs),
576 at91_ac97_pcm_defs);
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100577 if (retval)
578 return retval;
579
Andy Shevchenko020c5262017-05-09 19:18:36 +0300580 retval = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
581 if (retval)
582 return retval;
583
584 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &atmel_ac97_capture_ops);
585 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &atmel_ac97_playback_ops);
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100586
Takashi Iwai37b9b9a2019-12-09 10:48:35 +0100587 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100588 &chip->pdev->dev, hw.periods_min * hw.period_bytes_min,
589 hw.buffer_bytes_max);
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100590
591 pcm->private_data = chip;
592 pcm->info_flags = 0;
593 strcpy(pcm->name, chip->card->shortname);
594 chip->pcm = pcm;
595
596 return 0;
597}
598
599static int atmel_ac97c_mixer_new(struct atmel_ac97c *chip)
600{
601 struct snd_ac97_template template;
602 memset(&template, 0, sizeof(template));
603 template.private_data = chip;
604 return snd_ac97_mixer(chip->ac97_bus, &template, &chip->ac97);
605}
606
607static void atmel_ac97c_write(struct snd_ac97 *ac97, unsigned short reg,
608 unsigned short val)
609{
610 struct atmel_ac97c *chip = get_chip(ac97);
611 unsigned long word;
612 int timeout = 40;
613
614 word = (reg & 0x7f) << 16 | val;
615
616 do {
617 if (ac97c_readl(chip, COSR) & AC97C_CSR_TXRDY) {
618 ac97c_writel(chip, COTHR, word);
619 return;
620 }
621 udelay(1);
622 } while (--timeout);
623
624 dev_dbg(&chip->pdev->dev, "codec write timeout\n");
625}
626
627static unsigned short atmel_ac97c_read(struct snd_ac97 *ac97,
628 unsigned short reg)
629{
630 struct atmel_ac97c *chip = get_chip(ac97);
631 unsigned long word;
632 int timeout = 40;
633 int write = 10;
634
635 word = (0x80 | (reg & 0x7f)) << 16;
636
637 if ((ac97c_readl(chip, COSR) & AC97C_CSR_RXRDY) != 0)
638 ac97c_readl(chip, CORHR);
639
640retry_write:
641 timeout = 40;
642
643 do {
644 if ((ac97c_readl(chip, COSR) & AC97C_CSR_TXRDY) != 0) {
645 ac97c_writel(chip, COTHR, word);
646 goto read_reg;
647 }
648 udelay(10);
649 } while (--timeout);
650
651 if (!--write)
652 goto timed_out;
653 goto retry_write;
654
655read_reg:
656 do {
657 if ((ac97c_readl(chip, COSR) & AC97C_CSR_RXRDY) != 0) {
658 unsigned short val = ac97c_readl(chip, CORHR);
659 return val;
660 }
661 udelay(10);
662 } while (--timeout);
663
664 if (!--write)
665 goto timed_out;
666 goto retry_write;
667
668timed_out:
669 dev_dbg(&chip->pdev->dev, "codec read timeout\n");
670 return 0xffff;
671}
672
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100673static void atmel_ac97c_reset(struct atmel_ac97c *chip)
674{
Hans-Christian Egtvedt81baf3a2009-04-02 08:21:15 +0200675 ac97c_writel(chip, MR, 0);
676 ac97c_writel(chip, MR, AC97C_MR_ENA);
677 ac97c_writel(chip, CAMR, 0);
678 ac97c_writel(chip, COMR, 0);
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100679
Andy Shevchenko5f10e842017-09-01 19:15:06 +0300680 if (!IS_ERR(chip->reset_pin)) {
681 gpiod_set_value(chip->reset_pin, 0);
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100682 /* AC97 v2.2 specifications says minimum 1 us. */
Hans-Christian Egtvedt81baf3a2009-04-02 08:21:15 +0200683 udelay(2);
Andy Shevchenko5f10e842017-09-01 19:15:06 +0300684 gpiod_set_value(chip->reset_pin, 1);
Bo Shen8015e3d2011-12-19 17:57:52 +0800685 } else {
686 ac97c_writel(chip, MR, AC97C_MR_WRST | AC97C_MR_ENA);
687 udelay(2);
688 ac97c_writel(chip, MR, AC97C_MR_ENA);
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100689 }
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100690}
691
Alexander Steinb2d89572014-12-29 13:08:39 +0100692static const struct of_device_id atmel_ac97c_dt_ids[] = {
693 { .compatible = "atmel,at91sam9263-ac97c", },
694 { }
695};
696MODULE_DEVICE_TABLE(of, atmel_ac97c_dt_ids);
697
Bill Pemberton61dc6742012-12-06 12:35:13 -0500698static int atmel_ac97c_probe(struct platform_device *pdev)
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100699{
Andy Shevchenko5f10e842017-09-01 19:15:06 +0300700 struct device *dev = &pdev->dev;
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100701 struct snd_card *card;
702 struct atmel_ac97c *chip;
703 struct resource *regs;
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100704 struct clk *pclk;
Takashi Iwai3a3fac82020-01-03 09:16:41 +0100705 static const struct snd_ac97_bus_ops ops = {
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100706 .write = atmel_ac97c_write,
707 .read = atmel_ac97c_read,
708 };
709 int retval;
Hans-Christian Egtvedtdf163582009-04-02 08:21:14 +0200710 int irq;
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100711
712 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
713 if (!regs) {
714 dev_dbg(&pdev->dev, "no memory resource\n");
715 return -ENXIO;
716 }
717
Hans-Christian Egtvedtdf163582009-04-02 08:21:14 +0200718 irq = platform_get_irq(pdev, 0);
719 if (irq < 0) {
Gustavo A. R. Silva77201132017-06-30 17:34:23 -0500720 dev_dbg(&pdev->dev, "could not get irq: %d\n", irq);
721 return irq;
Hans-Christian Egtvedtdf163582009-04-02 08:21:14 +0200722 }
723
Andy Shevchenko020c5262017-05-09 19:18:36 +0300724 pclk = clk_get(&pdev->dev, "ac97_clk");
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100725 if (IS_ERR(pclk)) {
726 dev_dbg(&pdev->dev, "no peripheral clock\n");
727 return PTR_ERR(pclk);
728 }
Arvind Yadav260ea952017-07-25 15:45:37 +0530729 retval = clk_prepare_enable(pclk);
730 if (retval)
Christophe Jaillet05157602017-08-31 06:40:42 +0200731 goto err_prepare_enable;
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100732
Takashi Iwaia4f24732014-01-29 14:26:22 +0100733 retval = snd_card_new(&pdev->dev, SNDRV_DEFAULT_IDX1,
734 SNDRV_DEFAULT_STR1, THIS_MODULE,
735 sizeof(struct atmel_ac97c), &card);
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100736 if (retval) {
737 dev_dbg(&pdev->dev, "could not create sound card device\n");
738 goto err_snd_card_new;
739 }
740
741 chip = get_chip(card);
742
Hans-Christian Egtvedtdf163582009-04-02 08:21:14 +0200743 retval = request_irq(irq, atmel_ac97c_interrupt, 0, "AC97C", chip);
744 if (retval) {
745 dev_dbg(&pdev->dev, "unable to request irq %d\n", irq);
746 goto err_request_irq;
747 }
748 chip->irq = irq;
749
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100750 spin_lock_init(&chip->lock);
751
752 strcpy(card->driver, "Atmel AC97C");
753 strcpy(card->shortname, "Atmel AC97C");
754 sprintf(card->longname, "Atmel AC97 controller");
755
756 chip->card = card;
757 chip->pclk = pclk;
758 chip->pdev = pdev;
Joe Perches28f65c112011-06-09 09:13:32 -0700759 chip->regs = ioremap(regs->start, resource_size(regs));
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100760
761 if (!chip->regs) {
762 dev_dbg(&pdev->dev, "could not remap register memory\n");
Julia Lawall0c23e462012-08-19 09:02:57 +0200763 retval = -ENOMEM;
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100764 goto err_ioremap;
765 }
766
Andy Shevchenko5f10e842017-09-01 19:15:06 +0300767 chip->reset_pin = devm_gpiod_get_index(dev, "ac97", 2, GPIOD_OUT_HIGH);
768 if (IS_ERR(chip->reset_pin))
769 dev_dbg(dev, "reset pin not available\n");
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100770
Hans-Christian Egtvedt81baf3a2009-04-02 08:21:15 +0200771 atmel_ac97c_reset(chip);
772
Hans-Christian Egtvedtdf163582009-04-02 08:21:14 +0200773 /* Enable overrun interrupt from codec channel */
774 ac97c_writel(chip, COMR, AC97C_CSR_OVRUN);
775 ac97c_writel(chip, IER, ac97c_readl(chip, IMR) | AC97C_SR_COEVT);
776
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100777 retval = snd_ac97_bus(card, 0, &ops, chip, &chip->ac97_bus);
778 if (retval) {
779 dev_dbg(&pdev->dev, "could not register on ac97 bus\n");
780 goto err_ac97_bus;
781 }
782
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100783 retval = atmel_ac97c_mixer_new(chip);
784 if (retval) {
785 dev_dbg(&pdev->dev, "could not register ac97 mixer\n");
786 goto err_ac97_bus;
787 }
788
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100789 retval = atmel_ac97c_pcm_new(chip);
790 if (retval) {
791 dev_dbg(&pdev->dev, "could not register ac97 pcm device\n");
Andy Shevchenko020c5262017-05-09 19:18:36 +0300792 goto err_ac97_bus;
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100793 }
794
795 retval = snd_card_register(card);
796 if (retval) {
797 dev_dbg(&pdev->dev, "could not register sound card\n");
Andy Shevchenko020c5262017-05-09 19:18:36 +0300798 goto err_ac97_bus;
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100799 }
800
801 platform_set_drvdata(pdev, card);
802
Sedji Gaouaou71773952010-03-01 12:19:18 +0100803 dev_info(&pdev->dev, "Atmel AC97 controller at 0x%p, irq = %d\n",
804 chip->regs, irq);
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100805
806 return 0;
807
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100808err_ac97_bus:
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100809 iounmap(chip->regs);
810err_ioremap:
Hans-Christian Egtvedtdf163582009-04-02 08:21:14 +0200811 free_irq(irq, chip);
812err_request_irq:
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100813 snd_card_free(card);
814err_snd_card_new:
Alexander Stein11320152014-12-05 20:10:06 +0100815 clk_disable_unprepare(pclk);
Christophe Jaillet05157602017-08-31 06:40:42 +0200816err_prepare_enable:
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100817 clk_put(pclk);
818 return retval;
819}
820
Takashi Iwaid34e4e02012-08-09 15:47:15 +0200821#ifdef CONFIG_PM_SLEEP
Takashi Iwai284e7ca2012-07-02 11:22:40 +0200822static int atmel_ac97c_suspend(struct device *pdev)
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100823{
Takashi Iwai284e7ca2012-07-02 11:22:40 +0200824 struct snd_card *card = dev_get_drvdata(pdev);
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100825 struct atmel_ac97c *chip = card->private_data;
826
Alexander Stein11320152014-12-05 20:10:06 +0100827 clk_disable_unprepare(chip->pclk);
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100828 return 0;
829}
830
Takashi Iwai284e7ca2012-07-02 11:22:40 +0200831static int atmel_ac97c_resume(struct device *pdev)
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100832{
Takashi Iwai284e7ca2012-07-02 11:22:40 +0200833 struct snd_card *card = dev_get_drvdata(pdev);
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100834 struct atmel_ac97c *chip = card->private_data;
Arvind Yadav260ea952017-07-25 15:45:37 +0530835 int ret = clk_prepare_enable(chip->pclk);
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100836
Arvind Yadav260ea952017-07-25 15:45:37 +0530837 return ret;
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100838}
Takashi Iwai284e7ca2012-07-02 11:22:40 +0200839
840static SIMPLE_DEV_PM_OPS(atmel_ac97c_pm, atmel_ac97c_suspend, atmel_ac97c_resume);
841#define ATMEL_AC97C_PM_OPS &atmel_ac97c_pm
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100842#else
Takashi Iwai284e7ca2012-07-02 11:22:40 +0200843#define ATMEL_AC97C_PM_OPS NULL
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100844#endif
845
Bill Pemberton61dc6742012-12-06 12:35:13 -0500846static int atmel_ac97c_remove(struct platform_device *pdev)
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100847{
848 struct snd_card *card = platform_get_drvdata(pdev);
849 struct atmel_ac97c *chip = get_chip(card);
850
Hans-Christian Egtvedtbd74a182009-04-02 08:21:16 +0200851 ac97c_writel(chip, CAMR, 0);
852 ac97c_writel(chip, COMR, 0);
853 ac97c_writel(chip, MR, 0);
854
Alexander Stein11320152014-12-05 20:10:06 +0100855 clk_disable_unprepare(chip->pclk);
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100856 clk_put(chip->pclk);
857 iounmap(chip->regs);
Hans-Christian Egtvedtdf163582009-04-02 08:21:14 +0200858 free_irq(chip->irq, chip);
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100859
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100860 snd_card_free(card);
861
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100862 return 0;
863}
864
865static struct platform_driver atmel_ac97c_driver = {
Alexander Stein4b973ee2014-04-15 19:38:56 +0200866 .probe = atmel_ac97c_probe,
Bill Pemberton61dc6742012-12-06 12:35:13 -0500867 .remove = atmel_ac97c_remove,
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100868 .driver = {
869 .name = "atmel_ac97c",
Takashi Iwai284e7ca2012-07-02 11:22:40 +0200870 .pm = ATMEL_AC97C_PM_OPS,
Andy Shevchenko5f10e842017-09-01 19:15:06 +0300871 .of_match_table = atmel_ac97c_dt_ids,
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100872 },
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100873};
Alexander Stein4b973ee2014-04-15 19:38:56 +0200874module_platform_driver(atmel_ac97c_driver);
Hans-Christian Egtvedt4ede0282009-02-05 13:11:00 +0100875
876MODULE_LICENSE("GPL");
877MODULE_DESCRIPTION("Driver for Atmel AC97 controller");
Hans-Christian Egtvedt0cfae7c2011-06-28 16:59:14 +0200878MODULE_AUTHOR("Hans-Christian Egtvedt <egtvedt@samfundet.no>");