xref: /linux/sound/spi/at73c213.c (revision b77e0ce62d63a761ffb7f7245a215a49f5921c2f)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Driver for AT73C213 16-bit stereo DAC connected to Atmel SSC
4  *
5  * Copyright (C) 2006-2007 Atmel Norway
6  */
7 
8 /*#define DEBUG*/
9 
10 #include <linux/clk.h>
11 #include <linux/err.h>
12 #include <linux/delay.h>
13 #include <linux/device.h>
14 #include <linux/dma-mapping.h>
15 #include <linux/init.h>
16 #include <linux/interrupt.h>
17 #include <linux/module.h>
18 #include <linux/mutex.h>
19 #include <linux/platform_device.h>
20 #include <linux/io.h>
21 
22 #include <sound/initval.h>
23 #include <sound/control.h>
24 #include <sound/core.h>
25 #include <sound/pcm.h>
26 
27 #include <linux/atmel-ssc.h>
28 
29 #include <linux/spi/spi.h>
30 #include <linux/spi/at73c213.h>
31 
32 #include "at73c213.h"
33 
34 #define BITRATE_MIN	 8000 /* Hardware limit? */
35 #define BITRATE_TARGET	CONFIG_SND_AT73C213_TARGET_BITRATE
36 #define BITRATE_MAX	50000 /* Hardware limit. */
37 
38 /* Initial (hardware reset) AT73C213 register values. */
39 static const u8 snd_at73c213_original_image[18] =
40 {
41 	0x00,	/* 00 - CTRL    */
42 	0x05,	/* 01 - LLIG    */
43 	0x05,	/* 02 - RLIG    */
44 	0x08,	/* 03 - LPMG    */
45 	0x08,	/* 04 - RPMG    */
46 	0x00,	/* 05 - LLOG    */
47 	0x00,	/* 06 - RLOG    */
48 	0x22,	/* 07 - OLC     */
49 	0x09,	/* 08 - MC      */
50 	0x00,	/* 09 - CSFC    */
51 	0x00,	/* 0A - MISC    */
52 	0x00,	/* 0B -         */
53 	0x00,	/* 0C - PRECH   */
54 	0x05,	/* 0D - AUXG    */
55 	0x00,	/* 0E -         */
56 	0x00,	/* 0F -         */
57 	0x00,	/* 10 - RST     */
58 	0x00,	/* 11 - PA_CTRL */
59 };
60 
61 struct snd_at73c213 {
62 	struct snd_card			*card;
63 	struct snd_pcm			*pcm;
64 	struct snd_pcm_substream	*substream;
65 	struct at73c213_board_info	*board;
66 	int				irq;
67 	int				period;
68 	unsigned long			bitrate;
69 	struct ssc_device		*ssc;
70 	struct spi_device		*spi;
71 	u8				spi_wbuffer[2];
72 	u8				spi_rbuffer[2];
73 	/* Image of the SPI registers in AT73C213. */
74 	u8				reg_image[18];
75 	/* Protect SSC registers against concurrent access. */
76 	spinlock_t			lock;
77 	/* Protect mixer registers against concurrent access. */
78 	struct mutex			mixer_lock;
79 };
80 
81 #define get_chip(card) ((struct snd_at73c213 *)card->private_data)
82 
83 static int
84 snd_at73c213_write_reg(struct snd_at73c213 *chip, u8 reg, u8 val)
85 {
86 	struct spi_message msg;
87 	struct spi_transfer msg_xfer = {
88 		.len		= 2,
89 		.cs_change	= 0,
90 	};
91 	int retval;
92 
93 	spi_message_init(&msg);
94 
95 	chip->spi_wbuffer[0] = reg;
96 	chip->spi_wbuffer[1] = val;
97 
98 	msg_xfer.tx_buf = chip->spi_wbuffer;
99 	msg_xfer.rx_buf = chip->spi_rbuffer;
100 	spi_message_add_tail(&msg_xfer, &msg);
101 
102 	retval = spi_sync(chip->spi, &msg);
103 
104 	if (!retval)
105 		chip->reg_image[reg] = val;
106 
107 	return retval;
108 }
109 
110 static struct snd_pcm_hardware snd_at73c213_playback_hw = {
111 	.info		= SNDRV_PCM_INFO_INTERLEAVED |
112 			  SNDRV_PCM_INFO_BLOCK_TRANSFER,
113 	.formats	= SNDRV_PCM_FMTBIT_S16_BE,
114 	.rates		= SNDRV_PCM_RATE_CONTINUOUS,
115 	.rate_min	= 8000,  /* Replaced by chip->bitrate later. */
116 	.rate_max	= 50000, /* Replaced by chip->bitrate later. */
117 	.channels_min	= 1,
118 	.channels_max	= 2,
119 	.buffer_bytes_max = 64 * 1024 - 1,
120 	.period_bytes_min = 512,
121 	.period_bytes_max = 64 * 1024 - 1,
122 	.periods_min	= 4,
123 	.periods_max	= 1024,
124 };
125 
126 /*
127  * Calculate and set bitrate and divisions.
128  */
129 static int snd_at73c213_set_bitrate(struct snd_at73c213 *chip)
130 {
131 	unsigned long ssc_rate = clk_get_rate(chip->ssc->clk);
132 	unsigned long dac_rate_new, ssc_div;
133 	int status;
134 	unsigned long ssc_div_max, ssc_div_min;
135 	int max_tries;
136 
137 	/*
138 	 * We connect two clocks here, picking divisors so the I2S clocks
139 	 * out data at the same rate the DAC clocks it in ... and as close
140 	 * as practical to the desired target rate.
141 	 *
142 	 * The DAC master clock (MCLK) is programmable, and is either 256
143 	 * or (not here) 384 times the I2S output clock (BCLK).
144 	 */
145 
146 	/* SSC clock / (bitrate * stereo * 16-bit). */
147 	ssc_div = ssc_rate / (BITRATE_TARGET * 2 * 16);
148 	ssc_div_min = ssc_rate / (BITRATE_MAX * 2 * 16);
149 	ssc_div_max = ssc_rate / (BITRATE_MIN * 2 * 16);
150 	max_tries = (ssc_div_max - ssc_div_min) / 2;
151 
152 	if (max_tries < 1)
153 		max_tries = 1;
154 
155 	/* ssc_div must be even. */
156 	ssc_div = (ssc_div + 1) & ~1UL;
157 
158 	if ((ssc_rate / (ssc_div * 2 * 16)) < BITRATE_MIN) {
159 		ssc_div -= 2;
160 		if ((ssc_rate / (ssc_div * 2 * 16)) > BITRATE_MAX)
161 			return -ENXIO;
162 	}
163 
164 	/* Search for a possible bitrate. */
165 	do {
166 		/* SSC clock / (ssc divider * 16-bit * stereo). */
167 		if ((ssc_rate / (ssc_div * 2 * 16)) < BITRATE_MIN)
168 			return -ENXIO;
169 
170 		/* 256 / (2 * 16) = 8 */
171 		dac_rate_new = 8 * (ssc_rate / ssc_div);
172 
173 		status = clk_round_rate(chip->board->dac_clk, dac_rate_new);
174 		if (status <= 0)
175 			return status;
176 
177 		/* Ignore difference smaller than 256 Hz. */
178 		if ((status/256) == (dac_rate_new/256))
179 			goto set_rate;
180 
181 		ssc_div += 2;
182 	} while (--max_tries);
183 
184 	/* Not able to find a valid bitrate. */
185 	return -ENXIO;
186 
187 set_rate:
188 	status = clk_set_rate(chip->board->dac_clk, status);
189 	if (status < 0)
190 		return status;
191 
192 	/* Set divider in SSC device. */
193 	ssc_writel(chip->ssc->regs, CMR, ssc_div/2);
194 
195 	/* SSC clock / (ssc divider * 16-bit * stereo). */
196 	chip->bitrate = ssc_rate / (ssc_div * 16 * 2);
197 
198 	dev_info(&chip->spi->dev,
199 			"at73c213: supported bitrate is %lu (%lu divider)\n",
200 			chip->bitrate, ssc_div);
201 
202 	return 0;
203 }
204 
205 static int snd_at73c213_pcm_open(struct snd_pcm_substream *substream)
206 {
207 	struct snd_at73c213 *chip = snd_pcm_substream_chip(substream);
208 	struct snd_pcm_runtime *runtime = substream->runtime;
209 	int err;
210 
211 	/* ensure buffer_size is a multiple of period_size */
212 	err = snd_pcm_hw_constraint_integer(runtime,
213 					SNDRV_PCM_HW_PARAM_PERIODS);
214 	if (err < 0)
215 		return err;
216 	snd_at73c213_playback_hw.rate_min = chip->bitrate;
217 	snd_at73c213_playback_hw.rate_max = chip->bitrate;
218 	runtime->hw = snd_at73c213_playback_hw;
219 	chip->substream = substream;
220 
221 	clk_enable(chip->ssc->clk);
222 
223 	return 0;
224 }
225 
226 static int snd_at73c213_pcm_close(struct snd_pcm_substream *substream)
227 {
228 	struct snd_at73c213 *chip = snd_pcm_substream_chip(substream);
229 	chip->substream = NULL;
230 	clk_disable(chip->ssc->clk);
231 	return 0;
232 }
233 
234 static int snd_at73c213_pcm_hw_params(struct snd_pcm_substream *substream,
235 				 struct snd_pcm_hw_params *hw_params)
236 {
237 	struct snd_at73c213 *chip = snd_pcm_substream_chip(substream);
238 	int channels = params_channels(hw_params);
239 	int val;
240 
241 	val = ssc_readl(chip->ssc->regs, TFMR);
242 	val = SSC_BFINS(TFMR_DATNB, channels - 1, val);
243 	ssc_writel(chip->ssc->regs, TFMR, val);
244 
245 	return 0;
246 }
247 
248 static int snd_at73c213_pcm_prepare(struct snd_pcm_substream *substream)
249 {
250 	struct snd_at73c213 *chip = snd_pcm_substream_chip(substream);
251 	struct snd_pcm_runtime *runtime = substream->runtime;
252 	int block_size;
253 
254 	block_size = frames_to_bytes(runtime, runtime->period_size);
255 
256 	chip->period = 0;
257 
258 	ssc_writel(chip->ssc->regs, PDC_TPR,
259 			(long)runtime->dma_addr);
260 	ssc_writel(chip->ssc->regs, PDC_TCR,
261 			runtime->period_size * runtime->channels);
262 	ssc_writel(chip->ssc->regs, PDC_TNPR,
263 			(long)runtime->dma_addr + block_size);
264 	ssc_writel(chip->ssc->regs, PDC_TNCR,
265 			runtime->period_size * runtime->channels);
266 
267 	return 0;
268 }
269 
270 static int snd_at73c213_pcm_trigger(struct snd_pcm_substream *substream,
271 				   int cmd)
272 {
273 	struct snd_at73c213 *chip = snd_pcm_substream_chip(substream);
274 	int retval = 0;
275 
276 	spin_lock(&chip->lock);
277 
278 	switch (cmd) {
279 	case SNDRV_PCM_TRIGGER_START:
280 		ssc_writel(chip->ssc->regs, IER, SSC_BIT(IER_ENDTX));
281 		ssc_writel(chip->ssc->regs, PDC_PTCR, SSC_BIT(PDC_PTCR_TXTEN));
282 		break;
283 	case SNDRV_PCM_TRIGGER_STOP:
284 		ssc_writel(chip->ssc->regs, PDC_PTCR, SSC_BIT(PDC_PTCR_TXTDIS));
285 		ssc_writel(chip->ssc->regs, IDR, SSC_BIT(IDR_ENDTX));
286 		break;
287 	default:
288 		dev_dbg(&chip->spi->dev, "spurious command %x\n", cmd);
289 		retval = -EINVAL;
290 		break;
291 	}
292 
293 	spin_unlock(&chip->lock);
294 
295 	return retval;
296 }
297 
298 static snd_pcm_uframes_t
299 snd_at73c213_pcm_pointer(struct snd_pcm_substream *substream)
300 {
301 	struct snd_at73c213 *chip = snd_pcm_substream_chip(substream);
302 	struct snd_pcm_runtime *runtime = substream->runtime;
303 	snd_pcm_uframes_t pos;
304 	unsigned long bytes;
305 
306 	bytes = ssc_readl(chip->ssc->regs, PDC_TPR)
307 		- (unsigned long)runtime->dma_addr;
308 
309 	pos = bytes_to_frames(runtime, bytes);
310 	if (pos >= runtime->buffer_size)
311 		pos -= runtime->buffer_size;
312 
313 	return pos;
314 }
315 
316 static const struct snd_pcm_ops at73c213_playback_ops = {
317 	.open		= snd_at73c213_pcm_open,
318 	.close		= snd_at73c213_pcm_close,
319 	.hw_params	= snd_at73c213_pcm_hw_params,
320 	.prepare	= snd_at73c213_pcm_prepare,
321 	.trigger	= snd_at73c213_pcm_trigger,
322 	.pointer	= snd_at73c213_pcm_pointer,
323 };
324 
325 static int snd_at73c213_pcm_new(struct snd_at73c213 *chip, int device)
326 {
327 	struct snd_pcm *pcm;
328 	int retval;
329 
330 	retval = snd_pcm_new(chip->card, chip->card->shortname,
331 			device, 1, 0, &pcm);
332 	if (retval < 0)
333 		goto out;
334 
335 	pcm->private_data = chip;
336 	pcm->info_flags = SNDRV_PCM_INFO_BLOCK_TRANSFER;
337 	strcpy(pcm->name, "at73c213");
338 	chip->pcm = pcm;
339 
340 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &at73c213_playback_ops);
341 
342 	snd_pcm_set_managed_buffer_all(chip->pcm,
343 			SNDRV_DMA_TYPE_DEV, &chip->ssc->pdev->dev,
344 			64 * 1024, 64 * 1024);
345 out:
346 	return retval;
347 }
348 
349 static irqreturn_t snd_at73c213_interrupt(int irq, void *dev_id)
350 {
351 	struct snd_at73c213 *chip = dev_id;
352 	struct snd_pcm_runtime *runtime = chip->substream->runtime;
353 	u32 status;
354 	int offset;
355 	int block_size;
356 	int next_period;
357 	int retval = IRQ_NONE;
358 
359 	spin_lock(&chip->lock);
360 
361 	block_size = frames_to_bytes(runtime, runtime->period_size);
362 	status = ssc_readl(chip->ssc->regs, IMR);
363 
364 	if (status & SSC_BIT(IMR_ENDTX)) {
365 		chip->period++;
366 		if (chip->period == runtime->periods)
367 			chip->period = 0;
368 		next_period = chip->period + 1;
369 		if (next_period == runtime->periods)
370 			next_period = 0;
371 
372 		offset = block_size * next_period;
373 
374 		ssc_writel(chip->ssc->regs, PDC_TNPR,
375 				(long)runtime->dma_addr + offset);
376 		ssc_writel(chip->ssc->regs, PDC_TNCR,
377 				runtime->period_size * runtime->channels);
378 		retval = IRQ_HANDLED;
379 	}
380 
381 	ssc_readl(chip->ssc->regs, IMR);
382 	spin_unlock(&chip->lock);
383 
384 	if (status & SSC_BIT(IMR_ENDTX))
385 		snd_pcm_period_elapsed(chip->substream);
386 
387 	return retval;
388 }
389 
390 /*
391  * Mixer functions.
392  */
393 static int snd_at73c213_mono_get(struct snd_kcontrol *kcontrol,
394 				 struct snd_ctl_elem_value *ucontrol)
395 {
396 	struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol);
397 	int reg = kcontrol->private_value & 0xff;
398 	int shift = (kcontrol->private_value >> 8) & 0xff;
399 	int mask = (kcontrol->private_value >> 16) & 0xff;
400 	int invert = (kcontrol->private_value >> 24) & 0xff;
401 
402 	mutex_lock(&chip->mixer_lock);
403 
404 	ucontrol->value.integer.value[0] =
405 		(chip->reg_image[reg] >> shift) & mask;
406 
407 	if (invert)
408 		ucontrol->value.integer.value[0] =
409 			mask - ucontrol->value.integer.value[0];
410 
411 	mutex_unlock(&chip->mixer_lock);
412 
413 	return 0;
414 }
415 
416 static int snd_at73c213_mono_put(struct snd_kcontrol *kcontrol,
417 				 struct snd_ctl_elem_value *ucontrol)
418 {
419 	struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol);
420 	int reg = kcontrol->private_value & 0xff;
421 	int shift = (kcontrol->private_value >> 8) & 0xff;
422 	int mask = (kcontrol->private_value >> 16) & 0xff;
423 	int invert = (kcontrol->private_value >> 24) & 0xff;
424 	int change, retval;
425 	unsigned short val;
426 
427 	val = (ucontrol->value.integer.value[0] & mask);
428 	if (invert)
429 		val = mask - val;
430 	val <<= shift;
431 
432 	mutex_lock(&chip->mixer_lock);
433 
434 	val = (chip->reg_image[reg] & ~(mask << shift)) | val;
435 	change = val != chip->reg_image[reg];
436 	retval = snd_at73c213_write_reg(chip, reg, val);
437 
438 	mutex_unlock(&chip->mixer_lock);
439 
440 	if (retval)
441 		return retval;
442 
443 	return change;
444 }
445 
446 static int snd_at73c213_stereo_info(struct snd_kcontrol *kcontrol,
447 				  struct snd_ctl_elem_info *uinfo)
448 {
449 	int mask = (kcontrol->private_value >> 24) & 0xff;
450 
451 	if (mask == 1)
452 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
453 	else
454 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
455 
456 	uinfo->count = 2;
457 	uinfo->value.integer.min = 0;
458 	uinfo->value.integer.max = mask;
459 
460 	return 0;
461 }
462 
463 static int snd_at73c213_stereo_get(struct snd_kcontrol *kcontrol,
464 				 struct snd_ctl_elem_value *ucontrol)
465 {
466 	struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol);
467 	int left_reg = kcontrol->private_value & 0xff;
468 	int right_reg = (kcontrol->private_value >> 8) & 0xff;
469 	int shift_left = (kcontrol->private_value >> 16) & 0x07;
470 	int shift_right = (kcontrol->private_value >> 19) & 0x07;
471 	int mask = (kcontrol->private_value >> 24) & 0xff;
472 	int invert = (kcontrol->private_value >> 22) & 1;
473 
474 	mutex_lock(&chip->mixer_lock);
475 
476 	ucontrol->value.integer.value[0] =
477 		(chip->reg_image[left_reg] >> shift_left) & mask;
478 	ucontrol->value.integer.value[1] =
479 		(chip->reg_image[right_reg] >> shift_right) & mask;
480 
481 	if (invert) {
482 		ucontrol->value.integer.value[0] =
483 			mask - ucontrol->value.integer.value[0];
484 		ucontrol->value.integer.value[1] =
485 			mask - ucontrol->value.integer.value[1];
486 	}
487 
488 	mutex_unlock(&chip->mixer_lock);
489 
490 	return 0;
491 }
492 
493 static int snd_at73c213_stereo_put(struct snd_kcontrol *kcontrol,
494 				 struct snd_ctl_elem_value *ucontrol)
495 {
496 	struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol);
497 	int left_reg = kcontrol->private_value & 0xff;
498 	int right_reg = (kcontrol->private_value >> 8) & 0xff;
499 	int shift_left = (kcontrol->private_value >> 16) & 0x07;
500 	int shift_right = (kcontrol->private_value >> 19) & 0x07;
501 	int mask = (kcontrol->private_value >> 24) & 0xff;
502 	int invert = (kcontrol->private_value >> 22) & 1;
503 	int change, retval;
504 	unsigned short val1, val2;
505 
506 	val1 = ucontrol->value.integer.value[0] & mask;
507 	val2 = ucontrol->value.integer.value[1] & mask;
508 	if (invert) {
509 		val1 = mask - val1;
510 		val2 = mask - val2;
511 	}
512 	val1 <<= shift_left;
513 	val2 <<= shift_right;
514 
515 	mutex_lock(&chip->mixer_lock);
516 
517 	val1 = (chip->reg_image[left_reg] & ~(mask << shift_left)) | val1;
518 	val2 = (chip->reg_image[right_reg] & ~(mask << shift_right)) | val2;
519 	change = val1 != chip->reg_image[left_reg]
520 		|| val2 != chip->reg_image[right_reg];
521 	retval = snd_at73c213_write_reg(chip, left_reg, val1);
522 	if (retval) {
523 		mutex_unlock(&chip->mixer_lock);
524 		goto out;
525 	}
526 	retval = snd_at73c213_write_reg(chip, right_reg, val2);
527 	if (retval) {
528 		mutex_unlock(&chip->mixer_lock);
529 		goto out;
530 	}
531 
532 	mutex_unlock(&chip->mixer_lock);
533 
534 	return change;
535 
536 out:
537 	return retval;
538 }
539 
540 #define snd_at73c213_mono_switch_info	snd_ctl_boolean_mono_info
541 
542 static int snd_at73c213_mono_switch_get(struct snd_kcontrol *kcontrol,
543 				 struct snd_ctl_elem_value *ucontrol)
544 {
545 	struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol);
546 	int reg = kcontrol->private_value & 0xff;
547 	int shift = (kcontrol->private_value >> 8) & 0xff;
548 	int invert = (kcontrol->private_value >> 24) & 0xff;
549 
550 	mutex_lock(&chip->mixer_lock);
551 
552 	ucontrol->value.integer.value[0] =
553 		(chip->reg_image[reg] >> shift) & 0x01;
554 
555 	if (invert)
556 		ucontrol->value.integer.value[0] =
557 			0x01 - ucontrol->value.integer.value[0];
558 
559 	mutex_unlock(&chip->mixer_lock);
560 
561 	return 0;
562 }
563 
564 static int snd_at73c213_mono_switch_put(struct snd_kcontrol *kcontrol,
565 				 struct snd_ctl_elem_value *ucontrol)
566 {
567 	struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol);
568 	int reg = kcontrol->private_value & 0xff;
569 	int shift = (kcontrol->private_value >> 8) & 0xff;
570 	int mask = (kcontrol->private_value >> 16) & 0xff;
571 	int invert = (kcontrol->private_value >> 24) & 0xff;
572 	int change, retval;
573 	unsigned short val;
574 
575 	if (ucontrol->value.integer.value[0])
576 		val = mask;
577 	else
578 		val = 0;
579 
580 	if (invert)
581 		val = mask - val;
582 	val <<= shift;
583 
584 	mutex_lock(&chip->mixer_lock);
585 
586 	val |= (chip->reg_image[reg] & ~(mask << shift));
587 	change = val != chip->reg_image[reg];
588 
589 	retval = snd_at73c213_write_reg(chip, reg, val);
590 
591 	mutex_unlock(&chip->mixer_lock);
592 
593 	if (retval)
594 		return retval;
595 
596 	return change;
597 }
598 
599 static int snd_at73c213_pa_volume_info(struct snd_kcontrol *kcontrol,
600 				  struct snd_ctl_elem_info *uinfo)
601 {
602 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
603 	uinfo->count = 1;
604 	uinfo->value.integer.min = 0;
605 	uinfo->value.integer.max = ((kcontrol->private_value >> 16) & 0xff) - 1;
606 
607 	return 0;
608 }
609 
610 static int snd_at73c213_line_capture_volume_info(
611 		struct snd_kcontrol *kcontrol,
612 		struct snd_ctl_elem_info *uinfo)
613 {
614 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
615 	uinfo->count = 2;
616 	/* When inverted will give values 0x10001 => 0. */
617 	uinfo->value.integer.min = 14;
618 	uinfo->value.integer.max = 31;
619 
620 	return 0;
621 }
622 
623 static int snd_at73c213_aux_capture_volume_info(
624 		struct snd_kcontrol *kcontrol,
625 		struct snd_ctl_elem_info *uinfo)
626 {
627 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
628 	uinfo->count = 1;
629 	/* When inverted will give values 0x10001 => 0. */
630 	uinfo->value.integer.min = 14;
631 	uinfo->value.integer.max = 31;
632 
633 	return 0;
634 }
635 
636 #define AT73C213_MONO_SWITCH(xname, xindex, reg, shift, mask, invert)	\
637 {									\
638 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,				\
639 	.name = xname,							\
640 	.index = xindex,						\
641 	.info = snd_at73c213_mono_switch_info,				\
642 	.get = snd_at73c213_mono_switch_get,				\
643 	.put = snd_at73c213_mono_switch_put,				\
644 	.private_value = (reg | (shift << 8) | (mask << 16) | (invert << 24)) \
645 }
646 
647 #define AT73C213_STEREO(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
648 {									\
649 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,				\
650 	.name = xname,							\
651 	.index = xindex,						\
652 	.info = snd_at73c213_stereo_info,				\
653 	.get = snd_at73c213_stereo_get,					\
654 	.put = snd_at73c213_stereo_put,					\
655 	.private_value = (left_reg | (right_reg << 8)			\
656 			| (shift_left << 16) | (shift_right << 19)	\
657 			| (mask << 24) | (invert << 22))		\
658 }
659 
660 static const struct snd_kcontrol_new snd_at73c213_controls[] = {
661 AT73C213_STEREO("Master Playback Volume", 0, DAC_LMPG, DAC_RMPG, 0, 0, 0x1f, 1),
662 AT73C213_STEREO("Master Playback Switch", 0, DAC_LMPG, DAC_RMPG, 5, 5, 1, 1),
663 AT73C213_STEREO("PCM Playback Volume", 0, DAC_LLOG, DAC_RLOG, 0, 0, 0x1f, 1),
664 AT73C213_STEREO("PCM Playback Switch", 0, DAC_LLOG, DAC_RLOG, 5, 5, 1, 1),
665 AT73C213_MONO_SWITCH("Mono PA Playback Switch", 0, DAC_CTRL, DAC_CTRL_ONPADRV,
666 		     0x01, 0),
667 {
668 	.iface	= SNDRV_CTL_ELEM_IFACE_MIXER,
669 	.name	= "PA Playback Volume",
670 	.index	= 0,
671 	.info	= snd_at73c213_pa_volume_info,
672 	.get	= snd_at73c213_mono_get,
673 	.put	= snd_at73c213_mono_put,
674 	.private_value	= PA_CTRL | (PA_CTRL_APAGAIN << 8) | \
675 		(0x0f << 16) | (1 << 24),
676 },
677 AT73C213_MONO_SWITCH("PA High Gain Playback Switch", 0, PA_CTRL, PA_CTRL_APALP,
678 		     0x01, 1),
679 AT73C213_MONO_SWITCH("PA Playback Switch", 0, PA_CTRL, PA_CTRL_APAON, 0x01, 0),
680 {
681 	.iface	= SNDRV_CTL_ELEM_IFACE_MIXER,
682 	.name	= "Aux Capture Volume",
683 	.index	= 0,
684 	.info	= snd_at73c213_aux_capture_volume_info,
685 	.get	= snd_at73c213_mono_get,
686 	.put	= snd_at73c213_mono_put,
687 	.private_value	= DAC_AUXG | (0 << 8) | (0x1f << 16) | (1 << 24),
688 },
689 AT73C213_MONO_SWITCH("Aux Capture Switch", 0, DAC_CTRL, DAC_CTRL_ONAUXIN,
690 		     0x01, 0),
691 {
692 	.iface	= SNDRV_CTL_ELEM_IFACE_MIXER,
693 	.name	= "Line Capture Volume",
694 	.index	= 0,
695 	.info	= snd_at73c213_line_capture_volume_info,
696 	.get	= snd_at73c213_stereo_get,
697 	.put	= snd_at73c213_stereo_put,
698 	.private_value	= DAC_LLIG | (DAC_RLIG << 8) | (0 << 16) | (0 << 19)
699 		| (0x1f << 24) | (1 << 22),
700 },
701 AT73C213_MONO_SWITCH("Line Capture Switch", 0, DAC_CTRL, 0, 0x03, 0),
702 };
703 
704 static int snd_at73c213_mixer(struct snd_at73c213 *chip)
705 {
706 	struct snd_card *card;
707 	int errval, idx;
708 
709 	if (chip == NULL || chip->pcm == NULL)
710 		return -EINVAL;
711 
712 	card = chip->card;
713 
714 	strcpy(card->mixername, chip->pcm->name);
715 
716 	for (idx = 0; idx < ARRAY_SIZE(snd_at73c213_controls); idx++) {
717 		errval = snd_ctl_add(card,
718 				snd_ctl_new1(&snd_at73c213_controls[idx],
719 					chip));
720 		if (errval < 0)
721 			goto cleanup;
722 	}
723 
724 	return 0;
725 
726 cleanup:
727 	for (idx = 1; idx < ARRAY_SIZE(snd_at73c213_controls) + 1; idx++) {
728 		struct snd_kcontrol *kctl;
729 		kctl = snd_ctl_find_numid(card, idx);
730 		if (kctl)
731 			snd_ctl_remove(card, kctl);
732 	}
733 	return errval;
734 }
735 
736 /*
737  * Device functions
738  */
739 static int snd_at73c213_ssc_init(struct snd_at73c213 *chip)
740 {
741 	/*
742 	 * Continuous clock output.
743 	 * Starts on falling TF.
744 	 * Delay 1 cycle (1 bit).
745 	 * Periode is 16 bit (16 - 1).
746 	 */
747 	ssc_writel(chip->ssc->regs, TCMR,
748 			SSC_BF(TCMR_CKO, 1)
749 			| SSC_BF(TCMR_START, 4)
750 			| SSC_BF(TCMR_STTDLY, 1)
751 			| SSC_BF(TCMR_PERIOD, 16 - 1));
752 	/*
753 	 * Data length is 16 bit (16 - 1).
754 	 * Transmit MSB first.
755 	 * Transmit 2 words each transfer.
756 	 * Frame sync length is 16 bit (16 - 1).
757 	 * Frame starts on negative pulse.
758 	 */
759 	ssc_writel(chip->ssc->regs, TFMR,
760 			SSC_BF(TFMR_DATLEN, 16 - 1)
761 			| SSC_BIT(TFMR_MSBF)
762 			| SSC_BF(TFMR_DATNB, 1)
763 			| SSC_BF(TFMR_FSLEN, 16 - 1)
764 			| SSC_BF(TFMR_FSOS, 1));
765 
766 	return 0;
767 }
768 
769 static int snd_at73c213_chip_init(struct snd_at73c213 *chip)
770 {
771 	int retval;
772 	unsigned char dac_ctrl = 0;
773 
774 	retval = snd_at73c213_set_bitrate(chip);
775 	if (retval)
776 		goto out;
777 
778 	/* Enable DAC master clock. */
779 	clk_enable(chip->board->dac_clk);
780 
781 	/* Initialize at73c213 on SPI bus. */
782 	retval = snd_at73c213_write_reg(chip, DAC_RST, 0x04);
783 	if (retval)
784 		goto out_clk;
785 	msleep(1);
786 	retval = snd_at73c213_write_reg(chip, DAC_RST, 0x03);
787 	if (retval)
788 		goto out_clk;
789 
790 	/* Precharge everything. */
791 	retval = snd_at73c213_write_reg(chip, DAC_PRECH, 0xff);
792 	if (retval)
793 		goto out_clk;
794 	retval = snd_at73c213_write_reg(chip, PA_CTRL, (1<<PA_CTRL_APAPRECH));
795 	if (retval)
796 		goto out_clk;
797 	retval = snd_at73c213_write_reg(chip, DAC_CTRL,
798 			(1<<DAC_CTRL_ONLNOL) | (1<<DAC_CTRL_ONLNOR));
799 	if (retval)
800 		goto out_clk;
801 
802 	msleep(50);
803 
804 	/* Stop precharging PA. */
805 	retval = snd_at73c213_write_reg(chip, PA_CTRL,
806 			(1<<PA_CTRL_APALP) | 0x0f);
807 	if (retval)
808 		goto out_clk;
809 
810 	msleep(450);
811 
812 	/* Stop precharging DAC, turn on master power. */
813 	retval = snd_at73c213_write_reg(chip, DAC_PRECH, (1<<DAC_PRECH_ONMSTR));
814 	if (retval)
815 		goto out_clk;
816 
817 	msleep(1);
818 
819 	/* Turn on DAC. */
820 	dac_ctrl = (1<<DAC_CTRL_ONDACL) | (1<<DAC_CTRL_ONDACR)
821 		| (1<<DAC_CTRL_ONLNOL) | (1<<DAC_CTRL_ONLNOR);
822 
823 	retval = snd_at73c213_write_reg(chip, DAC_CTRL, dac_ctrl);
824 	if (retval)
825 		goto out_clk;
826 
827 	/* Mute sound. */
828 	retval = snd_at73c213_write_reg(chip, DAC_LMPG, 0x3f);
829 	if (retval)
830 		goto out_clk;
831 	retval = snd_at73c213_write_reg(chip, DAC_RMPG, 0x3f);
832 	if (retval)
833 		goto out_clk;
834 	retval = snd_at73c213_write_reg(chip, DAC_LLOG, 0x3f);
835 	if (retval)
836 		goto out_clk;
837 	retval = snd_at73c213_write_reg(chip, DAC_RLOG, 0x3f);
838 	if (retval)
839 		goto out_clk;
840 	retval = snd_at73c213_write_reg(chip, DAC_LLIG, 0x11);
841 	if (retval)
842 		goto out_clk;
843 	retval = snd_at73c213_write_reg(chip, DAC_RLIG, 0x11);
844 	if (retval)
845 		goto out_clk;
846 	retval = snd_at73c213_write_reg(chip, DAC_AUXG, 0x11);
847 	if (retval)
848 		goto out_clk;
849 
850 	/* Enable I2S device, i.e. clock output. */
851 	ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXEN));
852 
853 	goto out;
854 
855 out_clk:
856 	clk_disable(chip->board->dac_clk);
857 out:
858 	return retval;
859 }
860 
861 static int snd_at73c213_dev_free(struct snd_device *device)
862 {
863 	struct snd_at73c213 *chip = device->device_data;
864 
865 	ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXDIS));
866 	if (chip->irq >= 0) {
867 		free_irq(chip->irq, chip);
868 		chip->irq = -1;
869 	}
870 
871 	return 0;
872 }
873 
874 static int snd_at73c213_dev_init(struct snd_card *card,
875 				 struct spi_device *spi)
876 {
877 	static const struct snd_device_ops ops = {
878 		.dev_free	= snd_at73c213_dev_free,
879 	};
880 	struct snd_at73c213 *chip = get_chip(card);
881 	int irq, retval;
882 
883 	irq = chip->ssc->irq;
884 	if (irq < 0)
885 		return irq;
886 
887 	spin_lock_init(&chip->lock);
888 	mutex_init(&chip->mixer_lock);
889 	chip->card = card;
890 	chip->irq = -1;
891 
892 	clk_enable(chip->ssc->clk);
893 
894 	retval = request_irq(irq, snd_at73c213_interrupt, 0, "at73c213", chip);
895 	if (retval) {
896 		dev_dbg(&chip->spi->dev, "unable to request irq %d\n", irq);
897 		goto out;
898 	}
899 	chip->irq = irq;
900 
901 	memcpy(&chip->reg_image, &snd_at73c213_original_image,
902 			sizeof(snd_at73c213_original_image));
903 
904 	retval = snd_at73c213_ssc_init(chip);
905 	if (retval)
906 		goto out_irq;
907 
908 	retval = snd_at73c213_chip_init(chip);
909 	if (retval)
910 		goto out_irq;
911 
912 	retval = snd_at73c213_pcm_new(chip, 0);
913 	if (retval)
914 		goto out_irq;
915 
916 	retval = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
917 	if (retval)
918 		goto out_irq;
919 
920 	retval = snd_at73c213_mixer(chip);
921 	if (retval)
922 		goto out_snd_dev;
923 
924 	goto out;
925 
926 out_snd_dev:
927 	snd_device_free(card, chip);
928 out_irq:
929 	free_irq(chip->irq, chip);
930 	chip->irq = -1;
931 out:
932 	clk_disable(chip->ssc->clk);
933 
934 	return retval;
935 }
936 
937 static int snd_at73c213_probe(struct spi_device *spi)
938 {
939 	struct snd_card			*card;
940 	struct snd_at73c213		*chip;
941 	struct at73c213_board_info	*board;
942 	int				retval;
943 	char				id[16];
944 
945 	board = spi->dev.platform_data;
946 	if (!board) {
947 		dev_dbg(&spi->dev, "no platform_data\n");
948 		return -ENXIO;
949 	}
950 
951 	if (!board->dac_clk) {
952 		dev_dbg(&spi->dev, "no DAC clk\n");
953 		return -ENXIO;
954 	}
955 
956 	if (IS_ERR(board->dac_clk)) {
957 		dev_dbg(&spi->dev, "no DAC clk\n");
958 		return PTR_ERR(board->dac_clk);
959 	}
960 
961 	/* Allocate "card" using some unused identifiers. */
962 	snprintf(id, sizeof id, "at73c213_%d", board->ssc_id);
963 	retval = snd_card_new(&spi->dev, -1, id, THIS_MODULE,
964 			      sizeof(struct snd_at73c213), &card);
965 	if (retval < 0)
966 		goto out;
967 
968 	chip = card->private_data;
969 	chip->spi = spi;
970 	chip->board = board;
971 
972 	chip->ssc = ssc_request(board->ssc_id);
973 	if (IS_ERR(chip->ssc)) {
974 		dev_dbg(&spi->dev, "could not get ssc%d device\n",
975 				board->ssc_id);
976 		retval = PTR_ERR(chip->ssc);
977 		goto out_card;
978 	}
979 
980 	retval = snd_at73c213_dev_init(card, spi);
981 	if (retval)
982 		goto out_ssc;
983 
984 	strcpy(card->driver, "at73c213");
985 	strcpy(card->shortname, board->shortname);
986 	sprintf(card->longname, "%s on irq %d", card->shortname, chip->irq);
987 
988 	retval = snd_card_register(card);
989 	if (retval)
990 		goto out_ssc;
991 
992 	dev_set_drvdata(&spi->dev, card);
993 
994 	goto out;
995 
996 out_ssc:
997 	ssc_free(chip->ssc);
998 out_card:
999 	snd_card_free(card);
1000 out:
1001 	return retval;
1002 }
1003 
1004 static int snd_at73c213_remove(struct spi_device *spi)
1005 {
1006 	struct snd_card *card = dev_get_drvdata(&spi->dev);
1007 	struct snd_at73c213 *chip = card->private_data;
1008 	int retval;
1009 
1010 	/* Stop playback. */
1011 	clk_enable(chip->ssc->clk);
1012 	ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXDIS));
1013 	clk_disable(chip->ssc->clk);
1014 
1015 	/* Mute sound. */
1016 	retval = snd_at73c213_write_reg(chip, DAC_LMPG, 0x3f);
1017 	if (retval)
1018 		goto out;
1019 	retval = snd_at73c213_write_reg(chip, DAC_RMPG, 0x3f);
1020 	if (retval)
1021 		goto out;
1022 	retval = snd_at73c213_write_reg(chip, DAC_LLOG, 0x3f);
1023 	if (retval)
1024 		goto out;
1025 	retval = snd_at73c213_write_reg(chip, DAC_RLOG, 0x3f);
1026 	if (retval)
1027 		goto out;
1028 	retval = snd_at73c213_write_reg(chip, DAC_LLIG, 0x11);
1029 	if (retval)
1030 		goto out;
1031 	retval = snd_at73c213_write_reg(chip, DAC_RLIG, 0x11);
1032 	if (retval)
1033 		goto out;
1034 	retval = snd_at73c213_write_reg(chip, DAC_AUXG, 0x11);
1035 	if (retval)
1036 		goto out;
1037 
1038 	/* Turn off PA. */
1039 	retval = snd_at73c213_write_reg(chip, PA_CTRL,
1040 					chip->reg_image[PA_CTRL] | 0x0f);
1041 	if (retval)
1042 		goto out;
1043 	msleep(10);
1044 	retval = snd_at73c213_write_reg(chip, PA_CTRL,
1045 					(1 << PA_CTRL_APALP) | 0x0f);
1046 	if (retval)
1047 		goto out;
1048 
1049 	/* Turn off external DAC. */
1050 	retval = snd_at73c213_write_reg(chip, DAC_CTRL, 0x0c);
1051 	if (retval)
1052 		goto out;
1053 	msleep(2);
1054 	retval = snd_at73c213_write_reg(chip, DAC_CTRL, 0x00);
1055 	if (retval)
1056 		goto out;
1057 
1058 	/* Turn off master power. */
1059 	retval = snd_at73c213_write_reg(chip, DAC_PRECH, 0x00);
1060 	if (retval)
1061 		goto out;
1062 
1063 out:
1064 	/* Stop DAC master clock. */
1065 	clk_disable(chip->board->dac_clk);
1066 
1067 	ssc_free(chip->ssc);
1068 	snd_card_free(card);
1069 
1070 	return 0;
1071 }
1072 
1073 #ifdef CONFIG_PM_SLEEP
1074 
1075 static int snd_at73c213_suspend(struct device *dev)
1076 {
1077 	struct snd_card *card = dev_get_drvdata(dev);
1078 	struct snd_at73c213 *chip = card->private_data;
1079 
1080 	ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXDIS));
1081 	clk_disable(chip->ssc->clk);
1082 	clk_disable(chip->board->dac_clk);
1083 
1084 	return 0;
1085 }
1086 
1087 static int snd_at73c213_resume(struct device *dev)
1088 {
1089 	struct snd_card *card = dev_get_drvdata(dev);
1090 	struct snd_at73c213 *chip = card->private_data;
1091 
1092 	clk_enable(chip->board->dac_clk);
1093 	clk_enable(chip->ssc->clk);
1094 	ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXEN));
1095 
1096 	return 0;
1097 }
1098 
1099 static SIMPLE_DEV_PM_OPS(at73c213_pm_ops, snd_at73c213_suspend,
1100 		snd_at73c213_resume);
1101 #define AT73C213_PM_OPS (&at73c213_pm_ops)
1102 
1103 #else
1104 #define AT73C213_PM_OPS NULL
1105 #endif
1106 
1107 static struct spi_driver at73c213_driver = {
1108 	.driver		= {
1109 		.name	= "at73c213",
1110 		.pm	= AT73C213_PM_OPS,
1111 	},
1112 	.probe		= snd_at73c213_probe,
1113 	.remove		= snd_at73c213_remove,
1114 };
1115 
1116 module_spi_driver(at73c213_driver);
1117 
1118 MODULE_AUTHOR("Hans-Christian Egtvedt <egtvedt@samfundet.no>");
1119 MODULE_DESCRIPTION("Sound driver for AT73C213 with Atmel SSC");
1120 MODULE_LICENSE("GPL");
1121