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