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