1 // SPDX-License-Identifier: GPL-2.0-only
3 * Apple Onboard Audio driver for Onyx codec
5 * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
7 * This is a driver for the pcm3052 codec chip (codenamed Onyx)
8 * that is present in newer Apple hardware (with digital output).
10 * The Onyx codec has the following connections (listed by the bit
11 * to be used in aoa_codec.connected):
16 * Note that even though I know of no machine that has for example
17 * the digital output connected but not the analog, I have handled
18 * all the different cases in the code so that this driver may serve
19 * as a good example of what to do.
21 * NOTE: This driver assumes that there's at most one chip to be
22 * used with one alsa card, in form of creating all kinds
23 * of mixer elements without regard for their existence.
24 * But snd-aoa assumes that there's at most one card, so
25 * this means you can only have one onyx on a system. This
26 * should probably be fixed by changing the assumption of
27 * having just a single card on a system, and making the
28 * 'card' pointer accessible to anyone who needs it instead
29 * of hiding it in the aoa_snd_* functions...
31 #include <linux/delay.h>
32 #include <linux/module.h>
33 #include <linux/slab.h>
34 MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
35 MODULE_LICENSE("GPL");
36 MODULE_DESCRIPTION("pcm3052 (onyx) codec driver for snd-aoa");
40 #include "../soundbus/soundbus.h"
43 #define PFX "snd-aoa-codec-onyx: "
46 /* cache registers 65 to 80, they are write-only! */
48 struct i2c_client *i2c;
49 struct aoa_codec codec;
55 struct codec_info *codec_info;
57 /* mutex serializes concurrent access to the device
62 #define codec_to_onyx(c) container_of(c, struct onyx, codec)
64 /* both return 0 if all ok, else on error */
65 static int onyx_read_register(struct onyx *onyx, u8 reg, u8 *value)
69 if (reg != ONYX_REG_CONTROL) {
70 *value = onyx->cache[reg-FIRSTREGISTER];
73 v = i2c_smbus_read_byte_data(onyx->i2c, reg);
79 onyx->cache[ONYX_REG_CONTROL-FIRSTREGISTER] = *value;
83 static int onyx_write_register(struct onyx *onyx, u8 reg, u8 value)
87 result = i2c_smbus_write_byte_data(onyx->i2c, reg, value);
89 onyx->cache[reg-FIRSTREGISTER] = value;
95 static int onyx_dev_register(struct snd_device *dev)
100 static const struct snd_device_ops ops = {
101 .dev_register = onyx_dev_register,
104 /* this is necessary because most alsa mixer programs
105 * can't properly handle the negative range */
106 #define VOLUME_RANGE_SHIFT 128
108 static int onyx_snd_vol_info(struct snd_kcontrol *kcontrol,
109 struct snd_ctl_elem_info *uinfo)
111 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
113 uinfo->value.integer.min = -128 + VOLUME_RANGE_SHIFT;
114 uinfo->value.integer.max = -1 + VOLUME_RANGE_SHIFT;
118 static int onyx_snd_vol_get(struct snd_kcontrol *kcontrol,
119 struct snd_ctl_elem_value *ucontrol)
121 struct onyx *onyx = snd_kcontrol_chip(kcontrol);
124 mutex_lock(&onyx->mutex);
125 onyx_read_register(onyx, ONYX_REG_DAC_ATTEN_LEFT, &l);
126 onyx_read_register(onyx, ONYX_REG_DAC_ATTEN_RIGHT, &r);
127 mutex_unlock(&onyx->mutex);
129 ucontrol->value.integer.value[0] = l + VOLUME_RANGE_SHIFT;
130 ucontrol->value.integer.value[1] = r + VOLUME_RANGE_SHIFT;
135 static int onyx_snd_vol_put(struct snd_kcontrol *kcontrol,
136 struct snd_ctl_elem_value *ucontrol)
138 struct onyx *onyx = snd_kcontrol_chip(kcontrol);
141 if (ucontrol->value.integer.value[0] < -128 + VOLUME_RANGE_SHIFT ||
142 ucontrol->value.integer.value[0] > -1 + VOLUME_RANGE_SHIFT)
144 if (ucontrol->value.integer.value[1] < -128 + VOLUME_RANGE_SHIFT ||
145 ucontrol->value.integer.value[1] > -1 + VOLUME_RANGE_SHIFT)
148 mutex_lock(&onyx->mutex);
149 onyx_read_register(onyx, ONYX_REG_DAC_ATTEN_LEFT, &l);
150 onyx_read_register(onyx, ONYX_REG_DAC_ATTEN_RIGHT, &r);
152 if (l + VOLUME_RANGE_SHIFT == ucontrol->value.integer.value[0] &&
153 r + VOLUME_RANGE_SHIFT == ucontrol->value.integer.value[1]) {
154 mutex_unlock(&onyx->mutex);
158 onyx_write_register(onyx, ONYX_REG_DAC_ATTEN_LEFT,
159 ucontrol->value.integer.value[0]
160 - VOLUME_RANGE_SHIFT);
161 onyx_write_register(onyx, ONYX_REG_DAC_ATTEN_RIGHT,
162 ucontrol->value.integer.value[1]
163 - VOLUME_RANGE_SHIFT);
164 mutex_unlock(&onyx->mutex);
169 static const struct snd_kcontrol_new volume_control = {
170 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
171 .name = "Master Playback Volume",
172 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
173 .info = onyx_snd_vol_info,
174 .get = onyx_snd_vol_get,
175 .put = onyx_snd_vol_put,
178 /* like above, this is necessary because a lot
179 * of alsa mixer programs don't handle ranges
180 * that don't start at 0 properly.
181 * even alsamixer is one of them... */
182 #define INPUTGAIN_RANGE_SHIFT (-3)
184 static int onyx_snd_inputgain_info(struct snd_kcontrol *kcontrol,
185 struct snd_ctl_elem_info *uinfo)
187 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
189 uinfo->value.integer.min = 3 + INPUTGAIN_RANGE_SHIFT;
190 uinfo->value.integer.max = 28 + INPUTGAIN_RANGE_SHIFT;
194 static int onyx_snd_inputgain_get(struct snd_kcontrol *kcontrol,
195 struct snd_ctl_elem_value *ucontrol)
197 struct onyx *onyx = snd_kcontrol_chip(kcontrol);
200 mutex_lock(&onyx->mutex);
201 onyx_read_register(onyx, ONYX_REG_ADC_CONTROL, &ig);
202 mutex_unlock(&onyx->mutex);
204 ucontrol->value.integer.value[0] =
205 (ig & ONYX_ADC_PGA_GAIN_MASK) + INPUTGAIN_RANGE_SHIFT;
210 static int onyx_snd_inputgain_put(struct snd_kcontrol *kcontrol,
211 struct snd_ctl_elem_value *ucontrol)
213 struct onyx *onyx = snd_kcontrol_chip(kcontrol);
216 if (ucontrol->value.integer.value[0] < 3 + INPUTGAIN_RANGE_SHIFT ||
217 ucontrol->value.integer.value[0] > 28 + INPUTGAIN_RANGE_SHIFT)
219 mutex_lock(&onyx->mutex);
220 onyx_read_register(onyx, ONYX_REG_ADC_CONTROL, &v);
222 n &= ~ONYX_ADC_PGA_GAIN_MASK;
223 n |= (ucontrol->value.integer.value[0] - INPUTGAIN_RANGE_SHIFT)
224 & ONYX_ADC_PGA_GAIN_MASK;
225 onyx_write_register(onyx, ONYX_REG_ADC_CONTROL, n);
226 mutex_unlock(&onyx->mutex);
231 static const struct snd_kcontrol_new inputgain_control = {
232 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
233 .name = "Master Capture Volume",
234 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
235 .info = onyx_snd_inputgain_info,
236 .get = onyx_snd_inputgain_get,
237 .put = onyx_snd_inputgain_put,
240 static int onyx_snd_capture_source_info(struct snd_kcontrol *kcontrol,
241 struct snd_ctl_elem_info *uinfo)
243 static const char * const texts[] = { "Line-In", "Microphone" };
245 return snd_ctl_enum_info(uinfo, 1, 2, texts);
248 static int onyx_snd_capture_source_get(struct snd_kcontrol *kcontrol,
249 struct snd_ctl_elem_value *ucontrol)
251 struct onyx *onyx = snd_kcontrol_chip(kcontrol);
254 mutex_lock(&onyx->mutex);
255 onyx_read_register(onyx, ONYX_REG_ADC_CONTROL, &v);
256 mutex_unlock(&onyx->mutex);
258 ucontrol->value.enumerated.item[0] = !!(v&ONYX_ADC_INPUT_MIC);
263 static void onyx_set_capture_source(struct onyx *onyx, int mic)
267 mutex_lock(&onyx->mutex);
268 onyx_read_register(onyx, ONYX_REG_ADC_CONTROL, &v);
269 v &= ~ONYX_ADC_INPUT_MIC;
271 v |= ONYX_ADC_INPUT_MIC;
272 onyx_write_register(onyx, ONYX_REG_ADC_CONTROL, v);
273 mutex_unlock(&onyx->mutex);
276 static int onyx_snd_capture_source_put(struct snd_kcontrol *kcontrol,
277 struct snd_ctl_elem_value *ucontrol)
279 if (ucontrol->value.enumerated.item[0] > 1)
281 onyx_set_capture_source(snd_kcontrol_chip(kcontrol),
282 ucontrol->value.enumerated.item[0]);
286 static const struct snd_kcontrol_new capture_source_control = {
287 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
288 /* If we name this 'Input Source', it properly shows up in
289 * alsamixer as a selection, * but it's shown under the
290 * 'Playback' category.
291 * If I name it 'Capture Source', it shows up in strange
292 * ways (two bools of which one can be selected at a
293 * time) but at least it's shown in the 'Capture'
295 * I was told that this was due to backward compatibility,
296 * but I don't understand then why the mangling is *not*
297 * done when I name it "Input Source".....
299 .name = "Capture Source",
300 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
301 .info = onyx_snd_capture_source_info,
302 .get = onyx_snd_capture_source_get,
303 .put = onyx_snd_capture_source_put,
306 #define onyx_snd_mute_info snd_ctl_boolean_stereo_info
308 static int onyx_snd_mute_get(struct snd_kcontrol *kcontrol,
309 struct snd_ctl_elem_value *ucontrol)
311 struct onyx *onyx = snd_kcontrol_chip(kcontrol);
314 mutex_lock(&onyx->mutex);
315 onyx_read_register(onyx, ONYX_REG_DAC_CONTROL, &c);
316 mutex_unlock(&onyx->mutex);
318 ucontrol->value.integer.value[0] = !(c & ONYX_MUTE_LEFT);
319 ucontrol->value.integer.value[1] = !(c & ONYX_MUTE_RIGHT);
324 static int onyx_snd_mute_put(struct snd_kcontrol *kcontrol,
325 struct snd_ctl_elem_value *ucontrol)
327 struct onyx *onyx = snd_kcontrol_chip(kcontrol);
331 mutex_lock(&onyx->mutex);
332 if (onyx->analog_locked)
335 onyx_read_register(onyx, ONYX_REG_DAC_CONTROL, &v);
337 c &= ~(ONYX_MUTE_RIGHT | ONYX_MUTE_LEFT);
338 if (!ucontrol->value.integer.value[0])
340 if (!ucontrol->value.integer.value[1])
341 c |= ONYX_MUTE_RIGHT;
342 err = onyx_write_register(onyx, ONYX_REG_DAC_CONTROL, c);
345 mutex_unlock(&onyx->mutex);
347 return !err ? (v != c) : err;
350 static const struct snd_kcontrol_new mute_control = {
351 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
352 .name = "Master Playback Switch",
353 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
354 .info = onyx_snd_mute_info,
355 .get = onyx_snd_mute_get,
356 .put = onyx_snd_mute_put,
360 #define onyx_snd_single_bit_info snd_ctl_boolean_mono_info
362 #define FLAG_POLARITY_INVERT 1
363 #define FLAG_SPDIFLOCK 2
365 static int onyx_snd_single_bit_get(struct snd_kcontrol *kcontrol,
366 struct snd_ctl_elem_value *ucontrol)
368 struct onyx *onyx = snd_kcontrol_chip(kcontrol);
370 long int pv = kcontrol->private_value;
371 u8 polarity = (pv >> 16) & FLAG_POLARITY_INVERT;
372 u8 address = (pv >> 8) & 0xff;
375 mutex_lock(&onyx->mutex);
376 onyx_read_register(onyx, address, &c);
377 mutex_unlock(&onyx->mutex);
379 ucontrol->value.integer.value[0] = !!(c & mask) ^ polarity;
384 static int onyx_snd_single_bit_put(struct snd_kcontrol *kcontrol,
385 struct snd_ctl_elem_value *ucontrol)
387 struct onyx *onyx = snd_kcontrol_chip(kcontrol);
390 long int pv = kcontrol->private_value;
391 u8 polarity = (pv >> 16) & FLAG_POLARITY_INVERT;
392 u8 spdiflock = (pv >> 16) & FLAG_SPDIFLOCK;
393 u8 address = (pv >> 8) & 0xff;
396 mutex_lock(&onyx->mutex);
397 if (spdiflock && onyx->spdif_locked) {
398 /* even if alsamixer doesn't care.. */
402 onyx_read_register(onyx, address, &v);
405 if (!!ucontrol->value.integer.value[0] ^ polarity)
407 err = onyx_write_register(onyx, address, c);
410 mutex_unlock(&onyx->mutex);
412 return !err ? (v != c) : err;
415 #define SINGLE_BIT(n, type, description, address, mask, flags) \
416 static const struct snd_kcontrol_new n##_control = { \
417 .iface = SNDRV_CTL_ELEM_IFACE_##type, \
418 .name = description, \
419 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, \
420 .info = onyx_snd_single_bit_info, \
421 .get = onyx_snd_single_bit_get, \
422 .put = onyx_snd_single_bit_put, \
423 .private_value = (flags << 16) | (address << 8) | mask \
428 SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
435 ONYX_REG_DAC_CONTROL,
440 "Fast Digital Filter Rolloff",
443 FLAG_POLARITY_INVERT);
447 ONYX_REG_ADC_HPF_BYPASS,
449 FLAG_POLARITY_INVERT);
452 "Digital De-Emphasis",
457 static int onyx_spdif_info(struct snd_kcontrol *kcontrol,
458 struct snd_ctl_elem_info *uinfo)
460 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
465 static int onyx_spdif_mask_get(struct snd_kcontrol *kcontrol,
466 struct snd_ctl_elem_value *ucontrol)
468 /* datasheet page 30, all others are 0 */
469 ucontrol->value.iec958.status[0] = 0x3e;
470 ucontrol->value.iec958.status[1] = 0xff;
472 ucontrol->value.iec958.status[3] = 0x3f;
473 ucontrol->value.iec958.status[4] = 0x0f;
478 static const struct snd_kcontrol_new onyx_spdif_mask = {
479 .access = SNDRV_CTL_ELEM_ACCESS_READ,
480 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
481 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
482 .info = onyx_spdif_info,
483 .get = onyx_spdif_mask_get,
486 static int onyx_spdif_get(struct snd_kcontrol *kcontrol,
487 struct snd_ctl_elem_value *ucontrol)
489 struct onyx *onyx = snd_kcontrol_chip(kcontrol);
492 mutex_lock(&onyx->mutex);
493 onyx_read_register(onyx, ONYX_REG_DIG_INFO1, &v);
494 ucontrol->value.iec958.status[0] = v & 0x3e;
496 onyx_read_register(onyx, ONYX_REG_DIG_INFO2, &v);
497 ucontrol->value.iec958.status[1] = v;
499 onyx_read_register(onyx, ONYX_REG_DIG_INFO3, &v);
500 ucontrol->value.iec958.status[3] = v & 0x3f;
502 onyx_read_register(onyx, ONYX_REG_DIG_INFO4, &v);
503 ucontrol->value.iec958.status[4] = v & 0x0f;
504 mutex_unlock(&onyx->mutex);
509 static int onyx_spdif_put(struct snd_kcontrol *kcontrol,
510 struct snd_ctl_elem_value *ucontrol)
512 struct onyx *onyx = snd_kcontrol_chip(kcontrol);
515 mutex_lock(&onyx->mutex);
516 onyx_read_register(onyx, ONYX_REG_DIG_INFO1, &v);
517 v = (v & ~0x3e) | (ucontrol->value.iec958.status[0] & 0x3e);
518 onyx_write_register(onyx, ONYX_REG_DIG_INFO1, v);
520 v = ucontrol->value.iec958.status[1];
521 onyx_write_register(onyx, ONYX_REG_DIG_INFO2, v);
523 onyx_read_register(onyx, ONYX_REG_DIG_INFO3, &v);
524 v = (v & ~0x3f) | (ucontrol->value.iec958.status[3] & 0x3f);
525 onyx_write_register(onyx, ONYX_REG_DIG_INFO3, v);
527 onyx_read_register(onyx, ONYX_REG_DIG_INFO4, &v);
528 v = (v & ~0x0f) | (ucontrol->value.iec958.status[4] & 0x0f);
529 onyx_write_register(onyx, ONYX_REG_DIG_INFO4, v);
530 mutex_unlock(&onyx->mutex);
535 static const struct snd_kcontrol_new onyx_spdif_ctrl = {
536 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
537 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
538 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
539 .info = onyx_spdif_info,
540 .get = onyx_spdif_get,
541 .put = onyx_spdif_put,
546 static const u8 register_map[] = {
547 ONYX_REG_DAC_ATTEN_LEFT,
548 ONYX_REG_DAC_ATTEN_RIGHT,
550 ONYX_REG_DAC_CONTROL,
553 ONYX_REG_DAC_OUTPHASE,
554 ONYX_REG_ADC_CONTROL,
555 ONYX_REG_ADC_HPF_BYPASS,
562 static const u8 initial_values[ARRAY_SIZE(register_map)] = {
563 0x80, 0x80, /* muted */
564 ONYX_MRST | ONYX_SRST, /* but handled specially! */
565 ONYX_MUTE_LEFT | ONYX_MUTE_RIGHT,
566 0, /* no deemphasis */
567 ONYX_DAC_FILTER_ALWAYS,
568 ONYX_OUTPHASE_INVERTED,
569 (-1 /*dB*/ + 8) & 0xF, /* line in selected, -1 dB gain*/
571 (1<<2), /* pcm audio */
572 2, /* category: pcm coder */
573 0, /* sampling frequency 44.1 kHz, clock accuracy level II */
577 /* reset registers of chip, either to initial or to previous values */
578 static int onyx_register_init(struct onyx *onyx)
582 u8 regs[sizeof(initial_values)];
584 if (!onyx->initialised) {
585 memcpy(regs, initial_values, sizeof(initial_values));
586 if (onyx_read_register(onyx, ONYX_REG_CONTROL, &val))
588 val &= ~ONYX_SILICONVERSION;
589 val |= initial_values[3];
592 for (i=0; i<sizeof(register_map); i++)
593 regs[i] = onyx->cache[register_map[i]-FIRSTREGISTER];
596 for (i=0; i<sizeof(register_map); i++) {
597 if (onyx_write_register(onyx, register_map[i], regs[i]))
600 onyx->initialised = 1;
604 static struct transfer_info onyx_transfers[] = {
605 /* this is first so we can skip it if no input is present...
606 * No hardware exists with that, but it's here as an example
607 * of what to do :) */
610 .formats = SNDRV_PCM_FMTBIT_S8 |
611 SNDRV_PCM_FMTBIT_S16_BE |
612 SNDRV_PCM_FMTBIT_S24_BE,
613 .rates = SNDRV_PCM_RATE_8000_96000,
615 .must_be_clock_source = 0,
619 /* if analog and digital are currently off, anything should go,
620 * so this entry describes everything we can do... */
621 .formats = SNDRV_PCM_FMTBIT_S8 |
622 SNDRV_PCM_FMTBIT_S16_BE |
623 SNDRV_PCM_FMTBIT_S24_BE
624 #ifdef SNDRV_PCM_FMTBIT_COMPRESSED_16BE
625 | SNDRV_PCM_FMTBIT_COMPRESSED_16BE
628 .rates = SNDRV_PCM_RATE_8000_96000,
633 .formats = SNDRV_PCM_FMTBIT_S8 |
634 SNDRV_PCM_FMTBIT_S16_BE |
635 SNDRV_PCM_FMTBIT_S24_BE,
636 .rates = SNDRV_PCM_RATE_8000_96000,
638 .must_be_clock_source = 0,
642 /* digital pcm output, also possible for analog out */
643 .formats = SNDRV_PCM_FMTBIT_S8 |
644 SNDRV_PCM_FMTBIT_S16_BE |
645 SNDRV_PCM_FMTBIT_S24_BE,
646 .rates = SNDRV_PCM_RATE_32000 |
647 SNDRV_PCM_RATE_44100 |
648 SNDRV_PCM_RATE_48000,
650 .must_be_clock_source = 0,
653 #ifdef SNDRV_PCM_FMTBIT_COMPRESSED_16BE
654 /* Once alsa gets supports for this kind of thing we can add it... */
656 /* digital compressed output */
657 .formats = SNDRV_PCM_FMTBIT_COMPRESSED_16BE,
658 .rates = SNDRV_PCM_RATE_32000 |
659 SNDRV_PCM_RATE_44100 |
660 SNDRV_PCM_RATE_48000,
667 static int onyx_usable(struct codec_info_item *cii,
668 struct transfer_info *ti,
669 struct transfer_info *out)
672 struct onyx *onyx = cii->codec_data;
673 int spdif_enabled, analog_enabled;
675 mutex_lock(&onyx->mutex);
676 onyx_read_register(onyx, ONYX_REG_DIG_INFO4, &v);
677 spdif_enabled = !!(v & ONYX_SPDIF_ENABLE);
678 onyx_read_register(onyx, ONYX_REG_DAC_CONTROL, &v);
680 (v & (ONYX_MUTE_RIGHT|ONYX_MUTE_LEFT))
681 != (ONYX_MUTE_RIGHT|ONYX_MUTE_LEFT);
682 mutex_unlock(&onyx->mutex);
686 case 1: return analog_enabled;
687 case 2: return spdif_enabled;
692 static int onyx_prepare(struct codec_info_item *cii,
694 struct snd_pcm_substream *substream)
697 struct onyx *onyx = cii->codec_data;
700 mutex_lock(&onyx->mutex);
702 #ifdef SNDRV_PCM_FMTBIT_COMPRESSED_16BE
703 if (substream->runtime->format == SNDRV_PCM_FMTBIT_COMPRESSED_16BE) {
704 /* mute and lock analog output */
705 onyx_read_register(onyx, ONYX_REG_DAC_CONTROL, &v);
706 if (onyx_write_register(onyx,
707 ONYX_REG_DAC_CONTROL,
708 v | ONYX_MUTE_RIGHT | ONYX_MUTE_LEFT))
710 onyx->analog_locked = 1;
715 switch (substream->runtime->rate) {
719 /* these rates are ok for all outputs */
720 /* FIXME: program spdif channel control bits here so that
721 * userspace doesn't have to if it only plays pcm! */
725 /* got some rate that the digital output can't do,
726 * so disable and lock it */
727 onyx_read_register(cii->codec_data, ONYX_REG_DIG_INFO4, &v);
728 if (onyx_write_register(onyx,
730 v & ~ONYX_SPDIF_ENABLE))
732 onyx->spdif_locked = 1;
738 mutex_unlock(&onyx->mutex);
743 static int onyx_open(struct codec_info_item *cii,
744 struct snd_pcm_substream *substream)
746 struct onyx *onyx = cii->codec_data;
748 mutex_lock(&onyx->mutex);
750 mutex_unlock(&onyx->mutex);
755 static int onyx_close(struct codec_info_item *cii,
756 struct snd_pcm_substream *substream)
758 struct onyx *onyx = cii->codec_data;
760 mutex_lock(&onyx->mutex);
762 if (!onyx->open_count)
763 onyx->spdif_locked = onyx->analog_locked = 0;
764 mutex_unlock(&onyx->mutex);
769 static int onyx_switch_clock(struct codec_info_item *cii,
770 enum clock_switch what)
772 struct onyx *onyx = cii->codec_data;
774 mutex_lock(&onyx->mutex);
775 /* this *MUST* be more elaborate later... */
777 case CLOCK_SWITCH_PREPARE_SLAVE:
778 onyx->codec.gpio->methods->all_amps_off(onyx->codec.gpio);
780 case CLOCK_SWITCH_SLAVE:
781 onyx->codec.gpio->methods->all_amps_restore(onyx->codec.gpio);
783 default: /* silence warning */
786 mutex_unlock(&onyx->mutex);
793 static int onyx_suspend(struct codec_info_item *cii, pm_message_t state)
795 struct onyx *onyx = cii->codec_data;
799 mutex_lock(&onyx->mutex);
800 if (onyx_read_register(onyx, ONYX_REG_CONTROL, &v))
802 onyx_write_register(onyx, ONYX_REG_CONTROL, v | ONYX_ADPSV | ONYX_DAPSV);
803 /* Apple does a sleep here but the datasheet says to do it on resume */
806 mutex_unlock(&onyx->mutex);
811 static int onyx_resume(struct codec_info_item *cii)
813 struct onyx *onyx = cii->codec_data;
817 mutex_lock(&onyx->mutex);
820 onyx->codec.gpio->methods->set_hw_reset(onyx->codec.gpio, 0);
822 onyx->codec.gpio->methods->set_hw_reset(onyx->codec.gpio, 1);
824 onyx->codec.gpio->methods->set_hw_reset(onyx->codec.gpio, 0);
827 /* take codec out of suspend (if it still is after reset) */
828 if (onyx_read_register(onyx, ONYX_REG_CONTROL, &v))
830 onyx_write_register(onyx, ONYX_REG_CONTROL, v & ~(ONYX_ADPSV | ONYX_DAPSV));
831 /* FIXME: should divide by sample rate, but 8k is the lowest we go */
832 msleep(2205000/8000);
833 /* reset all values */
834 onyx_register_init(onyx);
837 mutex_unlock(&onyx->mutex);
842 #endif /* CONFIG_PM */
844 static struct codec_info onyx_codec_info = {
845 .transfers = onyx_transfers,
846 .sysclock_factor = 256,
848 .owner = THIS_MODULE,
849 .usable = onyx_usable,
850 .prepare = onyx_prepare,
853 .switch_clock = onyx_switch_clock,
855 .suspend = onyx_suspend,
856 .resume = onyx_resume,
860 static int onyx_init_codec(struct aoa_codec *codec)
862 struct onyx *onyx = codec_to_onyx(codec);
863 struct snd_kcontrol *ctl;
864 struct codec_info *ci = &onyx_codec_info;
868 if (!onyx->codec.gpio || !onyx->codec.gpio->methods) {
869 printk(KERN_ERR PFX "gpios not assigned!!\n");
873 onyx->codec.gpio->methods->set_hw_reset(onyx->codec.gpio, 0);
875 onyx->codec.gpio->methods->set_hw_reset(onyx->codec.gpio, 1);
877 onyx->codec.gpio->methods->set_hw_reset(onyx->codec.gpio, 0);
880 if (onyx_register_init(onyx)) {
881 printk(KERN_ERR PFX "failed to initialise onyx registers\n");
885 if (aoa_snd_device_new(SNDRV_DEV_CODEC, onyx, &ops)) {
886 printk(KERN_ERR PFX "failed to create onyx snd device!\n");
890 /* nothing connected? what a joke! */
891 if ((onyx->codec.connected & 0xF) == 0)
894 /* if no inputs are present... */
895 if ((onyx->codec.connected & 0xC) == 0) {
896 if (!onyx->codec_info)
897 onyx->codec_info = kmalloc(sizeof(struct codec_info), GFP_KERNEL);
898 if (!onyx->codec_info)
900 ci = onyx->codec_info;
901 *ci = onyx_codec_info;
905 /* if no outputs are present... */
906 if ((onyx->codec.connected & 3) == 0) {
907 if (!onyx->codec_info)
908 onyx->codec_info = kmalloc(sizeof(struct codec_info), GFP_KERNEL);
909 if (!onyx->codec_info)
911 ci = onyx->codec_info;
912 /* this is fine as there have to be inputs
913 * if we end up in this part of the code */
914 *ci = onyx_codec_info;
915 ci->transfers[1].formats = 0;
918 if (onyx->codec.soundbus_dev->attach_codec(onyx->codec.soundbus_dev,
921 printk(KERN_ERR PFX "error creating onyx pcm\n");
926 ctl = snd_ctl_new1(&n, onyx); \
929 onyx->codec.soundbus_dev->pcm->device; \
930 err = aoa_snd_ctl_add(ctl); \
936 if (onyx->codec.soundbus_dev->pcm) {
937 /* give the user appropriate controls
938 * depending on what inputs are connected */
939 if ((onyx->codec.connected & 0xC) == 0xC)
940 ADDCTL(capture_source_control);
941 else if (onyx->codec.connected & 4)
942 onyx_set_capture_source(onyx, 0);
944 onyx_set_capture_source(onyx, 1);
945 if (onyx->codec.connected & 0xC)
946 ADDCTL(inputgain_control);
948 /* depending on what output is connected,
949 * give the user appropriate controls */
950 if (onyx->codec.connected & 1) {
951 ADDCTL(volume_control);
952 ADDCTL(mute_control);
953 ADDCTL(ovr1_control);
954 ADDCTL(flt0_control);
956 ADDCTL(dm12_control);
957 /* spdif control defaults to off */
959 if (onyx->codec.connected & 2) {
960 ADDCTL(onyx_spdif_mask);
961 ADDCTL(onyx_spdif_ctrl);
963 if ((onyx->codec.connected & 3) == 3)
964 ADDCTL(spdif_control);
965 /* if only S/PDIF is connected, enable it unconditionally */
966 if ((onyx->codec.connected & 3) == 2) {
967 onyx_read_register(onyx, ONYX_REG_DIG_INFO4, &v);
968 v |= ONYX_SPDIF_ENABLE;
969 onyx_write_register(onyx, ONYX_REG_DIG_INFO4, v);
973 printk(KERN_INFO PFX "attached to onyx codec via i2c\n");
977 onyx->codec.soundbus_dev->detach_codec(onyx->codec.soundbus_dev, onyx);
978 snd_device_free(aoa_get_card(), onyx);
982 static void onyx_exit_codec(struct aoa_codec *codec)
984 struct onyx *onyx = codec_to_onyx(codec);
986 if (!onyx->codec.soundbus_dev) {
987 printk(KERN_ERR PFX "onyx_exit_codec called without soundbus_dev!\n");
990 onyx->codec.soundbus_dev->detach_codec(onyx->codec.soundbus_dev, onyx);
993 static int onyx_i2c_probe(struct i2c_client *client,
994 const struct i2c_device_id *id)
996 struct device_node *node = client->dev.of_node;
1000 onyx = kzalloc(sizeof(struct onyx), GFP_KERNEL);
1005 mutex_init(&onyx->mutex);
1007 i2c_set_clientdata(client, onyx);
1009 /* we try to read from register ONYX_REG_CONTROL
1010 * to check if the codec is present */
1011 if (onyx_read_register(onyx, ONYX_REG_CONTROL, &dummy) != 0) {
1012 printk(KERN_ERR PFX "failed to read control register\n");
1016 strscpy(onyx->codec.name, "onyx", MAX_CODEC_NAME_LEN);
1017 onyx->codec.owner = THIS_MODULE;
1018 onyx->codec.init = onyx_init_codec;
1019 onyx->codec.exit = onyx_exit_codec;
1020 onyx->codec.node = of_node_get(node);
1022 if (aoa_codec_register(&onyx->codec)) {
1025 printk(KERN_DEBUG PFX "created and attached onyx instance\n");
1032 static int onyx_i2c_remove(struct i2c_client *client)
1034 struct onyx *onyx = i2c_get_clientdata(client);
1036 aoa_codec_unregister(&onyx->codec);
1037 of_node_put(onyx->codec.node);
1038 kfree(onyx->codec_info);
1043 static const struct i2c_device_id onyx_i2c_id[] = {
1044 { "MAC,pcm3052", 0 },
1047 MODULE_DEVICE_TABLE(i2c,onyx_i2c_id);
1049 static struct i2c_driver onyx_driver = {
1051 .name = "aoa_codec_onyx",
1053 .probe = onyx_i2c_probe,
1054 .remove = onyx_i2c_remove,
1055 .id_table = onyx_i2c_id,
1058 module_i2c_driver(onyx_driver);