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