blob: 5e8cf9f44ca314656e7f596a95e8208158689b0a [file] [log] [blame]
Hans-Christian Egtvedteafe5702007-07-23 16:01:38 +02001/*
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>
Hans-Christian Egtvedtf488d9f2007-12-17 17:30:06 +010021#include <linux/mutex.h>
Hans-Christian Egtvedteafe5702007-07-23 16:01:38 +020022#include <linux/platform_device.h>
23#include <linux/io.h>
24
25#include <sound/driver.h>
26#include <sound/initval.h>
27#include <sound/control.h>
28#include <sound/core.h>
29#include <sound/pcm.h>
30
31#include <linux/atmel-ssc.h>
32
33#include <linux/spi/spi.h>
34#include <linux/spi/at73c213.h>
35
36#include "at73c213.h"
37
38#define BITRATE_MIN 8000 /* Hardware limit? */
39#define BITRATE_TARGET CONFIG_SND_AT73C213_TARGET_BITRATE
40#define BITRATE_MAX 50000 /* Hardware limit. */
41
42/* Initial (hardware reset) AT73C213 register values. */
43static u8 snd_at73c213_original_image[18] =
44{
45 0x00, /* 00 - CTRL */
46 0x05, /* 01 - LLIG */
47 0x05, /* 02 - RLIG */
48 0x08, /* 03 - LPMG */
49 0x08, /* 04 - RPMG */
50 0x00, /* 05 - LLOG */
51 0x00, /* 06 - RLOG */
52 0x22, /* 07 - OLC */
53 0x09, /* 08 - MC */
54 0x00, /* 09 - CSFC */
55 0x00, /* 0A - MISC */
56 0x00, /* 0B - */
57 0x00, /* 0C - PRECH */
58 0x05, /* 0D - AUXG */
59 0x00, /* 0E - */
60 0x00, /* 0F - */
61 0x00, /* 10 - RST */
62 0x00, /* 11 - PA_CTRL */
63};
64
65struct snd_at73c213 {
66 struct snd_card *card;
67 struct snd_pcm *pcm;
68 struct snd_pcm_substream *substream;
69 struct at73c213_board_info *board;
70 int irq;
71 int period;
72 unsigned long bitrate;
73 struct clk *bitclk;
74 struct ssc_device *ssc;
75 struct spi_device *spi;
76 u8 spi_wbuffer[2];
77 u8 spi_rbuffer[2];
78 /* Image of the SPI registers in AT73C213. */
79 u8 reg_image[18];
Hans-Christian Egtvedtf488d9f2007-12-17 17:30:06 +010080 /* Protect SSC registers against concurrent access. */
Hans-Christian Egtvedteafe5702007-07-23 16:01:38 +020081 spinlock_t lock;
Hans-Christian Egtvedtf488d9f2007-12-17 17:30:06 +010082 /* Protect mixer registers against concurrent access. */
83 struct mutex mixer_lock;
Hans-Christian Egtvedteafe5702007-07-23 16:01:38 +020084};
85
86#define get_chip(card) ((struct snd_at73c213 *)card->private_data)
87
88static int
89snd_at73c213_write_reg(struct snd_at73c213 *chip, u8 reg, u8 val)
90{
91 struct spi_message msg;
92 struct spi_transfer msg_xfer = {
93 .len = 2,
94 .cs_change = 0,
95 };
96 int retval;
97
98 spi_message_init(&msg);
99
100 chip->spi_wbuffer[0] = reg;
101 chip->spi_wbuffer[1] = val;
102
103 msg_xfer.tx_buf = chip->spi_wbuffer;
104 msg_xfer.rx_buf = chip->spi_rbuffer;
105 spi_message_add_tail(&msg_xfer, &msg);
106
107 retval = spi_sync(chip->spi, &msg);
108
109 if (!retval)
110 chip->reg_image[reg] = val;
111
112 return retval;
113}
114
115static struct snd_pcm_hardware snd_at73c213_playback_hw = {
116 .info = SNDRV_PCM_INFO_INTERLEAVED |
117 SNDRV_PCM_INFO_BLOCK_TRANSFER,
118 .formats = SNDRV_PCM_FMTBIT_S16_BE,
119 .rates = SNDRV_PCM_RATE_CONTINUOUS,
120 .rate_min = 8000, /* Replaced by chip->bitrate later. */
121 .rate_max = 50000, /* Replaced by chip->bitrate later. */
122 .channels_min = 2,
123 .channels_max = 2,
124 .buffer_bytes_max = 64 * 1024 - 1,
125 .period_bytes_min = 512,
126 .period_bytes_max = 64 * 1024 - 1,
127 .periods_min = 4,
128 .periods_max = 1024,
129};
130
131/*
132 * Calculate and set bitrate and divisions.
133 */
134static int snd_at73c213_set_bitrate(struct snd_at73c213 *chip)
135{
136 unsigned long ssc_rate = clk_get_rate(chip->ssc->clk);
137 unsigned long dac_rate_new, ssc_div, status;
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{
232 return snd_pcm_lib_malloc_pages(substream,
233 params_buffer_bytes(hw_params));
234}
235
236static int snd_at73c213_pcm_hw_free(struct snd_pcm_substream *substream)
237{
238 return snd_pcm_lib_free_pages(substream);
239}
240
241static int snd_at73c213_pcm_prepare(struct snd_pcm_substream *substream)
242{
243 struct snd_at73c213 *chip = snd_pcm_substream_chip(substream);
244 struct snd_pcm_runtime *runtime = substream->runtime;
245 int block_size;
246
247 block_size = frames_to_bytes(runtime, runtime->period_size);
248
249 chip->period = 0;
250
251 ssc_writel(chip->ssc->regs, PDC_TPR,
252 (long)runtime->dma_addr);
253 ssc_writel(chip->ssc->regs, PDC_TCR, runtime->period_size * 2);
254 ssc_writel(chip->ssc->regs, PDC_TNPR,
255 (long)runtime->dma_addr + block_size);
256 ssc_writel(chip->ssc->regs, PDC_TNCR, runtime->period_size * 2);
257
258 return 0;
259}
260
261static int snd_at73c213_pcm_trigger(struct snd_pcm_substream *substream,
262 int cmd)
263{
264 struct snd_at73c213 *chip = snd_pcm_substream_chip(substream);
265 int retval = 0;
266
267 spin_lock(&chip->lock);
268
269 switch (cmd) {
270 case SNDRV_PCM_TRIGGER_START:
271 ssc_writel(chip->ssc->regs, IER, SSC_BIT(IER_ENDTX));
272 ssc_writel(chip->ssc->regs, PDC_PTCR, SSC_BIT(PDC_PTCR_TXTEN));
273 break;
274 case SNDRV_PCM_TRIGGER_STOP:
275 ssc_writel(chip->ssc->regs, PDC_PTCR, SSC_BIT(PDC_PTCR_TXTDIS));
276 ssc_writel(chip->ssc->regs, IDR, SSC_BIT(IDR_ENDTX));
277 break;
278 default:
279 dev_dbg(&chip->spi->dev, "spurious command %x\n", cmd);
280 retval = -EINVAL;
281 break;
282 }
283
284 spin_unlock(&chip->lock);
285
286 return retval;
287}
288
289static snd_pcm_uframes_t
290snd_at73c213_pcm_pointer(struct snd_pcm_substream *substream)
291{
292 struct snd_at73c213 *chip = snd_pcm_substream_chip(substream);
293 struct snd_pcm_runtime *runtime = substream->runtime;
294 snd_pcm_uframes_t pos;
295 unsigned long bytes;
296
297 bytes = ssc_readl(chip->ssc->regs, PDC_TPR)
298 - (unsigned long)runtime->dma_addr;
299
300 pos = bytes_to_frames(runtime, bytes);
301 if (pos >= runtime->buffer_size)
302 pos -= runtime->buffer_size;
303
304 return pos;
305}
306
307static struct snd_pcm_ops at73c213_playback_ops = {
308 .open = snd_at73c213_pcm_open,
309 .close = snd_at73c213_pcm_close,
310 .ioctl = snd_pcm_lib_ioctl,
311 .hw_params = snd_at73c213_pcm_hw_params,
312 .hw_free = snd_at73c213_pcm_hw_free,
313 .prepare = snd_at73c213_pcm_prepare,
314 .trigger = snd_at73c213_pcm_trigger,
315 .pointer = snd_at73c213_pcm_pointer,
316};
317
318static void snd_at73c213_pcm_free(struct snd_pcm *pcm)
319{
320 struct snd_at73c213 *chip = snd_pcm_chip(pcm);
321 if (chip->pcm) {
322 snd_pcm_lib_preallocate_free_for_all(chip->pcm);
323 chip->pcm = NULL;
324 }
325}
326
327static int __devinit 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->private_free = snd_at73c213_pcm_free;
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);
379 ssc_writel(chip->ssc->regs, PDC_TNCR, runtime->period_size * 2);
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
Hans-Christian Egtvedtf488d9f2007-12-17 17:30:06 +0100404 mutex_lock(&chip->mixer_lock);
Hans-Christian Egtvedteafe5702007-07-23 16:01:38 +0200405
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
Hans-Christian Egtvedtf488d9f2007-12-17 17:30:06 +0100413 mutex_unlock(&chip->mixer_lock);
Hans-Christian Egtvedteafe5702007-07-23 16:01:38 +0200414
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
Hans-Christian Egtvedtf488d9f2007-12-17 17:30:06 +0100434 mutex_lock(&chip->mixer_lock);
Hans-Christian Egtvedteafe5702007-07-23 16:01:38 +0200435
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
Hans-Christian Egtvedtf488d9f2007-12-17 17:30:06 +0100440 mutex_unlock(&chip->mixer_lock);
Hans-Christian Egtvedteafe5702007-07-23 16:01:38 +0200441
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
Hans-Christian Egtvedtf488d9f2007-12-17 17:30:06 +0100476 mutex_lock(&chip->mixer_lock);
Hans-Christian Egtvedteafe5702007-07-23 16:01:38 +0200477
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
Hans-Christian Egtvedtf488d9f2007-12-17 17:30:06 +0100490 mutex_unlock(&chip->mixer_lock);
Hans-Christian Egtvedteafe5702007-07-23 16:01:38 +0200491
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
Hans-Christian Egtvedtf488d9f2007-12-17 17:30:06 +0100517 mutex_lock(&chip->mixer_lock);
Hans-Christian Egtvedteafe5702007-07-23 16:01:38 +0200518
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) {
Hans-Christian Egtvedtf488d9f2007-12-17 17:30:06 +0100525 mutex_unlock(&chip->mixer_lock);
Hans-Christian Egtvedteafe5702007-07-23 16:01:38 +0200526 goto out;
527 }
528 retval = snd_at73c213_write_reg(chip, right_reg, val2);
529 if (retval) {
Hans-Christian Egtvedtf488d9f2007-12-17 17:30:06 +0100530 mutex_unlock(&chip->mixer_lock);
Hans-Christian Egtvedteafe5702007-07-23 16:01:38 +0200531 goto out;
532 }
533
Hans-Christian Egtvedtf488d9f2007-12-17 17:30:06 +0100534 mutex_unlock(&chip->mixer_lock);
Hans-Christian Egtvedteafe5702007-07-23 16:01:38 +0200535
536 return change;
537
538out:
539 return retval;
540}
541
Takashi Iwai4b6c26a2007-11-15 16:18:14 +0100542#define snd_at73c213_mono_switch_info snd_ctl_boolean_mono_info
Hans-Christian Egtvedteafe5702007-07-23 16:01:38 +0200543
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
Hans-Christian Egtvedtf488d9f2007-12-17 17:30:06 +0100552 mutex_lock(&chip->mixer_lock);
Hans-Christian Egtvedteafe5702007-07-23 16:01:38 +0200553
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
Hans-Christian Egtvedtf488d9f2007-12-17 17:30:06 +0100561 mutex_unlock(&chip->mixer_lock);
Hans-Christian Egtvedteafe5702007-07-23 16:01:38 +0200562
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
Hans-Christian Egtvedtf488d9f2007-12-17 17:30:06 +0100586 mutex_lock(&chip->mixer_lock);
Hans-Christian Egtvedteafe5702007-07-23 16:01:38 +0200587
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
Hans-Christian Egtvedtf488d9f2007-12-17 17:30:06 +0100593 mutex_unlock(&chip->mixer_lock);
Hans-Christian Egtvedteafe5702007-07-23 16:01:38 +0200594
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 struct snd_kcontrol_new snd_at73c213_controls[] __devinitdata = {
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 __devinit 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 clk_enable(chip->board->dac_clk);
782
783 /* Initialize at73c213 on SPI bus. */
784 retval = snd_at73c213_write_reg(chip, DAC_RST, 0x04);
785 if (retval)
786 goto out_clk;
787 msleep(1);
788 retval = snd_at73c213_write_reg(chip, DAC_RST, 0x03);
789 if (retval)
790 goto out_clk;
791
792 /* Precharge everything. */
793 retval = snd_at73c213_write_reg(chip, DAC_PRECH, 0xff);
794 if (retval)
795 goto out_clk;
796 retval = snd_at73c213_write_reg(chip, PA_CTRL, (1<<PA_CTRL_APAPRECH));
797 if (retval)
798 goto out_clk;
799 retval = snd_at73c213_write_reg(chip, DAC_CTRL,
800 (1<<DAC_CTRL_ONLNOL) | (1<<DAC_CTRL_ONLNOR));
801 if (retval)
802 goto out_clk;
803
804 msleep(50);
805
806 /* Stop precharging PA. */
807 retval = snd_at73c213_write_reg(chip, PA_CTRL,
808 (1<<PA_CTRL_APALP) | 0x0f);
809 if (retval)
810 goto out_clk;
811
812 msleep(450);
813
814 /* Stop precharging DAC, turn on master power. */
815 retval = snd_at73c213_write_reg(chip, DAC_PRECH, (1<<DAC_PRECH_ONMSTR));
816 if (retval)
817 goto out_clk;
818
819 msleep(1);
820
821 /* Turn on DAC. */
822 dac_ctrl = (1<<DAC_CTRL_ONDACL) | (1<<DAC_CTRL_ONDACR)
823 | (1<<DAC_CTRL_ONLNOL) | (1<<DAC_CTRL_ONLNOR);
824
825 retval = snd_at73c213_write_reg(chip, DAC_CTRL, dac_ctrl);
826 if (retval)
827 goto out_clk;
828
829 /* Mute sound. */
830 retval = snd_at73c213_write_reg(chip, DAC_LMPG, 0x3f);
831 if (retval)
832 goto out_clk;
833 retval = snd_at73c213_write_reg(chip, DAC_RMPG, 0x3f);
834 if (retval)
835 goto out_clk;
836 retval = snd_at73c213_write_reg(chip, DAC_LLOG, 0x3f);
837 if (retval)
838 goto out_clk;
839 retval = snd_at73c213_write_reg(chip, DAC_RLOG, 0x3f);
840 if (retval)
841 goto out_clk;
842 retval = snd_at73c213_write_reg(chip, DAC_LLIG, 0x11);
843 if (retval)
844 goto out_clk;
845 retval = snd_at73c213_write_reg(chip, DAC_RLIG, 0x11);
846 if (retval)
847 goto out_clk;
848 retval = snd_at73c213_write_reg(chip, DAC_AUXG, 0x11);
849 if (retval)
850 goto out_clk;
851
852 /* Enable I2S device, i.e. clock output. */
853 ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXEN));
854
855 goto out;
856
857out_clk:
858 clk_disable(chip->board->dac_clk);
859out:
860 return retval;
861}
862
863static int snd_at73c213_dev_free(struct snd_device *device)
864{
865 struct snd_at73c213 *chip = device->device_data;
866
867 ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXDIS));
868 if (chip->irq >= 0) {
869 free_irq(chip->irq, chip);
870 chip->irq = -1;
871 }
872
873 return 0;
874}
875
876static int __devinit snd_at73c213_dev_init(struct snd_card *card,
877 struct spi_device *spi)
878{
879 static struct snd_device_ops ops = {
880 .dev_free = snd_at73c213_dev_free,
881 };
882 struct snd_at73c213 *chip = get_chip(card);
883 int irq, retval;
884
885 irq = chip->ssc->irq;
886 if (irq < 0)
887 return irq;
888
889 spin_lock_init(&chip->lock);
Hans-Christian Egtvedtf488d9f2007-12-17 17:30:06 +0100890 mutex_init(&chip->mixer_lock);
Hans-Christian Egtvedteafe5702007-07-23 16:01:38 +0200891 chip->card = card;
892 chip->irq = -1;
893
894 retval = request_irq(irq, snd_at73c213_interrupt, 0, "at73c213", chip);
895 if (retval) {
896 dev_dbg(&chip->spi->dev, "unable to request irq %d\n", irq);
897 goto out;
898 }
899 chip->irq = irq;
900
901 memcpy(&chip->reg_image, &snd_at73c213_original_image,
902 sizeof(snd_at73c213_original_image));
903
904 retval = snd_at73c213_ssc_init(chip);
905 if (retval)
906 goto out_irq;
907
908 retval = snd_at73c213_chip_init(chip);
909 if (retval)
910 goto out_irq;
911
912 retval = snd_at73c213_pcm_new(chip, 0);
913 if (retval)
914 goto out_irq;
915
916 retval = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
917 if (retval)
918 goto out_irq;
919
920 retval = snd_at73c213_mixer(chip);
921 if (retval)
922 goto out_snd_dev;
923
924 snd_card_set_dev(card, &spi->dev);
925
926 goto out;
927
928out_snd_dev:
929 snd_device_free(card, chip);
930out_irq:
931 free_irq(chip->irq, chip);
932 chip->irq = -1;
933out:
934 return retval;
935}
936
937static int snd_at73c213_probe(struct spi_device *spi)
938{
939 struct snd_card *card;
940 struct snd_at73c213 *chip;
941 struct at73c213_board_info *board;
942 int retval;
943 char id[16];
944
945 board = spi->dev.platform_data;
946 if (!board) {
947 dev_dbg(&spi->dev, "no platform_data\n");
948 return -ENXIO;
949 }
950
951 if (!board->dac_clk) {
952 dev_dbg(&spi->dev, "no DAC clk\n");
953 return -ENXIO;
954 }
955
956 if (IS_ERR(board->dac_clk)) {
957 dev_dbg(&spi->dev, "no DAC clk\n");
958 return PTR_ERR(board->dac_clk);
959 }
960
961 retval = -ENOMEM;
962
963 /* Allocate "card" using some unused identifiers. */
964 snprintf(id, sizeof id, "at73c213_%d", board->ssc_id);
965 card = snd_card_new(-1, id, THIS_MODULE, sizeof(struct snd_at73c213));
966 if (!card)
967 goto out;
968
969 chip = card->private_data;
970 chip->spi = spi;
971 chip->board = board;
972
973 chip->ssc = ssc_request(board->ssc_id);
974 if (IS_ERR(chip->ssc)) {
975 dev_dbg(&spi->dev, "could not get ssc%d device\n",
976 board->ssc_id);
977 retval = PTR_ERR(chip->ssc);
978 goto out_card;
979 }
980
981 retval = snd_at73c213_dev_init(card, spi);
982 if (retval)
983 goto out_ssc;
984
985 strcpy(card->driver, "at73c213");
986 strcpy(card->shortname, board->shortname);
987 sprintf(card->longname, "%s on irq %d", card->shortname, chip->irq);
988
989 retval = snd_card_register(card);
990 if (retval)
991 goto out_ssc;
992
993 dev_set_drvdata(&spi->dev, card);
994
995 goto out;
996
997out_ssc:
998 ssc_free(chip->ssc);
999out_card:
1000 snd_card_free(card);
1001out:
1002 return retval;
1003}
1004
1005static int __devexit snd_at73c213_remove(struct spi_device *spi)
1006{
1007 struct snd_card *card = dev_get_drvdata(&spi->dev);
1008 struct snd_at73c213 *chip = card->private_data;
1009 int retval;
1010
1011 /* Stop playback. */
1012 ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXDIS));
1013
1014 /* Mute sound. */
1015 retval = snd_at73c213_write_reg(chip, DAC_LMPG, 0x3f);
1016 if (retval)
1017 goto out;
1018 retval = snd_at73c213_write_reg(chip, DAC_RMPG, 0x3f);
1019 if (retval)
1020 goto out;
1021 retval = snd_at73c213_write_reg(chip, DAC_LLOG, 0x3f);
1022 if (retval)
1023 goto out;
1024 retval = snd_at73c213_write_reg(chip, DAC_RLOG, 0x3f);
1025 if (retval)
1026 goto out;
1027 retval = snd_at73c213_write_reg(chip, DAC_LLIG, 0x11);
1028 if (retval)
1029 goto out;
1030 retval = snd_at73c213_write_reg(chip, DAC_RLIG, 0x11);
1031 if (retval)
1032 goto out;
1033 retval = snd_at73c213_write_reg(chip, DAC_AUXG, 0x11);
1034 if (retval)
1035 goto out;
1036
1037 /* Turn off PA. */
1038 retval = snd_at73c213_write_reg(chip, PA_CTRL,
1039 chip->reg_image[PA_CTRL] | 0x0f);
1040 if (retval)
1041 goto out;
1042 msleep(10);
1043 retval = snd_at73c213_write_reg(chip, PA_CTRL,
1044 (1 << PA_CTRL_APALP) | 0x0f);
1045 if (retval)
1046 goto out;
1047
1048 /* Turn off external DAC. */
1049 retval = snd_at73c213_write_reg(chip, DAC_CTRL, 0x0c);
1050 if (retval)
1051 goto out;
1052 msleep(2);
1053 retval = snd_at73c213_write_reg(chip, DAC_CTRL, 0x00);
1054 if (retval)
1055 goto out;
1056
1057 /* Turn off master power. */
1058 retval = snd_at73c213_write_reg(chip, DAC_PRECH, 0x00);
1059 if (retval)
1060 goto out;
1061
1062out:
1063 /* Stop DAC master clock. */
1064 clk_disable(chip->board->dac_clk);
1065
1066 ssc_free(chip->ssc);
1067 snd_card_free(card);
1068 dev_set_drvdata(&spi->dev, NULL);
1069
1070 return 0;
1071}
1072
1073#ifdef CONFIG_PM
1074static int snd_at73c213_suspend(struct spi_device *spi, pm_message_t msg)
1075{
1076 struct snd_card *card = dev_get_drvdata(&spi->dev);
1077 struct snd_at73c213 *chip = card->private_data;
1078
1079 ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXDIS));
1080 clk_disable(chip->board->dac_clk);
1081
1082 return 0;
1083}
1084
1085static int snd_at73c213_resume(struct spi_device *spi)
1086{
1087 struct snd_card *card = dev_get_drvdata(&spi->dev);
1088 struct snd_at73c213 *chip = card->private_data;
1089
1090 clk_enable(chip->board->dac_clk);
1091 ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXEN));
1092
1093 return 0;
1094}
1095#else
1096#define snd_at73c213_suspend NULL
1097#define snd_at73c213_resume NULL
1098#endif
1099
1100static struct spi_driver at73c213_driver = {
1101 .driver = {
1102 .name = "at73c213",
1103 },
1104 .probe = snd_at73c213_probe,
1105 .suspend = snd_at73c213_suspend,
1106 .resume = snd_at73c213_resume,
1107 .remove = __devexit_p(snd_at73c213_remove),
1108};
1109
1110static int __init at73c213_init(void)
1111{
1112 return spi_register_driver(&at73c213_driver);
1113}
1114module_init(at73c213_init);
1115
1116static void __exit at73c213_exit(void)
1117{
1118 spi_unregister_driver(&at73c213_driver);
1119}
1120module_exit(at73c213_exit);
1121
1122MODULE_AUTHOR("Hans-Christian Egtvedt <hcegtvedt@atmel.com>");
1123MODULE_DESCRIPTION("Sound driver for AT73C213 with Atmel SSC");
1124MODULE_LICENSE("GPL");