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