xref: /linux/sound/atmel/ac97c.c (revision fab183d632628381b466a41479489541ac0e29a0)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Driver for Atmel AC97C
4  *
5  * Copyright (C) 2005-2009 Atmel Corporation
6  */
7 #include <linux/clk.h>
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/string.h>
19 #include <linux/types.h>
20 #include <linux/io.h>
21 
22 #include <sound/core.h>
23 #include <sound/initval.h>
24 #include <sound/pcm.h>
25 #include <sound/pcm_params.h>
26 #include <sound/ac97_codec.h>
27 #include <sound/memalloc.h>
28 
29 #include "ac97c.h"
30 
31 /* Serialize access to opened variable */
32 static DEFINE_MUTEX(opened_mutex);
33 
34 struct atmel_ac97c {
35 	struct clk			*pclk;
36 	struct platform_device		*pdev;
37 
38 	struct snd_pcm_substream	*playback_substream;
39 	struct snd_pcm_substream	*capture_substream;
40 	struct snd_card			*card;
41 	struct snd_pcm			*pcm;
42 	struct snd_ac97			*ac97;
43 	struct snd_ac97_bus		*ac97_bus;
44 
45 	u64				cur_format;
46 	unsigned int			cur_rate;
47 	int				playback_period, capture_period;
48 	/* Serialize access to opened variable */
49 	spinlock_t			lock;
50 	void __iomem			*regs;
51 	int				irq;
52 	int				opened;
53 	struct gpio_desc		*reset_pin;
54 };
55 
56 #define get_chip(card) ((struct atmel_ac97c *)(card)->private_data)
57 
58 #define ac97c_writel(chip, reg, val)			\
59 	__raw_writel((val), (chip)->regs + AC97C_##reg)
60 #define ac97c_readl(chip, reg)				\
61 	__raw_readl((chip)->regs + AC97C_##reg)
62 
63 static const struct snd_pcm_hardware atmel_ac97c_hw = {
64 	.info			= (SNDRV_PCM_INFO_MMAP
65 				  | SNDRV_PCM_INFO_MMAP_VALID
66 				  | SNDRV_PCM_INFO_INTERLEAVED
67 				  | SNDRV_PCM_INFO_BLOCK_TRANSFER
68 				  | SNDRV_PCM_INFO_JOINT_DUPLEX
69 				  | SNDRV_PCM_INFO_RESUME
70 				  | SNDRV_PCM_INFO_PAUSE),
71 	.formats		= (SNDRV_PCM_FMTBIT_S16_BE
72 				  | SNDRV_PCM_FMTBIT_S16_LE),
73 	.rates			= (SNDRV_PCM_RATE_CONTINUOUS),
74 	.rate_min		= 4000,
75 	.rate_max		= 48000,
76 	.channels_min		= 1,
77 	.channels_max		= 2,
78 	.buffer_bytes_max	= 2 * 2 * 64 * 2048,
79 	.period_bytes_min	= 4096,
80 	.period_bytes_max	= 4096,
81 	.periods_min		= 6,
82 	.periods_max		= 64,
83 };
84 
atmel_ac97c_playback_open(struct snd_pcm_substream * substream)85 static int atmel_ac97c_playback_open(struct snd_pcm_substream *substream)
86 {
87 	struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
88 	struct snd_pcm_runtime *runtime = substream->runtime;
89 
90 	guard(mutex)(&opened_mutex);
91 	chip->opened++;
92 	runtime->hw = atmel_ac97c_hw;
93 	if (chip->cur_rate) {
94 		runtime->hw.rate_min = chip->cur_rate;
95 		runtime->hw.rate_max = chip->cur_rate;
96 	}
97 	if (chip->cur_format)
98 		runtime->hw.formats = pcm_format_to_bits(chip->cur_format);
99 	chip->playback_substream = substream;
100 	return 0;
101 }
102 
atmel_ac97c_capture_open(struct snd_pcm_substream * substream)103 static int atmel_ac97c_capture_open(struct snd_pcm_substream *substream)
104 {
105 	struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
106 	struct snd_pcm_runtime *runtime = substream->runtime;
107 
108 	guard(mutex)(&opened_mutex);
109 	chip->opened++;
110 	runtime->hw = atmel_ac97c_hw;
111 	if (chip->cur_rate) {
112 		runtime->hw.rate_min = chip->cur_rate;
113 		runtime->hw.rate_max = chip->cur_rate;
114 	}
115 	if (chip->cur_format)
116 		runtime->hw.formats = pcm_format_to_bits(chip->cur_format);
117 	chip->capture_substream = substream;
118 	return 0;
119 }
120 
atmel_ac97c_playback_close(struct snd_pcm_substream * substream)121 static int atmel_ac97c_playback_close(struct snd_pcm_substream *substream)
122 {
123 	struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
124 
125 	guard(mutex)(&opened_mutex);
126 	chip->opened--;
127 	if (!chip->opened) {
128 		chip->cur_rate = 0;
129 		chip->cur_format = 0;
130 	}
131 
132 	chip->playback_substream = NULL;
133 
134 	return 0;
135 }
136 
atmel_ac97c_capture_close(struct snd_pcm_substream * substream)137 static int atmel_ac97c_capture_close(struct snd_pcm_substream *substream)
138 {
139 	struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
140 
141 	guard(mutex)(&opened_mutex);
142 	chip->opened--;
143 	if (!chip->opened) {
144 		chip->cur_rate = 0;
145 		chip->cur_format = 0;
146 	}
147 
148 	chip->capture_substream = NULL;
149 
150 	return 0;
151 }
152 
atmel_ac97c_playback_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hw_params)153 static int atmel_ac97c_playback_hw_params(struct snd_pcm_substream *substream,
154 		struct snd_pcm_hw_params *hw_params)
155 {
156 	struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
157 
158 	/* Set restrictions to params. */
159 	guard(mutex)(&opened_mutex);
160 	chip->cur_rate = params_rate(hw_params);
161 	chip->cur_format = params_format(hw_params);
162 
163 	return 0;
164 }
165 
atmel_ac97c_capture_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hw_params)166 static int atmel_ac97c_capture_hw_params(struct snd_pcm_substream *substream,
167 		struct snd_pcm_hw_params *hw_params)
168 {
169 	struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
170 
171 	/* Set restrictions to params. */
172 	guard(mutex)(&opened_mutex);
173 	chip->cur_rate = params_rate(hw_params);
174 	chip->cur_format = params_format(hw_params);
175 
176 	return 0;
177 }
178 
atmel_ac97c_playback_prepare(struct snd_pcm_substream * substream)179 static int atmel_ac97c_playback_prepare(struct snd_pcm_substream *substream)
180 {
181 	struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
182 	struct snd_pcm_runtime *runtime = substream->runtime;
183 	int block_size = frames_to_bytes(runtime, runtime->period_size);
184 	unsigned long word = ac97c_readl(chip, OCA);
185 	int retval;
186 
187 	chip->playback_period = 0;
188 	word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT));
189 
190 	/* assign channels to AC97C channel A */
191 	switch (runtime->channels) {
192 	case 1:
193 		word |= AC97C_CH_ASSIGN(PCM_LEFT, A);
194 		break;
195 	case 2:
196 		word |= AC97C_CH_ASSIGN(PCM_LEFT, A)
197 			| AC97C_CH_ASSIGN(PCM_RIGHT, A);
198 		break;
199 	default:
200 		/* TODO: support more than two channels */
201 		return -EINVAL;
202 	}
203 	ac97c_writel(chip, OCA, word);
204 
205 	/* configure sample format and size */
206 	word = ac97c_readl(chip, CAMR);
207 	if (chip->opened <= 1)
208 		word = AC97C_CMR_DMAEN | AC97C_CMR_SIZE_16;
209 	else
210 		word |= AC97C_CMR_DMAEN | AC97C_CMR_SIZE_16;
211 
212 	switch (runtime->format) {
213 	case SNDRV_PCM_FORMAT_S16_LE:
214 		break;
215 	case SNDRV_PCM_FORMAT_S16_BE:
216 		word &= ~(AC97C_CMR_CEM_LITTLE);
217 		break;
218 	default:
219 		word = ac97c_readl(chip, OCA);
220 		word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT));
221 		ac97c_writel(chip, OCA, word);
222 		return -EINVAL;
223 	}
224 
225 	/* Enable underrun interrupt on channel A */
226 	word |= AC97C_CSR_UNRUN;
227 
228 	ac97c_writel(chip, CAMR, word);
229 
230 	/* Enable channel A event interrupt */
231 	word = ac97c_readl(chip, IMR);
232 	word |= AC97C_SR_CAEVT;
233 	ac97c_writel(chip, IER, word);
234 
235 	/* set variable rate if needed */
236 	if (runtime->rate != 48000) {
237 		word = ac97c_readl(chip, MR);
238 		word |= AC97C_MR_VRA;
239 		ac97c_writel(chip, MR, word);
240 	} else {
241 		word = ac97c_readl(chip, MR);
242 		word &= ~(AC97C_MR_VRA);
243 		ac97c_writel(chip, MR, word);
244 	}
245 
246 	retval = snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE,
247 			runtime->rate);
248 	if (retval)
249 		dev_dbg(&chip->pdev->dev, "could not set rate %d Hz\n",
250 				runtime->rate);
251 
252 	/* Initialize and start the PDC */
253 	writel(runtime->dma_addr, chip->regs + ATMEL_PDC_TPR);
254 	writel(block_size / 2, chip->regs + ATMEL_PDC_TCR);
255 	writel(runtime->dma_addr + block_size, chip->regs + ATMEL_PDC_TNPR);
256 	writel(block_size / 2, chip->regs + ATMEL_PDC_TNCR);
257 
258 	return retval;
259 }
260 
atmel_ac97c_capture_prepare(struct snd_pcm_substream * substream)261 static int atmel_ac97c_capture_prepare(struct snd_pcm_substream *substream)
262 {
263 	struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
264 	struct snd_pcm_runtime *runtime = substream->runtime;
265 	int block_size = frames_to_bytes(runtime, runtime->period_size);
266 	unsigned long word = ac97c_readl(chip, ICA);
267 	int retval;
268 
269 	chip->capture_period = 0;
270 	word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT));
271 
272 	/* assign channels to AC97C channel A */
273 	switch (runtime->channels) {
274 	case 1:
275 		word |= AC97C_CH_ASSIGN(PCM_LEFT, A);
276 		break;
277 	case 2:
278 		word |= AC97C_CH_ASSIGN(PCM_LEFT, A)
279 			| AC97C_CH_ASSIGN(PCM_RIGHT, A);
280 		break;
281 	default:
282 		/* TODO: support more than two channels */
283 		return -EINVAL;
284 	}
285 	ac97c_writel(chip, ICA, word);
286 
287 	/* configure sample format and size */
288 	word = ac97c_readl(chip, CAMR);
289 	if (chip->opened <= 1)
290 		word = AC97C_CMR_DMAEN | AC97C_CMR_SIZE_16;
291 	else
292 		word |= AC97C_CMR_DMAEN | AC97C_CMR_SIZE_16;
293 
294 	switch (runtime->format) {
295 	case SNDRV_PCM_FORMAT_S16_LE:
296 		break;
297 	case SNDRV_PCM_FORMAT_S16_BE:
298 		word &= ~(AC97C_CMR_CEM_LITTLE);
299 		break;
300 	default:
301 		word = ac97c_readl(chip, ICA);
302 		word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT));
303 		ac97c_writel(chip, ICA, word);
304 		return -EINVAL;
305 	}
306 
307 	/* Enable overrun interrupt on channel A */
308 	word |= AC97C_CSR_OVRUN;
309 
310 	ac97c_writel(chip, CAMR, word);
311 
312 	/* Enable channel A event interrupt */
313 	word = ac97c_readl(chip, IMR);
314 	word |= AC97C_SR_CAEVT;
315 	ac97c_writel(chip, IER, word);
316 
317 	/* set variable rate if needed */
318 	if (runtime->rate != 48000) {
319 		word = ac97c_readl(chip, MR);
320 		word |= AC97C_MR_VRA;
321 		ac97c_writel(chip, MR, word);
322 	} else {
323 		word = ac97c_readl(chip, MR);
324 		word &= ~(AC97C_MR_VRA);
325 		ac97c_writel(chip, MR, word);
326 	}
327 
328 	retval = snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE,
329 			runtime->rate);
330 	if (retval)
331 		dev_dbg(&chip->pdev->dev, "could not set rate %d Hz\n",
332 				runtime->rate);
333 
334 	/* Initialize and start the PDC */
335 	writel(runtime->dma_addr, chip->regs + ATMEL_PDC_RPR);
336 	writel(block_size / 2, chip->regs + ATMEL_PDC_RCR);
337 	writel(runtime->dma_addr + block_size, chip->regs + ATMEL_PDC_RNPR);
338 	writel(block_size / 2, chip->regs + ATMEL_PDC_RNCR);
339 
340 	return retval;
341 }
342 
343 static int
atmel_ac97c_playback_trigger(struct snd_pcm_substream * substream,int cmd)344 atmel_ac97c_playback_trigger(struct snd_pcm_substream *substream, int cmd)
345 {
346 	struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
347 	unsigned long camr, ptcr = 0;
348 
349 	camr = ac97c_readl(chip, CAMR);
350 
351 	switch (cmd) {
352 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
353 	case SNDRV_PCM_TRIGGER_RESUME:
354 	case SNDRV_PCM_TRIGGER_START:
355 		ptcr = ATMEL_PDC_TXTEN;
356 		camr |= AC97C_CMR_CENA | AC97C_CSR_ENDTX;
357 		break;
358 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
359 	case SNDRV_PCM_TRIGGER_SUSPEND:
360 	case SNDRV_PCM_TRIGGER_STOP:
361 		ptcr |= ATMEL_PDC_TXTDIS;
362 		if (chip->opened <= 1)
363 			camr &= ~AC97C_CMR_CENA;
364 		break;
365 	default:
366 		return -EINVAL;
367 	}
368 
369 	ac97c_writel(chip, CAMR, camr);
370 	writel(ptcr, chip->regs + ATMEL_PDC_PTCR);
371 	return 0;
372 }
373 
374 static int
atmel_ac97c_capture_trigger(struct snd_pcm_substream * substream,int cmd)375 atmel_ac97c_capture_trigger(struct snd_pcm_substream *substream, int cmd)
376 {
377 	struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
378 	unsigned long camr, ptcr = 0;
379 
380 	camr = ac97c_readl(chip, CAMR);
381 	ptcr = readl(chip->regs + ATMEL_PDC_PTSR);
382 
383 	switch (cmd) {
384 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
385 	case SNDRV_PCM_TRIGGER_RESUME:
386 	case SNDRV_PCM_TRIGGER_START:
387 		ptcr = ATMEL_PDC_RXTEN;
388 		camr |= AC97C_CMR_CENA | AC97C_CSR_ENDRX;
389 		break;
390 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
391 	case SNDRV_PCM_TRIGGER_SUSPEND:
392 	case SNDRV_PCM_TRIGGER_STOP:
393 		ptcr |= ATMEL_PDC_RXTDIS;
394 		if (chip->opened <= 1)
395 			camr &= ~AC97C_CMR_CENA;
396 		break;
397 	default:
398 		return -EINVAL;
399 	}
400 
401 	ac97c_writel(chip, CAMR, camr);
402 	writel(ptcr, chip->regs + ATMEL_PDC_PTCR);
403 	return 0;
404 }
405 
406 static snd_pcm_uframes_t
atmel_ac97c_playback_pointer(struct snd_pcm_substream * substream)407 atmel_ac97c_playback_pointer(struct snd_pcm_substream *substream)
408 {
409 	struct atmel_ac97c	*chip = snd_pcm_substream_chip(substream);
410 	struct snd_pcm_runtime	*runtime = substream->runtime;
411 	snd_pcm_uframes_t	frames;
412 	unsigned long		bytes;
413 
414 	bytes = readl(chip->regs + ATMEL_PDC_TPR);
415 	bytes -= runtime->dma_addr;
416 
417 	frames = bytes_to_frames(runtime, bytes);
418 	if (frames >= runtime->buffer_size)
419 		frames -= runtime->buffer_size;
420 	return frames;
421 }
422 
423 static snd_pcm_uframes_t
atmel_ac97c_capture_pointer(struct snd_pcm_substream * substream)424 atmel_ac97c_capture_pointer(struct snd_pcm_substream *substream)
425 {
426 	struct atmel_ac97c	*chip = snd_pcm_substream_chip(substream);
427 	struct snd_pcm_runtime	*runtime = substream->runtime;
428 	snd_pcm_uframes_t	frames;
429 	unsigned long		bytes;
430 
431 	bytes = readl(chip->regs + ATMEL_PDC_RPR);
432 	bytes -= runtime->dma_addr;
433 
434 	frames = bytes_to_frames(runtime, bytes);
435 	if (frames >= runtime->buffer_size)
436 		frames -= runtime->buffer_size;
437 	return frames;
438 }
439 
440 static const struct snd_pcm_ops atmel_ac97_playback_ops = {
441 	.open		= atmel_ac97c_playback_open,
442 	.close		= atmel_ac97c_playback_close,
443 	.hw_params	= atmel_ac97c_playback_hw_params,
444 	.prepare	= atmel_ac97c_playback_prepare,
445 	.trigger	= atmel_ac97c_playback_trigger,
446 	.pointer	= atmel_ac97c_playback_pointer,
447 };
448 
449 static const struct snd_pcm_ops atmel_ac97_capture_ops = {
450 	.open		= atmel_ac97c_capture_open,
451 	.close		= atmel_ac97c_capture_close,
452 	.hw_params	= atmel_ac97c_capture_hw_params,
453 	.prepare	= atmel_ac97c_capture_prepare,
454 	.trigger	= atmel_ac97c_capture_trigger,
455 	.pointer	= atmel_ac97c_capture_pointer,
456 };
457 
atmel_ac97c_interrupt(int irq,void * dev)458 static irqreturn_t atmel_ac97c_interrupt(int irq, void *dev)
459 {
460 	struct atmel_ac97c	*chip  = (struct atmel_ac97c *)dev;
461 	irqreturn_t		retval = IRQ_NONE;
462 	u32			sr     = ac97c_readl(chip, SR);
463 	u32			casr   = ac97c_readl(chip, CASR);
464 	u32			cosr   = ac97c_readl(chip, COSR);
465 	u32			camr   = ac97c_readl(chip, CAMR);
466 
467 	if (sr & AC97C_SR_CAEVT) {
468 		struct snd_pcm_runtime *runtime;
469 		int offset, next_period, block_size;
470 		dev_dbg(&chip->pdev->dev, "channel A event%s%s%s%s%s%s\n",
471 			(casr & AC97C_CSR_OVRUN)   ? " OVRUN"   : "",
472 			(casr & AC97C_CSR_RXRDY)   ? " RXRDY"   : "",
473 			(casr & AC97C_CSR_UNRUN)   ? " UNRUN"   : "",
474 			(casr & AC97C_CSR_TXEMPTY) ? " TXEMPTY" : "",
475 			(casr & AC97C_CSR_TXRDY)   ? " TXRDY"   : "",
476 			!casr                      ? " NONE"    : "");
477 		if ((casr & camr) & AC97C_CSR_ENDTX) {
478 			runtime = chip->playback_substream->runtime;
479 			block_size = frames_to_bytes(runtime, runtime->period_size);
480 			chip->playback_period++;
481 
482 			if (chip->playback_period == runtime->periods)
483 				chip->playback_period = 0;
484 			next_period = chip->playback_period + 1;
485 			if (next_period == runtime->periods)
486 				next_period = 0;
487 
488 			offset = block_size * next_period;
489 
490 			writel(runtime->dma_addr + offset, chip->regs + ATMEL_PDC_TNPR);
491 			writel(block_size / 2, chip->regs + ATMEL_PDC_TNCR);
492 
493 			snd_pcm_period_elapsed(chip->playback_substream);
494 		}
495 		if ((casr & camr) & AC97C_CSR_ENDRX) {
496 			runtime = chip->capture_substream->runtime;
497 			block_size = frames_to_bytes(runtime, runtime->period_size);
498 			chip->capture_period++;
499 
500 			if (chip->capture_period == runtime->periods)
501 				chip->capture_period = 0;
502 			next_period = chip->capture_period + 1;
503 			if (next_period == runtime->periods)
504 				next_period = 0;
505 
506 			offset = block_size * next_period;
507 
508 			writel(runtime->dma_addr + offset, chip->regs + ATMEL_PDC_RNPR);
509 			writel(block_size / 2, chip->regs + ATMEL_PDC_RNCR);
510 			snd_pcm_period_elapsed(chip->capture_substream);
511 		}
512 		retval = IRQ_HANDLED;
513 	}
514 
515 	if (sr & AC97C_SR_COEVT) {
516 		dev_info(&chip->pdev->dev, "codec channel event%s%s%s%s%s\n",
517 			 (cosr & AC97C_CSR_OVRUN)   ? " OVRUN"   : "",
518 			 (cosr & AC97C_CSR_RXRDY)   ? " RXRDY"   : "",
519 			 (cosr & AC97C_CSR_TXEMPTY) ? " TXEMPTY" : "",
520 			 (cosr & AC97C_CSR_TXRDY)   ? " TXRDY"   : "",
521 			 !cosr                      ? " NONE"    : "");
522 		retval = IRQ_HANDLED;
523 	}
524 
525 	if (retval == IRQ_NONE) {
526 		dev_err(&chip->pdev->dev, "spurious interrupt sr 0x%08x "
527 				"casr 0x%08x cosr 0x%08x\n", sr, casr, cosr);
528 	}
529 
530 	return retval;
531 }
532 
533 static const struct ac97_pcm at91_ac97_pcm_defs[] = {
534 	/* Playback */
535 	{
536 		.exclusive = 1,
537 		.r = { {
538 			.slots = ((1 << AC97_SLOT_PCM_LEFT)
539 				  | (1 << AC97_SLOT_PCM_RIGHT)),
540 		} },
541 	},
542 	/* PCM in */
543 	{
544 		.stream = 1,
545 		.exclusive = 1,
546 		.r = { {
547 			.slots = ((1 << AC97_SLOT_PCM_LEFT)
548 					| (1 << AC97_SLOT_PCM_RIGHT)),
549 		} }
550 	},
551 	/* Mic in */
552 	{
553 		.stream = 1,
554 		.exclusive = 1,
555 		.r = { {
556 			.slots = (1<<AC97_SLOT_MIC),
557 		} }
558 	},
559 };
560 
atmel_ac97c_pcm_new(struct atmel_ac97c * chip)561 static int atmel_ac97c_pcm_new(struct atmel_ac97c *chip)
562 {
563 	struct snd_pcm		*pcm;
564 	struct snd_pcm_hardware	hw = atmel_ac97c_hw;
565 	int			retval;
566 
567 	retval = snd_ac97_pcm_assign(chip->ac97_bus,
568 				     ARRAY_SIZE(at91_ac97_pcm_defs),
569 				     at91_ac97_pcm_defs);
570 	if (retval)
571 		return retval;
572 
573 	retval = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
574 	if (retval)
575 		return retval;
576 
577 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &atmel_ac97_capture_ops);
578 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &atmel_ac97_playback_ops);
579 
580 	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
581 			&chip->pdev->dev, hw.periods_min * hw.period_bytes_min,
582 			hw.buffer_bytes_max);
583 
584 	pcm->private_data = chip;
585 	pcm->info_flags = 0;
586 	strscpy(pcm->name, chip->card->shortname);
587 	chip->pcm = pcm;
588 
589 	return 0;
590 }
591 
atmel_ac97c_mixer_new(struct atmel_ac97c * chip)592 static int atmel_ac97c_mixer_new(struct atmel_ac97c *chip)
593 {
594 	struct snd_ac97_template template;
595 	memset(&template, 0, sizeof(template));
596 	template.private_data = chip;
597 	return snd_ac97_mixer(chip->ac97_bus, &template, &chip->ac97);
598 }
599 
atmel_ac97c_write(struct snd_ac97 * ac97,unsigned short reg,unsigned short val)600 static void atmel_ac97c_write(struct snd_ac97 *ac97, unsigned short reg,
601 		unsigned short val)
602 {
603 	struct atmel_ac97c *chip = get_chip(ac97);
604 	unsigned long word;
605 	int timeout = 40;
606 
607 	word = (reg & 0x7f) << 16 | val;
608 
609 	do {
610 		if (ac97c_readl(chip, COSR) & AC97C_CSR_TXRDY) {
611 			ac97c_writel(chip, COTHR, word);
612 			return;
613 		}
614 		udelay(1);
615 	} while (--timeout);
616 
617 	dev_dbg(&chip->pdev->dev, "codec write timeout\n");
618 }
619 
atmel_ac97c_read(struct snd_ac97 * ac97,unsigned short reg)620 static unsigned short atmel_ac97c_read(struct snd_ac97 *ac97,
621 		unsigned short reg)
622 {
623 	struct atmel_ac97c *chip = get_chip(ac97);
624 	unsigned long word;
625 	int timeout = 40;
626 	int write = 10;
627 
628 	word = (0x80 | (reg & 0x7f)) << 16;
629 
630 	if ((ac97c_readl(chip, COSR) & AC97C_CSR_RXRDY) != 0)
631 		ac97c_readl(chip, CORHR);
632 
633 retry_write:
634 	timeout = 40;
635 
636 	do {
637 		if ((ac97c_readl(chip, COSR) & AC97C_CSR_TXRDY) != 0) {
638 			ac97c_writel(chip, COTHR, word);
639 			goto read_reg;
640 		}
641 		udelay(10);
642 	} while (--timeout);
643 
644 	if (!--write)
645 		goto timed_out;
646 	goto retry_write;
647 
648 read_reg:
649 	do {
650 		if ((ac97c_readl(chip, COSR) & AC97C_CSR_RXRDY) != 0) {
651 			unsigned short val = ac97c_readl(chip, CORHR);
652 			return val;
653 		}
654 		udelay(10);
655 	} while (--timeout);
656 
657 	if (!--write)
658 		goto timed_out;
659 	goto retry_write;
660 
661 timed_out:
662 	dev_dbg(&chip->pdev->dev, "codec read timeout\n");
663 	return 0xffff;
664 }
665 
atmel_ac97c_reset(struct atmel_ac97c * chip)666 static void atmel_ac97c_reset(struct atmel_ac97c *chip)
667 {
668 	ac97c_writel(chip, MR,   0);
669 	ac97c_writel(chip, MR,   AC97C_MR_ENA);
670 	ac97c_writel(chip, CAMR, 0);
671 	ac97c_writel(chip, COMR, 0);
672 
673 	if (!IS_ERR(chip->reset_pin)) {
674 		gpiod_set_value(chip->reset_pin, 0);
675 		/* AC97 v2.2 specifications says minimum 1 us. */
676 		udelay(2);
677 		gpiod_set_value(chip->reset_pin, 1);
678 	} else {
679 		ac97c_writel(chip, MR, AC97C_MR_WRST | AC97C_MR_ENA);
680 		udelay(2);
681 		ac97c_writel(chip, MR, AC97C_MR_ENA);
682 	}
683 }
684 
685 static const struct of_device_id atmel_ac97c_dt_ids[] = {
686 	{ .compatible = "atmel,at91sam9263-ac97c", },
687 	{ }
688 };
689 MODULE_DEVICE_TABLE(of, atmel_ac97c_dt_ids);
690 
atmel_ac97c_probe(struct platform_device * pdev)691 static int atmel_ac97c_probe(struct platform_device *pdev)
692 {
693 	struct device			*dev = &pdev->dev;
694 	struct snd_card			*card;
695 	struct atmel_ac97c		*chip;
696 	void __iomem			*regs;
697 	struct clk			*pclk;
698 	static const struct snd_ac97_bus_ops	ops = {
699 		.write	= atmel_ac97c_write,
700 		.read	= atmel_ac97c_read,
701 	};
702 	int				retval;
703 	int				irq;
704 
705 	regs = devm_platform_ioremap_resource(pdev, 0);
706 	if (IS_ERR(regs))
707 		return PTR_ERR(regs);
708 
709 	irq = platform_get_irq(pdev, 0);
710 	if (irq < 0)
711 		return irq;
712 
713 	pclk = devm_clk_get_enabled(&pdev->dev, "ac97_clk");
714 	if (IS_ERR(pclk)) {
715 		dev_dbg(&pdev->dev, "no peripheral clock\n");
716 		return PTR_ERR(pclk);
717 	}
718 
719 	retval = snd_devm_card_new(&pdev->dev, SNDRV_DEFAULT_IDX1,
720 			      SNDRV_DEFAULT_STR1, THIS_MODULE,
721 			      sizeof(struct atmel_ac97c), &card);
722 	if (retval) {
723 		dev_dbg(&pdev->dev, "could not create sound card device\n");
724 		return retval;
725 	}
726 
727 	chip = get_chip(card);
728 
729 	retval = devm_request_irq(&pdev->dev, irq, atmel_ac97c_interrupt, 0, "AC97C", chip);
730 	if (retval)
731 		return retval;
732 
733 	chip->irq = irq;
734 
735 	spin_lock_init(&chip->lock);
736 
737 	strscpy(card->driver, "Atmel AC97C");
738 	strscpy(card->shortname, "Atmel AC97C");
739 	strscpy(card->longname, "Atmel AC97 controller");
740 
741 	chip->card = card;
742 	chip->pclk = pclk;
743 	chip->pdev = pdev;
744 	chip->regs = regs;
745 
746 	chip->reset_pin = devm_gpiod_get_index(dev, "ac97", 2, GPIOD_OUT_HIGH);
747 	if (IS_ERR(chip->reset_pin))
748 		dev_dbg(dev, "reset pin not available\n");
749 
750 	atmel_ac97c_reset(chip);
751 
752 	/* Enable overrun interrupt from codec channel */
753 	ac97c_writel(chip, COMR, AC97C_CSR_OVRUN);
754 	ac97c_writel(chip, IER, ac97c_readl(chip, IMR) | AC97C_SR_COEVT);
755 
756 	retval = snd_ac97_bus(card, 0, &ops, chip, &chip->ac97_bus);
757 	if (retval) {
758 		dev_dbg(&pdev->dev, "could not register on ac97 bus\n");
759 		return retval;
760 	}
761 
762 	retval = atmel_ac97c_mixer_new(chip);
763 	if (retval) {
764 		dev_dbg(&pdev->dev, "could not register ac97 mixer\n");
765 		return retval;
766 	}
767 
768 	retval = atmel_ac97c_pcm_new(chip);
769 	if (retval) {
770 		dev_dbg(&pdev->dev, "could not register ac97 pcm device\n");
771 		return retval;
772 	}
773 
774 	retval = snd_card_register(card);
775 	if (retval) {
776 		dev_dbg(&pdev->dev, "could not register sound card\n");
777 		return retval;
778 	}
779 
780 	platform_set_drvdata(pdev, card);
781 
782 	dev_info(&pdev->dev, "Atmel AC97 controller at 0x%p, irq = %d\n",
783 			chip->regs, irq);
784 
785 	return 0;
786 }
787 
atmel_ac97c_suspend(struct device * pdev)788 static int atmel_ac97c_suspend(struct device *pdev)
789 {
790 	struct snd_card *card = dev_get_drvdata(pdev);
791 	struct atmel_ac97c *chip = card->private_data;
792 
793 	clk_disable_unprepare(chip->pclk);
794 	return 0;
795 }
796 
atmel_ac97c_resume(struct device * pdev)797 static int atmel_ac97c_resume(struct device *pdev)
798 {
799 	struct snd_card *card = dev_get_drvdata(pdev);
800 	struct atmel_ac97c *chip = card->private_data;
801 	int ret = clk_prepare_enable(chip->pclk);
802 
803 	return ret;
804 }
805 
806 static DEFINE_SIMPLE_DEV_PM_OPS(atmel_ac97c_pm, atmel_ac97c_suspend, atmel_ac97c_resume);
807 
atmel_ac97c_remove(struct platform_device * pdev)808 static void atmel_ac97c_remove(struct platform_device *pdev)
809 {
810 	struct snd_card *card = platform_get_drvdata(pdev);
811 	struct atmel_ac97c *chip = get_chip(card);
812 
813 	ac97c_writel(chip, CAMR, 0);
814 	ac97c_writel(chip, COMR, 0);
815 	ac97c_writel(chip, MR,   0);
816 }
817 
818 static struct platform_driver atmel_ac97c_driver = {
819 	.probe		= atmel_ac97c_probe,
820 	.remove		= atmel_ac97c_remove,
821 	.driver		= {
822 		.name	= "atmel_ac97c",
823 		.pm	= pm_ptr(&atmel_ac97c_pm),
824 		.of_match_table = atmel_ac97c_dt_ids,
825 	},
826 };
827 module_platform_driver(atmel_ac97c_driver);
828 
829 MODULE_LICENSE("GPL");
830 MODULE_DESCRIPTION("Driver for Atmel AC97 controller");
831 MODULE_AUTHOR("Hans-Christian Egtvedt <egtvedt@samfundet.no>");
832