1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for Atmel AC97C
5 * Copyright (C) 2005-2009 Atmel Corporation
8 #include <linux/delay.h>
9 #include <linux/bitmap.h>
10 #include <linux/device.h>
11 #include <linux/atmel_pdc.h>
12 #include <linux/gpio/consumer.h>
13 #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>
18 #include <linux/types.h>
21 #include <linux/of_device.h>
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>
28 #include <sound/memalloc.h>
32 /* Serialize access to opened variable */
33 static DEFINE_MUTEX(opened_mutex
);
37 struct platform_device
*pdev
;
39 struct snd_pcm_substream
*playback_substream
;
40 struct snd_pcm_substream
*capture_substream
;
41 struct snd_card
*card
;
43 struct snd_ac97
*ac97
;
44 struct snd_ac97_bus
*ac97_bus
;
47 unsigned int cur_rate
;
48 int playback_period
, capture_period
;
49 /* Serialize access to opened variable */
54 struct gpio_desc
*reset_pin
;
57 #define get_chip(card) ((struct atmel_ac97c *)(card)->private_data)
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)
64 static const struct snd_pcm_hardware atmel_ac97c_hw
= {
65 .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
),
79 .buffer_bytes_max
= 2 * 2 * 64 * 2048,
80 .period_bytes_min
= 4096,
81 .period_bytes_max
= 4096,
86 static int atmel_ac97c_playback_open(struct snd_pcm_substream
*substream
)
88 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
89 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
91 mutex_lock(&opened_mutex
);
93 runtime
->hw
= atmel_ac97c_hw
;
95 runtime
->hw
.rate_min
= chip
->cur_rate
;
96 runtime
->hw
.rate_max
= chip
->cur_rate
;
99 runtime
->hw
.formats
= pcm_format_to_bits(chip
->cur_format
);
100 mutex_unlock(&opened_mutex
);
101 chip
->playback_substream
= substream
;
105 static int atmel_ac97c_capture_open(struct snd_pcm_substream
*substream
)
107 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
108 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
110 mutex_lock(&opened_mutex
);
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
;
117 if (chip
->cur_format
)
118 runtime
->hw
.formats
= pcm_format_to_bits(chip
->cur_format
);
119 mutex_unlock(&opened_mutex
);
120 chip
->capture_substream
= substream
;
124 static int atmel_ac97c_playback_close(struct snd_pcm_substream
*substream
)
126 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
128 mutex_lock(&opened_mutex
);
132 chip
->cur_format
= 0;
134 mutex_unlock(&opened_mutex
);
136 chip
->playback_substream
= NULL
;
141 static int atmel_ac97c_capture_close(struct snd_pcm_substream
*substream
)
143 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
145 mutex_lock(&opened_mutex
);
149 chip
->cur_format
= 0;
151 mutex_unlock(&opened_mutex
);
153 chip
->capture_substream
= NULL
;
158 static int atmel_ac97c_playback_hw_params(struct snd_pcm_substream
*substream
,
159 struct snd_pcm_hw_params
*hw_params
)
161 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
164 retval
= snd_pcm_lib_malloc_pages(substream
,
165 params_buffer_bytes(hw_params
));
169 /* Set restrictions to params. */
170 mutex_lock(&opened_mutex
);
171 chip
->cur_rate
= params_rate(hw_params
);
172 chip
->cur_format
= params_format(hw_params
);
173 mutex_unlock(&opened_mutex
);
178 static int atmel_ac97c_capture_hw_params(struct snd_pcm_substream
*substream
,
179 struct snd_pcm_hw_params
*hw_params
)
181 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
184 retval
= snd_pcm_lib_malloc_pages(substream
,
185 params_buffer_bytes(hw_params
));
189 /* Set restrictions to params. */
190 mutex_lock(&opened_mutex
);
191 chip
->cur_rate
= params_rate(hw_params
);
192 chip
->cur_format
= params_format(hw_params
);
193 mutex_unlock(&opened_mutex
);
198 static int atmel_ac97c_playback_prepare(struct snd_pcm_substream
*substream
)
200 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
201 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
202 int block_size
= frames_to_bytes(runtime
, runtime
->period_size
);
203 unsigned long word
= ac97c_readl(chip
, OCA
);
206 chip
->playback_period
= 0;
207 word
&= ~(AC97C_CH_MASK(PCM_LEFT
) | AC97C_CH_MASK(PCM_RIGHT
));
209 /* assign channels to AC97C channel A */
210 switch (runtime
->channels
) {
212 word
|= AC97C_CH_ASSIGN(PCM_LEFT
, A
);
215 word
|= AC97C_CH_ASSIGN(PCM_LEFT
, A
)
216 | AC97C_CH_ASSIGN(PCM_RIGHT
, A
);
219 /* TODO: support more than two channels */
222 ac97c_writel(chip
, OCA
, word
);
224 /* configure sample format and size */
225 word
= ac97c_readl(chip
, CAMR
);
226 if (chip
->opened
<= 1)
227 word
= AC97C_CMR_DMAEN
| AC97C_CMR_SIZE_16
;
229 word
|= AC97C_CMR_DMAEN
| AC97C_CMR_SIZE_16
;
231 switch (runtime
->format
) {
232 case SNDRV_PCM_FORMAT_S16_LE
:
234 case SNDRV_PCM_FORMAT_S16_BE
: /* fall through */
235 word
&= ~(AC97C_CMR_CEM_LITTLE
);
238 word
= ac97c_readl(chip
, OCA
);
239 word
&= ~(AC97C_CH_MASK(PCM_LEFT
) | AC97C_CH_MASK(PCM_RIGHT
));
240 ac97c_writel(chip
, OCA
, word
);
244 /* Enable underrun interrupt on channel A */
245 word
|= AC97C_CSR_UNRUN
;
247 ac97c_writel(chip
, CAMR
, word
);
249 /* Enable channel A event interrupt */
250 word
= ac97c_readl(chip
, IMR
);
251 word
|= AC97C_SR_CAEVT
;
252 ac97c_writel(chip
, IER
, word
);
254 /* set variable rate if needed */
255 if (runtime
->rate
!= 48000) {
256 word
= ac97c_readl(chip
, MR
);
257 word
|= AC97C_MR_VRA
;
258 ac97c_writel(chip
, MR
, word
);
260 word
= ac97c_readl(chip
, MR
);
261 word
&= ~(AC97C_MR_VRA
);
262 ac97c_writel(chip
, MR
, word
);
265 retval
= snd_ac97_set_rate(chip
->ac97
, AC97_PCM_FRONT_DAC_RATE
,
268 dev_dbg(&chip
->pdev
->dev
, "could not set rate %d Hz\n",
271 /* Initialize and start the PDC */
272 writel(runtime
->dma_addr
, chip
->regs
+ ATMEL_PDC_TPR
);
273 writel(block_size
/ 2, chip
->regs
+ ATMEL_PDC_TCR
);
274 writel(runtime
->dma_addr
+ block_size
, chip
->regs
+ ATMEL_PDC_TNPR
);
275 writel(block_size
/ 2, chip
->regs
+ ATMEL_PDC_TNCR
);
280 static int atmel_ac97c_capture_prepare(struct snd_pcm_substream
*substream
)
282 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
283 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
284 int block_size
= frames_to_bytes(runtime
, runtime
->period_size
);
285 unsigned long word
= ac97c_readl(chip
, ICA
);
288 chip
->capture_period
= 0;
289 word
&= ~(AC97C_CH_MASK(PCM_LEFT
) | AC97C_CH_MASK(PCM_RIGHT
));
291 /* assign channels to AC97C channel A */
292 switch (runtime
->channels
) {
294 word
|= AC97C_CH_ASSIGN(PCM_LEFT
, A
);
297 word
|= AC97C_CH_ASSIGN(PCM_LEFT
, A
)
298 | AC97C_CH_ASSIGN(PCM_RIGHT
, A
);
301 /* TODO: support more than two channels */
304 ac97c_writel(chip
, ICA
, word
);
306 /* configure sample format and size */
307 word
= ac97c_readl(chip
, CAMR
);
308 if (chip
->opened
<= 1)
309 word
= AC97C_CMR_DMAEN
| AC97C_CMR_SIZE_16
;
311 word
|= AC97C_CMR_DMAEN
| AC97C_CMR_SIZE_16
;
313 switch (runtime
->format
) {
314 case SNDRV_PCM_FORMAT_S16_LE
:
316 case SNDRV_PCM_FORMAT_S16_BE
: /* fall through */
317 word
&= ~(AC97C_CMR_CEM_LITTLE
);
320 word
= ac97c_readl(chip
, ICA
);
321 word
&= ~(AC97C_CH_MASK(PCM_LEFT
) | AC97C_CH_MASK(PCM_RIGHT
));
322 ac97c_writel(chip
, ICA
, word
);
326 /* Enable overrun interrupt on channel A */
327 word
|= AC97C_CSR_OVRUN
;
329 ac97c_writel(chip
, CAMR
, word
);
331 /* Enable channel A event interrupt */
332 word
= ac97c_readl(chip
, IMR
);
333 word
|= AC97C_SR_CAEVT
;
334 ac97c_writel(chip
, IER
, word
);
336 /* set variable rate if needed */
337 if (runtime
->rate
!= 48000) {
338 word
= ac97c_readl(chip
, MR
);
339 word
|= AC97C_MR_VRA
;
340 ac97c_writel(chip
, MR
, word
);
342 word
= ac97c_readl(chip
, MR
);
343 word
&= ~(AC97C_MR_VRA
);
344 ac97c_writel(chip
, MR
, word
);
347 retval
= snd_ac97_set_rate(chip
->ac97
, AC97_PCM_LR_ADC_RATE
,
350 dev_dbg(&chip
->pdev
->dev
, "could not set rate %d Hz\n",
353 /* Initialize and start the PDC */
354 writel(runtime
->dma_addr
, chip
->regs
+ ATMEL_PDC_RPR
);
355 writel(block_size
/ 2, chip
->regs
+ ATMEL_PDC_RCR
);
356 writel(runtime
->dma_addr
+ block_size
, chip
->regs
+ ATMEL_PDC_RNPR
);
357 writel(block_size
/ 2, chip
->regs
+ ATMEL_PDC_RNCR
);
363 atmel_ac97c_playback_trigger(struct snd_pcm_substream
*substream
, int cmd
)
365 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
366 unsigned long camr
, ptcr
= 0;
368 camr
= ac97c_readl(chip
, CAMR
);
371 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
: /* fall through */
372 case SNDRV_PCM_TRIGGER_RESUME
: /* fall through */
373 case SNDRV_PCM_TRIGGER_START
:
374 ptcr
= ATMEL_PDC_TXTEN
;
375 camr
|= AC97C_CMR_CENA
| AC97C_CSR_ENDTX
;
377 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
: /* fall through */
378 case SNDRV_PCM_TRIGGER_SUSPEND
: /* fall through */
379 case SNDRV_PCM_TRIGGER_STOP
:
380 ptcr
|= ATMEL_PDC_TXTDIS
;
381 if (chip
->opened
<= 1)
382 camr
&= ~AC97C_CMR_CENA
;
388 ac97c_writel(chip
, CAMR
, camr
);
389 writel(ptcr
, chip
->regs
+ ATMEL_PDC_PTCR
);
394 atmel_ac97c_capture_trigger(struct snd_pcm_substream
*substream
, int cmd
)
396 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
397 unsigned long camr
, ptcr
= 0;
399 camr
= ac97c_readl(chip
, CAMR
);
400 ptcr
= readl(chip
->regs
+ ATMEL_PDC_PTSR
);
403 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
: /* fall through */
404 case SNDRV_PCM_TRIGGER_RESUME
: /* fall through */
405 case SNDRV_PCM_TRIGGER_START
:
406 ptcr
= ATMEL_PDC_RXTEN
;
407 camr
|= AC97C_CMR_CENA
| AC97C_CSR_ENDRX
;
409 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
: /* fall through */
410 case SNDRV_PCM_TRIGGER_SUSPEND
: /* fall through */
411 case SNDRV_PCM_TRIGGER_STOP
:
412 ptcr
|= ATMEL_PDC_RXTDIS
;
413 if (chip
->opened
<= 1)
414 camr
&= ~AC97C_CMR_CENA
;
420 ac97c_writel(chip
, CAMR
, camr
);
421 writel(ptcr
, chip
->regs
+ ATMEL_PDC_PTCR
);
425 static snd_pcm_uframes_t
426 atmel_ac97c_playback_pointer(struct snd_pcm_substream
*substream
)
428 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
429 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
430 snd_pcm_uframes_t frames
;
433 bytes
= readl(chip
->regs
+ ATMEL_PDC_TPR
);
434 bytes
-= runtime
->dma_addr
;
436 frames
= bytes_to_frames(runtime
, bytes
);
437 if (frames
>= runtime
->buffer_size
)
438 frames
-= runtime
->buffer_size
;
442 static snd_pcm_uframes_t
443 atmel_ac97c_capture_pointer(struct snd_pcm_substream
*substream
)
445 struct atmel_ac97c
*chip
= snd_pcm_substream_chip(substream
);
446 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
447 snd_pcm_uframes_t frames
;
450 bytes
= readl(chip
->regs
+ ATMEL_PDC_RPR
);
451 bytes
-= runtime
->dma_addr
;
453 frames
= bytes_to_frames(runtime
, bytes
);
454 if (frames
>= runtime
->buffer_size
)
455 frames
-= runtime
->buffer_size
;
459 static const struct snd_pcm_ops atmel_ac97_playback_ops
= {
460 .open
= atmel_ac97c_playback_open
,
461 .close
= atmel_ac97c_playback_close
,
462 .ioctl
= snd_pcm_lib_ioctl
,
463 .hw_params
= atmel_ac97c_playback_hw_params
,
464 .hw_free
= snd_pcm_lib_free_pages
,
465 .prepare
= atmel_ac97c_playback_prepare
,
466 .trigger
= atmel_ac97c_playback_trigger
,
467 .pointer
= atmel_ac97c_playback_pointer
,
470 static const struct snd_pcm_ops atmel_ac97_capture_ops
= {
471 .open
= atmel_ac97c_capture_open
,
472 .close
= atmel_ac97c_capture_close
,
473 .ioctl
= snd_pcm_lib_ioctl
,
474 .hw_params
= atmel_ac97c_capture_hw_params
,
475 .hw_free
= snd_pcm_lib_free_pages
,
476 .prepare
= atmel_ac97c_capture_prepare
,
477 .trigger
= atmel_ac97c_capture_trigger
,
478 .pointer
= atmel_ac97c_capture_pointer
,
481 static irqreturn_t
atmel_ac97c_interrupt(int irq
, void *dev
)
483 struct atmel_ac97c
*chip
= (struct atmel_ac97c
*)dev
;
484 irqreturn_t retval
= IRQ_NONE
;
485 u32 sr
= ac97c_readl(chip
, SR
);
486 u32 casr
= ac97c_readl(chip
, CASR
);
487 u32 cosr
= ac97c_readl(chip
, COSR
);
488 u32 camr
= ac97c_readl(chip
, CAMR
);
490 if (sr
& AC97C_SR_CAEVT
) {
491 struct snd_pcm_runtime
*runtime
;
492 int offset
, next_period
, block_size
;
493 dev_dbg(&chip
->pdev
->dev
, "channel A event%s%s%s%s%s%s\n",
494 casr
& AC97C_CSR_OVRUN
? " OVRUN" : "",
495 casr
& AC97C_CSR_RXRDY
? " RXRDY" : "",
496 casr
& AC97C_CSR_UNRUN
? " UNRUN" : "",
497 casr
& AC97C_CSR_TXEMPTY
? " TXEMPTY" : "",
498 casr
& AC97C_CSR_TXRDY
? " TXRDY" : "",
499 !casr
? " NONE" : "");
500 if ((casr
& camr
) & AC97C_CSR_ENDTX
) {
501 runtime
= chip
->playback_substream
->runtime
;
502 block_size
= frames_to_bytes(runtime
, runtime
->period_size
);
503 chip
->playback_period
++;
505 if (chip
->playback_period
== runtime
->periods
)
506 chip
->playback_period
= 0;
507 next_period
= chip
->playback_period
+ 1;
508 if (next_period
== runtime
->periods
)
511 offset
= block_size
* next_period
;
513 writel(runtime
->dma_addr
+ offset
, chip
->regs
+ ATMEL_PDC_TNPR
);
514 writel(block_size
/ 2, chip
->regs
+ ATMEL_PDC_TNCR
);
516 snd_pcm_period_elapsed(chip
->playback_substream
);
518 if ((casr
& camr
) & AC97C_CSR_ENDRX
) {
519 runtime
= chip
->capture_substream
->runtime
;
520 block_size
= frames_to_bytes(runtime
, runtime
->period_size
);
521 chip
->capture_period
++;
523 if (chip
->capture_period
== runtime
->periods
)
524 chip
->capture_period
= 0;
525 next_period
= chip
->capture_period
+ 1;
526 if (next_period
== runtime
->periods
)
529 offset
= block_size
* next_period
;
531 writel(runtime
->dma_addr
+ offset
, chip
->regs
+ ATMEL_PDC_RNPR
);
532 writel(block_size
/ 2, chip
->regs
+ ATMEL_PDC_RNCR
);
533 snd_pcm_period_elapsed(chip
->capture_substream
);
535 retval
= IRQ_HANDLED
;
538 if (sr
& AC97C_SR_COEVT
) {
539 dev_info(&chip
->pdev
->dev
, "codec channel event%s%s%s%s%s\n",
540 cosr
& AC97C_CSR_OVRUN
? " OVRUN" : "",
541 cosr
& AC97C_CSR_RXRDY
? " RXRDY" : "",
542 cosr
& AC97C_CSR_TXEMPTY
? " TXEMPTY" : "",
543 cosr
& AC97C_CSR_TXRDY
? " TXRDY" : "",
544 !cosr
? " NONE" : "");
545 retval
= IRQ_HANDLED
;
548 if (retval
== IRQ_NONE
) {
549 dev_err(&chip
->pdev
->dev
, "spurious interrupt sr 0x%08x "
550 "casr 0x%08x cosr 0x%08x\n", sr
, casr
, cosr
);
556 static const struct ac97_pcm at91_ac97_pcm_defs
[] = {
561 .slots
= ((1 << AC97_SLOT_PCM_LEFT
)
562 | (1 << AC97_SLOT_PCM_RIGHT
)),
570 .slots
= ((1 << AC97_SLOT_PCM_LEFT
)
571 | (1 << AC97_SLOT_PCM_RIGHT
)),
579 .slots
= (1<<AC97_SLOT_MIC
),
584 static int atmel_ac97c_pcm_new(struct atmel_ac97c
*chip
)
587 struct snd_pcm_hardware hw
= atmel_ac97c_hw
;
590 retval
= snd_ac97_pcm_assign(chip
->ac97_bus
,
591 ARRAY_SIZE(at91_ac97_pcm_defs
),
596 retval
= snd_pcm_new(chip
->card
, chip
->card
->shortname
, 0, 1, 1, &pcm
);
600 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &atmel_ac97_capture_ops
);
601 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &atmel_ac97_playback_ops
);
603 snd_pcm_lib_preallocate_pages_for_all(pcm
, SNDRV_DMA_TYPE_DEV
,
604 &chip
->pdev
->dev
, hw
.periods_min
* hw
.period_bytes_min
,
605 hw
.buffer_bytes_max
);
607 pcm
->private_data
= chip
;
609 strcpy(pcm
->name
, chip
->card
->shortname
);
615 static int atmel_ac97c_mixer_new(struct atmel_ac97c
*chip
)
617 struct snd_ac97_template
template;
618 memset(&template, 0, sizeof(template));
619 template.private_data
= chip
;
620 return snd_ac97_mixer(chip
->ac97_bus
, &template, &chip
->ac97
);
623 static void atmel_ac97c_write(struct snd_ac97
*ac97
, unsigned short reg
,
626 struct atmel_ac97c
*chip
= get_chip(ac97
);
630 word
= (reg
& 0x7f) << 16 | val
;
633 if (ac97c_readl(chip
, COSR
) & AC97C_CSR_TXRDY
) {
634 ac97c_writel(chip
, COTHR
, word
);
640 dev_dbg(&chip
->pdev
->dev
, "codec write timeout\n");
643 static unsigned short atmel_ac97c_read(struct snd_ac97
*ac97
,
646 struct atmel_ac97c
*chip
= get_chip(ac97
);
651 word
= (0x80 | (reg
& 0x7f)) << 16;
653 if ((ac97c_readl(chip
, COSR
) & AC97C_CSR_RXRDY
) != 0)
654 ac97c_readl(chip
, CORHR
);
660 if ((ac97c_readl(chip
, COSR
) & AC97C_CSR_TXRDY
) != 0) {
661 ac97c_writel(chip
, COTHR
, word
);
673 if ((ac97c_readl(chip
, COSR
) & AC97C_CSR_RXRDY
) != 0) {
674 unsigned short val
= ac97c_readl(chip
, CORHR
);
685 dev_dbg(&chip
->pdev
->dev
, "codec read timeout\n");
689 static void atmel_ac97c_reset(struct atmel_ac97c
*chip
)
691 ac97c_writel(chip
, MR
, 0);
692 ac97c_writel(chip
, MR
, AC97C_MR_ENA
);
693 ac97c_writel(chip
, CAMR
, 0);
694 ac97c_writel(chip
, COMR
, 0);
696 if (!IS_ERR(chip
->reset_pin
)) {
697 gpiod_set_value(chip
->reset_pin
, 0);
698 /* AC97 v2.2 specifications says minimum 1 us. */
700 gpiod_set_value(chip
->reset_pin
, 1);
702 ac97c_writel(chip
, MR
, AC97C_MR_WRST
| AC97C_MR_ENA
);
704 ac97c_writel(chip
, MR
, AC97C_MR_ENA
);
708 static const struct of_device_id atmel_ac97c_dt_ids
[] = {
709 { .compatible
= "atmel,at91sam9263-ac97c", },
712 MODULE_DEVICE_TABLE(of
, atmel_ac97c_dt_ids
);
714 static int atmel_ac97c_probe(struct platform_device
*pdev
)
716 struct device
*dev
= &pdev
->dev
;
717 struct snd_card
*card
;
718 struct atmel_ac97c
*chip
;
719 struct resource
*regs
;
721 static struct snd_ac97_bus_ops ops
= {
722 .write
= atmel_ac97c_write
,
723 .read
= atmel_ac97c_read
,
728 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
730 dev_dbg(&pdev
->dev
, "no memory resource\n");
734 irq
= platform_get_irq(pdev
, 0);
736 dev_dbg(&pdev
->dev
, "could not get irq: %d\n", irq
);
740 pclk
= clk_get(&pdev
->dev
, "ac97_clk");
742 dev_dbg(&pdev
->dev
, "no peripheral clock\n");
743 return PTR_ERR(pclk
);
745 retval
= clk_prepare_enable(pclk
);
747 goto err_prepare_enable
;
749 retval
= snd_card_new(&pdev
->dev
, SNDRV_DEFAULT_IDX1
,
750 SNDRV_DEFAULT_STR1
, THIS_MODULE
,
751 sizeof(struct atmel_ac97c
), &card
);
753 dev_dbg(&pdev
->dev
, "could not create sound card device\n");
754 goto err_snd_card_new
;
757 chip
= get_chip(card
);
759 retval
= request_irq(irq
, atmel_ac97c_interrupt
, 0, "AC97C", chip
);
761 dev_dbg(&pdev
->dev
, "unable to request irq %d\n", irq
);
762 goto err_request_irq
;
766 spin_lock_init(&chip
->lock
);
768 strcpy(card
->driver
, "Atmel AC97C");
769 strcpy(card
->shortname
, "Atmel AC97C");
770 sprintf(card
->longname
, "Atmel AC97 controller");
775 chip
->regs
= ioremap(regs
->start
, resource_size(regs
));
778 dev_dbg(&pdev
->dev
, "could not remap register memory\n");
783 chip
->reset_pin
= devm_gpiod_get_index(dev
, "ac97", 2, GPIOD_OUT_HIGH
);
784 if (IS_ERR(chip
->reset_pin
))
785 dev_dbg(dev
, "reset pin not available\n");
787 atmel_ac97c_reset(chip
);
789 /* Enable overrun interrupt from codec channel */
790 ac97c_writel(chip
, COMR
, AC97C_CSR_OVRUN
);
791 ac97c_writel(chip
, IER
, ac97c_readl(chip
, IMR
) | AC97C_SR_COEVT
);
793 retval
= snd_ac97_bus(card
, 0, &ops
, chip
, &chip
->ac97_bus
);
795 dev_dbg(&pdev
->dev
, "could not register on ac97 bus\n");
799 retval
= atmel_ac97c_mixer_new(chip
);
801 dev_dbg(&pdev
->dev
, "could not register ac97 mixer\n");
805 retval
= atmel_ac97c_pcm_new(chip
);
807 dev_dbg(&pdev
->dev
, "could not register ac97 pcm device\n");
811 retval
= snd_card_register(card
);
813 dev_dbg(&pdev
->dev
, "could not register sound card\n");
817 platform_set_drvdata(pdev
, card
);
819 dev_info(&pdev
->dev
, "Atmel AC97 controller at 0x%p, irq = %d\n",
831 clk_disable_unprepare(pclk
);
837 #ifdef CONFIG_PM_SLEEP
838 static int atmel_ac97c_suspend(struct device
*pdev
)
840 struct snd_card
*card
= dev_get_drvdata(pdev
);
841 struct atmel_ac97c
*chip
= card
->private_data
;
843 clk_disable_unprepare(chip
->pclk
);
847 static int atmel_ac97c_resume(struct device
*pdev
)
849 struct snd_card
*card
= dev_get_drvdata(pdev
);
850 struct atmel_ac97c
*chip
= card
->private_data
;
851 int ret
= clk_prepare_enable(chip
->pclk
);
856 static SIMPLE_DEV_PM_OPS(atmel_ac97c_pm
, atmel_ac97c_suspend
, atmel_ac97c_resume
);
857 #define ATMEL_AC97C_PM_OPS &atmel_ac97c_pm
859 #define ATMEL_AC97C_PM_OPS NULL
862 static int atmel_ac97c_remove(struct platform_device
*pdev
)
864 struct snd_card
*card
= platform_get_drvdata(pdev
);
865 struct atmel_ac97c
*chip
= get_chip(card
);
867 ac97c_writel(chip
, CAMR
, 0);
868 ac97c_writel(chip
, COMR
, 0);
869 ac97c_writel(chip
, MR
, 0);
871 clk_disable_unprepare(chip
->pclk
);
874 free_irq(chip
->irq
, chip
);
881 static struct platform_driver atmel_ac97c_driver
= {
882 .probe
= atmel_ac97c_probe
,
883 .remove
= atmel_ac97c_remove
,
885 .name
= "atmel_ac97c",
886 .pm
= ATMEL_AC97C_PM_OPS
,
887 .of_match_table
= atmel_ac97c_dt_ids
,
890 module_platform_driver(atmel_ac97c_driver
);
892 MODULE_LICENSE("GPL");
893 MODULE_DESCRIPTION("Driver for Atmel AC97 controller");
894 MODULE_AUTHOR("Hans-Christian Egtvedt <egtvedt@samfundet.no>");