2 * Apple Onboard Audio driver for Onyx codec
4 * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
6 * GPL v2, can be found in COPYING.
9 * This is a driver for the pcm3052 codec chip (codenamed Onyx)
10 * that is present in newer Apple hardware (with digital output).
12 * The Onyx codec has the following connections (listed by the bit
13 * to be used in aoa_codec.connected):
18 * Note that even though I know of no machine that has for example
19 * the digital output connected but not the analog, I have handled
20 * all the different cases in the code so that this driver may serve
21 * as a good example of what to do.
23 * NOTE: This driver assumes that there's at most one chip to be
24 * used with one alsa card, in form of creating all kinds
25 * of mixer elements without regard for their existence.
26 * But snd-aoa assumes that there's at most one card, so
27 * this means you can only have one onyx on a system. This
28 * should probably be fixed by changing the assumption of
29 * having just a single card on a system, and making the
30 * 'card' pointer accessible to anyone who needs it instead
31 * of hiding it in the aoa_snd_* functions...
34 #include <linux/delay.h>
35 #include <linux/module.h>
36 MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
37 MODULE_LICENSE("GPL");
38 MODULE_DESCRIPTION("pcm3052 (onyx) codec driver for snd-aoa");
42 #include "../soundbus/soundbus.h"
45 #define PFX "snd-aoa-codec-onyx: "
48 /* cache registers 65 to 80, they are write-only! */
50 struct i2c_client *i2c;
51 struct aoa_codec codec;
57 struct codec_info *codec_info;
59 /* mutex serializes concurrent access to the device
64 #define codec_to_onyx(c) container_of(c, struct onyx, codec)
66 /* both return 0 if all ok, else on error */
67 static int onyx_read_register(struct onyx *onyx, u8 reg, u8 *value)
71 if (reg != ONYX_REG_CONTROL) {
72 *value = onyx->cache[reg-FIRSTREGISTER];
75 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 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 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 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 char *texts[] = { "Line-In", "Microphone" };
245 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
247 uinfo->value.enumerated.items = 2;
248 if (uinfo->value.enumerated.item > 1)
249 uinfo->value.enumerated.item = 1;
250 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
254 static int onyx_snd_capture_source_get(struct snd_kcontrol *kcontrol,
255 struct snd_ctl_elem_value *ucontrol)
257 struct onyx *onyx = snd_kcontrol_chip(kcontrol);
260 mutex_lock(&onyx->mutex);
261 onyx_read_register(onyx, ONYX_REG_ADC_CONTROL, &v);
262 mutex_unlock(&onyx->mutex);
264 ucontrol->value.enumerated.item[0] = !!(v&ONYX_ADC_INPUT_MIC);
269 static void onyx_set_capture_source(struct onyx *onyx, int mic)
273 mutex_lock(&onyx->mutex);
274 onyx_read_register(onyx, ONYX_REG_ADC_CONTROL, &v);
275 v &= ~ONYX_ADC_INPUT_MIC;
277 v |= ONYX_ADC_INPUT_MIC;
278 onyx_write_register(onyx, ONYX_REG_ADC_CONTROL, v);
279 mutex_unlock(&onyx->mutex);
282 static int onyx_snd_capture_source_put(struct snd_kcontrol *kcontrol,
283 struct snd_ctl_elem_value *ucontrol)
285 if (ucontrol->value.enumerated.item[0] > 1)
287 onyx_set_capture_source(snd_kcontrol_chip(kcontrol),
288 ucontrol->value.enumerated.item[0]);
292 static struct snd_kcontrol_new capture_source_control = {
293 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
294 /* If we name this 'Input Source', it properly shows up in
295 * alsamixer as a selection, * but it's shown under the
296 * 'Playback' category.
297 * If I name it 'Capture Source', it shows up in strange
298 * ways (two bools of which one can be selected at a
299 * time) but at least it's shown in the 'Capture'
301 * I was told that this was due to backward compatibility,
302 * but I don't understand then why the mangling is *not*
303 * done when I name it "Input Source".....
305 .name = "Capture Source",
306 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
307 .info = onyx_snd_capture_source_info,
308 .get = onyx_snd_capture_source_get,
309 .put = onyx_snd_capture_source_put,
312 #define onyx_snd_mute_info snd_ctl_boolean_stereo_info
314 static int onyx_snd_mute_get(struct snd_kcontrol *kcontrol,
315 struct snd_ctl_elem_value *ucontrol)
317 struct onyx *onyx = snd_kcontrol_chip(kcontrol);
320 mutex_lock(&onyx->mutex);
321 onyx_read_register(onyx, ONYX_REG_DAC_CONTROL, &c);
322 mutex_unlock(&onyx->mutex);
324 ucontrol->value.integer.value[0] = !(c & ONYX_MUTE_LEFT);
325 ucontrol->value.integer.value[1] = !(c & ONYX_MUTE_RIGHT);
330 static int onyx_snd_mute_put(struct snd_kcontrol *kcontrol,
331 struct snd_ctl_elem_value *ucontrol)
333 struct onyx *onyx = snd_kcontrol_chip(kcontrol);
337 mutex_lock(&onyx->mutex);
338 if (onyx->analog_locked)
341 onyx_read_register(onyx, ONYX_REG_DAC_CONTROL, &v);
343 c &= ~(ONYX_MUTE_RIGHT | ONYX_MUTE_LEFT);
344 if (!ucontrol->value.integer.value[0])
346 if (!ucontrol->value.integer.value[1])
347 c |= ONYX_MUTE_RIGHT;
348 err = onyx_write_register(onyx, ONYX_REG_DAC_CONTROL, c);
351 mutex_unlock(&onyx->mutex);
353 return !err ? (v != c) : err;
356 static struct snd_kcontrol_new mute_control = {
357 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
358 .name = "Master Playback Switch",
359 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
360 .info = onyx_snd_mute_info,
361 .get = onyx_snd_mute_get,
362 .put = onyx_snd_mute_put,
366 #define onyx_snd_single_bit_info snd_ctl_boolean_mono_info
368 #define FLAG_POLARITY_INVERT 1
369 #define FLAG_SPDIFLOCK 2
371 static int onyx_snd_single_bit_get(struct snd_kcontrol *kcontrol,
372 struct snd_ctl_elem_value *ucontrol)
374 struct onyx *onyx = snd_kcontrol_chip(kcontrol);
376 long int pv = kcontrol->private_value;
377 u8 polarity = (pv >> 16) & FLAG_POLARITY_INVERT;
378 u8 address = (pv >> 8) & 0xff;
381 mutex_lock(&onyx->mutex);
382 onyx_read_register(onyx, address, &c);
383 mutex_unlock(&onyx->mutex);
385 ucontrol->value.integer.value[0] = !!(c & mask) ^ polarity;
390 static int onyx_snd_single_bit_put(struct snd_kcontrol *kcontrol,
391 struct snd_ctl_elem_value *ucontrol)
393 struct onyx *onyx = snd_kcontrol_chip(kcontrol);
396 long int pv = kcontrol->private_value;
397 u8 polarity = (pv >> 16) & FLAG_POLARITY_INVERT;
398 u8 spdiflock = (pv >> 16) & FLAG_SPDIFLOCK;
399 u8 address = (pv >> 8) & 0xff;
402 mutex_lock(&onyx->mutex);
403 if (spdiflock && onyx->spdif_locked) {
404 /* even if alsamixer doesn't care.. */
408 onyx_read_register(onyx, address, &v);
411 if (!!ucontrol->value.integer.value[0] ^ polarity)
413 err = onyx_write_register(onyx, address, c);
416 mutex_unlock(&onyx->mutex);
418 return !err ? (v != c) : err;
421 #define SINGLE_BIT(n, type, description, address, mask, flags) \
422 static struct snd_kcontrol_new n##_control = { \
423 .iface = SNDRV_CTL_ELEM_IFACE_##type, \
424 .name = description, \
425 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, \
426 .info = onyx_snd_single_bit_info, \
427 .get = onyx_snd_single_bit_get, \
428 .put = onyx_snd_single_bit_put, \
429 .private_value = (flags << 16) | (address << 8) | mask \
434 SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
441 ONYX_REG_DAC_CONTROL,
446 "Fast Digital Filter Rolloff",
449 FLAG_POLARITY_INVERT);
453 ONYX_REG_ADC_HPF_BYPASS,
455 FLAG_POLARITY_INVERT);
458 "Digital De-Emphasis",
463 static int onyx_spdif_info(struct snd_kcontrol *kcontrol,
464 struct snd_ctl_elem_info *uinfo)
466 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
471 static int onyx_spdif_mask_get(struct snd_kcontrol *kcontrol,
472 struct snd_ctl_elem_value *ucontrol)
474 /* datasheet page 30, all others are 0 */
475 ucontrol->value.iec958.status[0] = 0x3e;
476 ucontrol->value.iec958.status[1] = 0xff;
478 ucontrol->value.iec958.status[3] = 0x3f;
479 ucontrol->value.iec958.status[4] = 0x0f;
484 static struct snd_kcontrol_new onyx_spdif_mask = {
485 .access = SNDRV_CTL_ELEM_ACCESS_READ,
486 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
487 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
488 .info = onyx_spdif_info,
489 .get = onyx_spdif_mask_get,
492 static int onyx_spdif_get(struct snd_kcontrol *kcontrol,
493 struct snd_ctl_elem_value *ucontrol)
495 struct onyx *onyx = snd_kcontrol_chip(kcontrol);
498 mutex_lock(&onyx->mutex);
499 onyx_read_register(onyx, ONYX_REG_DIG_INFO1, &v);
500 ucontrol->value.iec958.status[0] = v & 0x3e;
502 onyx_read_register(onyx, ONYX_REG_DIG_INFO2, &v);
503 ucontrol->value.iec958.status[1] = v;
505 onyx_read_register(onyx, ONYX_REG_DIG_INFO3, &v);
506 ucontrol->value.iec958.status[3] = v & 0x3f;
508 onyx_read_register(onyx, ONYX_REG_DIG_INFO4, &v);
509 ucontrol->value.iec958.status[4] = v & 0x0f;
510 mutex_unlock(&onyx->mutex);
515 static int onyx_spdif_put(struct snd_kcontrol *kcontrol,
516 struct snd_ctl_elem_value *ucontrol)
518 struct onyx *onyx = snd_kcontrol_chip(kcontrol);
521 mutex_lock(&onyx->mutex);
522 onyx_read_register(onyx, ONYX_REG_DIG_INFO1, &v);
523 v = (v & ~0x3e) | (ucontrol->value.iec958.status[0] & 0x3e);
524 onyx_write_register(onyx, ONYX_REG_DIG_INFO1, v);
526 v = ucontrol->value.iec958.status[1];
527 onyx_write_register(onyx, ONYX_REG_DIG_INFO2, v);
529 onyx_read_register(onyx, ONYX_REG_DIG_INFO3, &v);
530 v = (v & ~0x3f) | (ucontrol->value.iec958.status[3] & 0x3f);
531 onyx_write_register(onyx, ONYX_REG_DIG_INFO3, v);
533 onyx_read_register(onyx, ONYX_REG_DIG_INFO4, &v);
534 v = (v & ~0x0f) | (ucontrol->value.iec958.status[4] & 0x0f);
535 onyx_write_register(onyx, ONYX_REG_DIG_INFO4, v);
536 mutex_unlock(&onyx->mutex);
541 static struct snd_kcontrol_new onyx_spdif_ctrl = {
542 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
543 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
544 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
545 .info = onyx_spdif_info,
546 .get = onyx_spdif_get,
547 .put = onyx_spdif_put,
552 static u8 register_map[] = {
553 ONYX_REG_DAC_ATTEN_LEFT,
554 ONYX_REG_DAC_ATTEN_RIGHT,
556 ONYX_REG_DAC_CONTROL,
559 ONYX_REG_DAC_OUTPHASE,
560 ONYX_REG_ADC_CONTROL,
561 ONYX_REG_ADC_HPF_BYPASS,
568 static u8 initial_values[ARRAY_SIZE(register_map)] = {
569 0x80, 0x80, /* muted */
570 ONYX_MRST | ONYX_SRST, /* but handled specially! */
571 ONYX_MUTE_LEFT | ONYX_MUTE_RIGHT,
572 0, /* no deemphasis */
573 ONYX_DAC_FILTER_ALWAYS,
574 ONYX_OUTPHASE_INVERTED,
575 (-1 /*dB*/ + 8) & 0xF, /* line in selected, -1 dB gain*/
577 (1<<2), /* pcm audio */
578 2, /* category: pcm coder */
579 0, /* sampling frequency 44.1 kHz, clock accuracy level II */
583 /* reset registers of chip, either to initial or to previous values */
584 static int onyx_register_init(struct onyx *onyx)
588 u8 regs[sizeof(initial_values)];
590 if (!onyx->initialised) {
591 memcpy(regs, initial_values, sizeof(initial_values));
592 if (onyx_read_register(onyx, ONYX_REG_CONTROL, &val))
594 val &= ~ONYX_SILICONVERSION;
595 val |= initial_values[3];
598 for (i=0; i<sizeof(register_map); i++)
599 regs[i] = onyx->cache[register_map[i]-FIRSTREGISTER];
602 for (i=0; i<sizeof(register_map); i++) {
603 if (onyx_write_register(onyx, register_map[i], regs[i]))
606 onyx->initialised = 1;
610 static struct transfer_info onyx_transfers[] = {
611 /* this is first so we can skip it if no input is present...
612 * No hardware exists with that, but it's here as an example
613 * of what to do :) */
616 .formats = SNDRV_PCM_FMTBIT_S8 |
617 SNDRV_PCM_FMTBIT_S16_BE |
618 SNDRV_PCM_FMTBIT_S24_BE,
619 .rates = SNDRV_PCM_RATE_8000_96000,
621 .must_be_clock_source = 0,
625 /* if analog and digital are currently off, anything should go,
626 * so this entry describes everything we can do... */
627 .formats = SNDRV_PCM_FMTBIT_S8 |
628 SNDRV_PCM_FMTBIT_S16_BE |
629 SNDRV_PCM_FMTBIT_S24_BE
630 #ifdef SNDRV_PCM_FMTBIT_COMPRESSED_16BE
631 | SNDRV_PCM_FMTBIT_COMPRESSED_16BE
634 .rates = SNDRV_PCM_RATE_8000_96000,
639 .formats = SNDRV_PCM_FMTBIT_S8 |
640 SNDRV_PCM_FMTBIT_S16_BE |
641 SNDRV_PCM_FMTBIT_S24_BE,
642 .rates = SNDRV_PCM_RATE_8000_96000,
644 .must_be_clock_source = 0,
648 /* digital pcm output, also possible for analog out */
649 .formats = SNDRV_PCM_FMTBIT_S8 |
650 SNDRV_PCM_FMTBIT_S16_BE |
651 SNDRV_PCM_FMTBIT_S24_BE,
652 .rates = SNDRV_PCM_RATE_32000 |
653 SNDRV_PCM_RATE_44100 |
654 SNDRV_PCM_RATE_48000,
656 .must_be_clock_source = 0,
659 #ifdef SNDRV_PCM_FMTBIT_COMPRESSED_16BE
660 /* Once alsa gets supports for this kind of thing we can add it... */
662 /* digital compressed output */
663 .formats = SNDRV_PCM_FMTBIT_COMPRESSED_16BE,
664 .rates = SNDRV_PCM_RATE_32000 |
665 SNDRV_PCM_RATE_44100 |
666 SNDRV_PCM_RATE_48000,
673 static int onyx_usable(struct codec_info_item *cii,
674 struct transfer_info *ti,
675 struct transfer_info *out)
678 struct onyx *onyx = cii->codec_data;
679 int spdif_enabled, analog_enabled;
681 mutex_lock(&onyx->mutex);
682 onyx_read_register(onyx, ONYX_REG_DIG_INFO4, &v);
683 spdif_enabled = !!(v & ONYX_SPDIF_ENABLE);
684 onyx_read_register(onyx, ONYX_REG_DAC_CONTROL, &v);
686 (v & (ONYX_MUTE_RIGHT|ONYX_MUTE_LEFT))
687 != (ONYX_MUTE_RIGHT|ONYX_MUTE_LEFT);
688 mutex_unlock(&onyx->mutex);
692 case 1: return analog_enabled;
693 case 2: return spdif_enabled;
698 static int onyx_prepare(struct codec_info_item *cii,
700 struct snd_pcm_substream *substream)
703 struct onyx *onyx = cii->codec_data;
706 mutex_lock(&onyx->mutex);
708 #ifdef SNDRV_PCM_FMTBIT_COMPRESSED_16BE
709 if (substream->runtime->format == SNDRV_PCM_FMTBIT_COMPRESSED_16BE) {
710 /* mute and lock analog output */
711 onyx_read_register(onyx, ONYX_REG_DAC_CONTROL, &v);
712 if (onyx_write_register(onyx,
713 ONYX_REG_DAC_CONTROL,
714 v | ONYX_MUTE_RIGHT | ONYX_MUTE_LEFT))
716 onyx->analog_locked = 1;
721 switch (substream->runtime->rate) {
725 /* these rates are ok for all outputs */
726 /* FIXME: program spdif channel control bits here so that
727 * userspace doesn't have to if it only plays pcm! */
731 /* got some rate that the digital output can't do,
732 * so disable and lock it */
733 onyx_read_register(cii->codec_data, ONYX_REG_DIG_INFO4, &v);
734 if (onyx_write_register(onyx,
736 v & ~ONYX_SPDIF_ENABLE))
738 onyx->spdif_locked = 1;
744 mutex_unlock(&onyx->mutex);
749 static int onyx_open(struct codec_info_item *cii,
750 struct snd_pcm_substream *substream)
752 struct onyx *onyx = cii->codec_data;
754 mutex_lock(&onyx->mutex);
756 mutex_unlock(&onyx->mutex);
761 static int onyx_close(struct codec_info_item *cii,
762 struct snd_pcm_substream *substream)
764 struct onyx *onyx = cii->codec_data;
766 mutex_lock(&onyx->mutex);
768 if (!onyx->open_count)
769 onyx->spdif_locked = onyx->analog_locked = 0;
770 mutex_unlock(&onyx->mutex);
775 static int onyx_switch_clock(struct codec_info_item *cii,
776 enum clock_switch what)
778 struct onyx *onyx = cii->codec_data;
780 mutex_lock(&onyx->mutex);
781 /* this *MUST* be more elaborate later... */
783 case CLOCK_SWITCH_PREPARE_SLAVE:
784 onyx->codec.gpio->methods->all_amps_off(onyx->codec.gpio);
786 case CLOCK_SWITCH_SLAVE:
787 onyx->codec.gpio->methods->all_amps_restore(onyx->codec.gpio);
789 default: /* silence warning */
792 mutex_unlock(&onyx->mutex);
799 static int onyx_suspend(struct codec_info_item *cii, pm_message_t state)
801 struct onyx *onyx = cii->codec_data;
805 mutex_lock(&onyx->mutex);
806 if (onyx_read_register(onyx, ONYX_REG_CONTROL, &v))
808 onyx_write_register(onyx, ONYX_REG_CONTROL, v | ONYX_ADPSV | ONYX_DAPSV);
809 /* Apple does a sleep here but the datasheet says to do it on resume */
812 mutex_unlock(&onyx->mutex);
817 static int onyx_resume(struct codec_info_item *cii)
819 struct onyx *onyx = cii->codec_data;
823 mutex_lock(&onyx->mutex);
826 onyx->codec.gpio->methods->set_hw_reset(onyx->codec.gpio, 0);
828 onyx->codec.gpio->methods->set_hw_reset(onyx->codec.gpio, 1);
830 onyx->codec.gpio->methods->set_hw_reset(onyx->codec.gpio, 0);
833 /* take codec out of suspend (if it still is after reset) */
834 if (onyx_read_register(onyx, ONYX_REG_CONTROL, &v))
836 onyx_write_register(onyx, ONYX_REG_CONTROL, v & ~(ONYX_ADPSV | ONYX_DAPSV));
837 /* FIXME: should divide by sample rate, but 8k is the lowest we go */
838 msleep(2205000/8000);
839 /* reset all values */
840 onyx_register_init(onyx);
843 mutex_unlock(&onyx->mutex);
848 #endif /* CONFIG_PM */
850 static struct codec_info onyx_codec_info = {
851 .transfers = onyx_transfers,
852 .sysclock_factor = 256,
854 .owner = THIS_MODULE,
855 .usable = onyx_usable,
856 .prepare = onyx_prepare,
859 .switch_clock = onyx_switch_clock,
861 .suspend = onyx_suspend,
862 .resume = onyx_resume,
866 static int onyx_init_codec(struct aoa_codec *codec)
868 struct onyx *onyx = codec_to_onyx(codec);
869 struct snd_kcontrol *ctl;
870 struct codec_info *ci = &onyx_codec_info;
874 if (!onyx->codec.gpio || !onyx->codec.gpio->methods) {
875 printk(KERN_ERR PFX "gpios not assigned!!\n");
879 onyx->codec.gpio->methods->set_hw_reset(onyx->codec.gpio, 0);
881 onyx->codec.gpio->methods->set_hw_reset(onyx->codec.gpio, 1);
883 onyx->codec.gpio->methods->set_hw_reset(onyx->codec.gpio, 0);
886 if (onyx_register_init(onyx)) {
887 printk(KERN_ERR PFX "failed to initialise onyx registers\n");
891 if (aoa_snd_device_new(SNDRV_DEV_LOWLEVEL, onyx, &ops)) {
892 printk(KERN_ERR PFX "failed to create onyx snd device!\n");
896 /* nothing connected? what a joke! */
897 if ((onyx->codec.connected & 0xF) == 0)
900 /* if no inputs are present... */
901 if ((onyx->codec.connected & 0xC) == 0) {
902 if (!onyx->codec_info)
903 onyx->codec_info = kmalloc(sizeof(struct codec_info), GFP_KERNEL);
904 if (!onyx->codec_info)
906 ci = onyx->codec_info;
907 *ci = onyx_codec_info;
911 /* if no outputs are present... */
912 if ((onyx->codec.connected & 3) == 0) {
913 if (!onyx->codec_info)
914 onyx->codec_info = kmalloc(sizeof(struct codec_info), GFP_KERNEL);
915 if (!onyx->codec_info)
917 ci = onyx->codec_info;
918 /* this is fine as there have to be inputs
919 * if we end up in this part of the code */
920 *ci = onyx_codec_info;
921 ci->transfers[1].formats = 0;
924 if (onyx->codec.soundbus_dev->attach_codec(onyx->codec.soundbus_dev,
927 printk(KERN_ERR PFX "error creating onyx pcm\n");
932 ctl = snd_ctl_new1(&n, onyx); \
935 onyx->codec.soundbus_dev->pcm->device; \
936 err = aoa_snd_ctl_add(ctl); \
942 if (onyx->codec.soundbus_dev->pcm) {
943 /* give the user appropriate controls
944 * depending on what inputs are connected */
945 if ((onyx->codec.connected & 0xC) == 0xC)
946 ADDCTL(capture_source_control);
947 else if (onyx->codec.connected & 4)
948 onyx_set_capture_source(onyx, 0);
950 onyx_set_capture_source(onyx, 1);
951 if (onyx->codec.connected & 0xC)
952 ADDCTL(inputgain_control);
954 /* depending on what output is connected,
955 * give the user appropriate controls */
956 if (onyx->codec.connected & 1) {
957 ADDCTL(volume_control);
958 ADDCTL(mute_control);
959 ADDCTL(ovr1_control);
960 ADDCTL(flt0_control);
962 ADDCTL(dm12_control);
963 /* spdif control defaults to off */
965 if (onyx->codec.connected & 2) {
966 ADDCTL(onyx_spdif_mask);
967 ADDCTL(onyx_spdif_ctrl);
969 if ((onyx->codec.connected & 3) == 3)
970 ADDCTL(spdif_control);
971 /* if only S/PDIF is connected, enable it unconditionally */
972 if ((onyx->codec.connected & 3) == 2) {
973 onyx_read_register(onyx, ONYX_REG_DIG_INFO4, &v);
974 v |= ONYX_SPDIF_ENABLE;
975 onyx_write_register(onyx, ONYX_REG_DIG_INFO4, v);
979 printk(KERN_INFO PFX "attached to onyx codec via i2c\n");
983 onyx->codec.soundbus_dev->detach_codec(onyx->codec.soundbus_dev, onyx);
984 snd_device_free(aoa_get_card(), onyx);
988 static void onyx_exit_codec(struct aoa_codec *codec)
990 struct onyx *onyx = codec_to_onyx(codec);
992 if (!onyx->codec.soundbus_dev) {
993 printk(KERN_ERR PFX "onyx_exit_codec called without soundbus_dev!\n");
996 onyx->codec.soundbus_dev->detach_codec(onyx->codec.soundbus_dev, onyx);
999 static int onyx_create(struct i2c_adapter *adapter,
1000 struct device_node *node,
1003 struct i2c_board_info info;
1004 struct i2c_client *client;
1006 memset(&info, 0, sizeof(struct i2c_board_info));
1007 strlcpy(info.type, "aoa_codec_onyx", I2C_NAME_SIZE);
1009 info.platform_data = node;
1010 client = i2c_new_device(adapter, &info);
1015 * We know the driver is already loaded, so the device should be
1016 * already bound. If not it means binding failed, which suggests
1017 * the device doesn't really exist and should be deleted.
1018 * Ideally this would be replaced by better checks _before_
1019 * instantiating the device.
1021 if (!client->driver) {
1022 i2c_unregister_device(client);
1027 * Let i2c-core delete that device on driver removal.
1028 * This is safe because i2c-core holds the core_lock mutex for us.
1030 list_add_tail(&client->detected, &client->driver->clients);
1034 static int onyx_i2c_probe(struct i2c_client *client,
1035 const struct i2c_device_id *id)
1037 struct device_node *node = client->dev.platform_data;
1041 onyx = kzalloc(sizeof(struct onyx), GFP_KERNEL);
1046 mutex_init(&onyx->mutex);
1048 i2c_set_clientdata(client, onyx);
1050 /* we try to read from register ONYX_REG_CONTROL
1051 * to check if the codec is present */
1052 if (onyx_read_register(onyx, ONYX_REG_CONTROL, &dummy) != 0) {
1053 printk(KERN_ERR PFX "failed to read control register\n");
1057 strlcpy(onyx->codec.name, "onyx", MAX_CODEC_NAME_LEN);
1058 onyx->codec.owner = THIS_MODULE;
1059 onyx->codec.init = onyx_init_codec;
1060 onyx->codec.exit = onyx_exit_codec;
1061 onyx->codec.node = of_node_get(node);
1063 if (aoa_codec_register(&onyx->codec)) {
1066 printk(KERN_DEBUG PFX "created and attached onyx instance\n");
1069 i2c_set_clientdata(client, NULL);
1074 static int onyx_i2c_attach(struct i2c_adapter *adapter)
1076 struct device_node *busnode, *dev = NULL;
1077 struct pmac_i2c_bus *bus;
1079 bus = pmac_i2c_adapter_to_bus(adapter);
1082 busnode = pmac_i2c_get_bus_node(bus);
1084 while ((dev = of_get_next_child(busnode, dev)) != NULL) {
1085 if (of_device_is_compatible(dev, "pcm3052")) {
1087 printk(KERN_DEBUG PFX "found pcm3052\n");
1088 addr = of_get_property(dev, "reg", NULL);
1091 return onyx_create(adapter, dev, (*addr)>>1);
1095 /* if that didn't work, try desperate mode for older
1096 * machines that have stuff missing from the device tree */
1098 if (!of_device_is_compatible(busnode, "k2-i2c"))
1101 printk(KERN_DEBUG PFX "found k2-i2c, checking if onyx chip is on it\n");
1102 /* probe both possible addresses for the onyx chip */
1103 if (onyx_create(adapter, NULL, 0x46) == 0)
1105 return onyx_create(adapter, NULL, 0x47);
1108 static int onyx_i2c_remove(struct i2c_client *client)
1110 struct onyx *onyx = i2c_get_clientdata(client);
1112 aoa_codec_unregister(&onyx->codec);
1113 of_node_put(onyx->codec.node);
1114 if (onyx->codec_info)
1115 kfree(onyx->codec_info);
1116 i2c_set_clientdata(client, onyx);
1121 static const struct i2c_device_id onyx_i2c_id[] = {
1122 { "aoa_codec_onyx", 0 },
1126 static struct i2c_driver onyx_driver = {
1128 .name = "aoa_codec_onyx",
1129 .owner = THIS_MODULE,
1131 .attach_adapter = onyx_i2c_attach,
1132 .probe = onyx_i2c_probe,
1133 .remove = onyx_i2c_remove,
1134 .id_table = onyx_i2c_id,
1137 static int __init onyx_init(void)
1139 return i2c_add_driver(&onyx_driver);
1142 static void __exit onyx_exit(void)
1144 i2c_del_driver(&onyx_driver);
1147 module_init(onyx_init);
1148 module_exit(onyx_exit);