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