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