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 #include <linux/slab.h>
37 MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
38 MODULE_LICENSE("GPL");
39 MODULE_DESCRIPTION("pcm3052 (onyx) codec driver for snd-aoa");
43 #include "../soundbus/soundbus.h"
46 #define PFX "snd-aoa-codec-onyx: "
49 /* cache registers 65 to 80, they are write-only! */
51 struct i2c_client
*i2c
;
52 struct aoa_codec codec
;
58 struct codec_info
*codec_info
;
60 /* mutex serializes concurrent access to the device
65 #define codec_to_onyx(c) container_of(c, struct onyx, codec)
67 /* both return 0 if all ok, else on error */
68 static int onyx_read_register(struct onyx
*onyx
, u8 reg
, u8
*value
)
72 if (reg
!= ONYX_REG_CONTROL
) {
73 *value
= onyx
->cache
[reg
-FIRSTREGISTER
];
76 v
= i2c_smbus_read_byte_data(onyx
->i2c
, reg
);
80 onyx
->cache
[ONYX_REG_CONTROL
-FIRSTREGISTER
] = *value
;
84 static int onyx_write_register(struct onyx
*onyx
, u8 reg
, u8 value
)
88 result
= i2c_smbus_write_byte_data(onyx
->i2c
, reg
, value
);
90 onyx
->cache
[reg
-FIRSTREGISTER
] = value
;
96 static int onyx_dev_register(struct snd_device
*dev
)
101 static struct snd_device_ops ops
= {
102 .dev_register
= onyx_dev_register
,
105 /* this is necessary because most alsa mixer programs
106 * can't properly handle the negative range */
107 #define VOLUME_RANGE_SHIFT 128
109 static int onyx_snd_vol_info(struct snd_kcontrol
*kcontrol
,
110 struct snd_ctl_elem_info
*uinfo
)
112 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
114 uinfo
->value
.integer
.min
= -128 + VOLUME_RANGE_SHIFT
;
115 uinfo
->value
.integer
.max
= -1 + VOLUME_RANGE_SHIFT
;
119 static int onyx_snd_vol_get(struct snd_kcontrol
*kcontrol
,
120 struct snd_ctl_elem_value
*ucontrol
)
122 struct onyx
*onyx
= snd_kcontrol_chip(kcontrol
);
125 mutex_lock(&onyx
->mutex
);
126 onyx_read_register(onyx
, ONYX_REG_DAC_ATTEN_LEFT
, &l
);
127 onyx_read_register(onyx
, ONYX_REG_DAC_ATTEN_RIGHT
, &r
);
128 mutex_unlock(&onyx
->mutex
);
130 ucontrol
->value
.integer
.value
[0] = l
+ VOLUME_RANGE_SHIFT
;
131 ucontrol
->value
.integer
.value
[1] = r
+ VOLUME_RANGE_SHIFT
;
136 static int onyx_snd_vol_put(struct snd_kcontrol
*kcontrol
,
137 struct snd_ctl_elem_value
*ucontrol
)
139 struct onyx
*onyx
= snd_kcontrol_chip(kcontrol
);
142 if (ucontrol
->value
.integer
.value
[0] < -128 + VOLUME_RANGE_SHIFT
||
143 ucontrol
->value
.integer
.value
[0] > -1 + VOLUME_RANGE_SHIFT
)
145 if (ucontrol
->value
.integer
.value
[1] < -128 + VOLUME_RANGE_SHIFT
||
146 ucontrol
->value
.integer
.value
[1] > -1 + VOLUME_RANGE_SHIFT
)
149 mutex_lock(&onyx
->mutex
);
150 onyx_read_register(onyx
, ONYX_REG_DAC_ATTEN_LEFT
, &l
);
151 onyx_read_register(onyx
, ONYX_REG_DAC_ATTEN_RIGHT
, &r
);
153 if (l
+ VOLUME_RANGE_SHIFT
== ucontrol
->value
.integer
.value
[0] &&
154 r
+ VOLUME_RANGE_SHIFT
== ucontrol
->value
.integer
.value
[1]) {
155 mutex_unlock(&onyx
->mutex
);
159 onyx_write_register(onyx
, ONYX_REG_DAC_ATTEN_LEFT
,
160 ucontrol
->value
.integer
.value
[0]
161 - VOLUME_RANGE_SHIFT
);
162 onyx_write_register(onyx
, ONYX_REG_DAC_ATTEN_RIGHT
,
163 ucontrol
->value
.integer
.value
[1]
164 - VOLUME_RANGE_SHIFT
);
165 mutex_unlock(&onyx
->mutex
);
170 static struct snd_kcontrol_new volume_control
= {
171 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
172 .name
= "Master Playback Volume",
173 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
174 .info
= onyx_snd_vol_info
,
175 .get
= onyx_snd_vol_get
,
176 .put
= onyx_snd_vol_put
,
179 /* like above, this is necessary because a lot
180 * of alsa mixer programs don't handle ranges
181 * that don't start at 0 properly.
182 * even alsamixer is one of them... */
183 #define INPUTGAIN_RANGE_SHIFT (-3)
185 static int onyx_snd_inputgain_info(struct snd_kcontrol
*kcontrol
,
186 struct snd_ctl_elem_info
*uinfo
)
188 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
190 uinfo
->value
.integer
.min
= 3 + INPUTGAIN_RANGE_SHIFT
;
191 uinfo
->value
.integer
.max
= 28 + INPUTGAIN_RANGE_SHIFT
;
195 static int onyx_snd_inputgain_get(struct snd_kcontrol
*kcontrol
,
196 struct snd_ctl_elem_value
*ucontrol
)
198 struct onyx
*onyx
= snd_kcontrol_chip(kcontrol
);
201 mutex_lock(&onyx
->mutex
);
202 onyx_read_register(onyx
, ONYX_REG_ADC_CONTROL
, &ig
);
203 mutex_unlock(&onyx
->mutex
);
205 ucontrol
->value
.integer
.value
[0] =
206 (ig
& ONYX_ADC_PGA_GAIN_MASK
) + INPUTGAIN_RANGE_SHIFT
;
211 static int onyx_snd_inputgain_put(struct snd_kcontrol
*kcontrol
,
212 struct snd_ctl_elem_value
*ucontrol
)
214 struct onyx
*onyx
= snd_kcontrol_chip(kcontrol
);
217 if (ucontrol
->value
.integer
.value
[0] < 3 + INPUTGAIN_RANGE_SHIFT
||
218 ucontrol
->value
.integer
.value
[0] > 28 + INPUTGAIN_RANGE_SHIFT
)
220 mutex_lock(&onyx
->mutex
);
221 onyx_read_register(onyx
, ONYX_REG_ADC_CONTROL
, &v
);
223 n
&= ~ONYX_ADC_PGA_GAIN_MASK
;
224 n
|= (ucontrol
->value
.integer
.value
[0] - INPUTGAIN_RANGE_SHIFT
)
225 & ONYX_ADC_PGA_GAIN_MASK
;
226 onyx_write_register(onyx
, ONYX_REG_ADC_CONTROL
, n
);
227 mutex_unlock(&onyx
->mutex
);
232 static struct snd_kcontrol_new inputgain_control
= {
233 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
234 .name
= "Master Capture Volume",
235 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
236 .info
= onyx_snd_inputgain_info
,
237 .get
= onyx_snd_inputgain_get
,
238 .put
= onyx_snd_inputgain_put
,
241 static int onyx_snd_capture_source_info(struct snd_kcontrol
*kcontrol
,
242 struct snd_ctl_elem_info
*uinfo
)
244 static const char * const texts
[] = { "Line-In", "Microphone" };
246 return snd_ctl_enum_info(uinfo
, 1, 2, texts
);
249 static int onyx_snd_capture_source_get(struct snd_kcontrol
*kcontrol
,
250 struct snd_ctl_elem_value
*ucontrol
)
252 struct onyx
*onyx
= snd_kcontrol_chip(kcontrol
);
255 mutex_lock(&onyx
->mutex
);
256 onyx_read_register(onyx
, ONYX_REG_ADC_CONTROL
, &v
);
257 mutex_unlock(&onyx
->mutex
);
259 ucontrol
->value
.enumerated
.item
[0] = !!(v
&ONYX_ADC_INPUT_MIC
);
264 static void onyx_set_capture_source(struct onyx
*onyx
, int mic
)
268 mutex_lock(&onyx
->mutex
);
269 onyx_read_register(onyx
, ONYX_REG_ADC_CONTROL
, &v
);
270 v
&= ~ONYX_ADC_INPUT_MIC
;
272 v
|= ONYX_ADC_INPUT_MIC
;
273 onyx_write_register(onyx
, ONYX_REG_ADC_CONTROL
, v
);
274 mutex_unlock(&onyx
->mutex
);
277 static int onyx_snd_capture_source_put(struct snd_kcontrol
*kcontrol
,
278 struct snd_ctl_elem_value
*ucontrol
)
280 if (ucontrol
->value
.enumerated
.item
[0] > 1)
282 onyx_set_capture_source(snd_kcontrol_chip(kcontrol
),
283 ucontrol
->value
.enumerated
.item
[0]);
287 static struct snd_kcontrol_new capture_source_control
= {
288 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
289 /* If we name this 'Input Source', it properly shows up in
290 * alsamixer as a selection, * but it's shown under the
291 * 'Playback' category.
292 * If I name it 'Capture Source', it shows up in strange
293 * ways (two bools of which one can be selected at a
294 * time) but at least it's shown in the 'Capture'
296 * I was told that this was due to backward compatibility,
297 * but I don't understand then why the mangling is *not*
298 * done when I name it "Input Source".....
300 .name
= "Capture Source",
301 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
302 .info
= onyx_snd_capture_source_info
,
303 .get
= onyx_snd_capture_source_get
,
304 .put
= onyx_snd_capture_source_put
,
307 #define onyx_snd_mute_info snd_ctl_boolean_stereo_info
309 static int onyx_snd_mute_get(struct snd_kcontrol
*kcontrol
,
310 struct snd_ctl_elem_value
*ucontrol
)
312 struct onyx
*onyx
= snd_kcontrol_chip(kcontrol
);
315 mutex_lock(&onyx
->mutex
);
316 onyx_read_register(onyx
, ONYX_REG_DAC_CONTROL
, &c
);
317 mutex_unlock(&onyx
->mutex
);
319 ucontrol
->value
.integer
.value
[0] = !(c
& ONYX_MUTE_LEFT
);
320 ucontrol
->value
.integer
.value
[1] = !(c
& ONYX_MUTE_RIGHT
);
325 static int onyx_snd_mute_put(struct snd_kcontrol
*kcontrol
,
326 struct snd_ctl_elem_value
*ucontrol
)
328 struct onyx
*onyx
= snd_kcontrol_chip(kcontrol
);
332 mutex_lock(&onyx
->mutex
);
333 if (onyx
->analog_locked
)
336 onyx_read_register(onyx
, ONYX_REG_DAC_CONTROL
, &v
);
338 c
&= ~(ONYX_MUTE_RIGHT
| ONYX_MUTE_LEFT
);
339 if (!ucontrol
->value
.integer
.value
[0])
341 if (!ucontrol
->value
.integer
.value
[1])
342 c
|= ONYX_MUTE_RIGHT
;
343 err
= onyx_write_register(onyx
, ONYX_REG_DAC_CONTROL
, c
);
346 mutex_unlock(&onyx
->mutex
);
348 return !err
? (v
!= c
) : err
;
351 static struct snd_kcontrol_new mute_control
= {
352 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
353 .name
= "Master Playback Switch",
354 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
355 .info
= onyx_snd_mute_info
,
356 .get
= onyx_snd_mute_get
,
357 .put
= onyx_snd_mute_put
,
361 #define onyx_snd_single_bit_info snd_ctl_boolean_mono_info
363 #define FLAG_POLARITY_INVERT 1
364 #define FLAG_SPDIFLOCK 2
366 static int onyx_snd_single_bit_get(struct snd_kcontrol
*kcontrol
,
367 struct snd_ctl_elem_value
*ucontrol
)
369 struct onyx
*onyx
= snd_kcontrol_chip(kcontrol
);
371 long int pv
= kcontrol
->private_value
;
372 u8 polarity
= (pv
>> 16) & FLAG_POLARITY_INVERT
;
373 u8 address
= (pv
>> 8) & 0xff;
376 mutex_lock(&onyx
->mutex
);
377 onyx_read_register(onyx
, address
, &c
);
378 mutex_unlock(&onyx
->mutex
);
380 ucontrol
->value
.integer
.value
[0] = !!(c
& mask
) ^ polarity
;
385 static int onyx_snd_single_bit_put(struct snd_kcontrol
*kcontrol
,
386 struct snd_ctl_elem_value
*ucontrol
)
388 struct onyx
*onyx
= snd_kcontrol_chip(kcontrol
);
391 long int pv
= kcontrol
->private_value
;
392 u8 polarity
= (pv
>> 16) & FLAG_POLARITY_INVERT
;
393 u8 spdiflock
= (pv
>> 16) & FLAG_SPDIFLOCK
;
394 u8 address
= (pv
>> 8) & 0xff;
397 mutex_lock(&onyx
->mutex
);
398 if (spdiflock
&& onyx
->spdif_locked
) {
399 /* even if alsamixer doesn't care.. */
403 onyx_read_register(onyx
, address
, &v
);
406 if (!!ucontrol
->value
.integer
.value
[0] ^ polarity
)
408 err
= onyx_write_register(onyx
, address
, c
);
411 mutex_unlock(&onyx
->mutex
);
413 return !err
? (v
!= c
) : err
;
416 #define SINGLE_BIT(n, type, description, address, mask, flags) \
417 static struct snd_kcontrol_new n##_control = { \
418 .iface = SNDRV_CTL_ELEM_IFACE_##type, \
419 .name = description, \
420 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, \
421 .info = onyx_snd_single_bit_info, \
422 .get = onyx_snd_single_bit_get, \
423 .put = onyx_snd_single_bit_put, \
424 .private_value = (flags << 16) | (address << 8) | mask \
429 SNDRV_CTL_NAME_IEC958("", PLAYBACK
, SWITCH
),
436 ONYX_REG_DAC_CONTROL
,
441 "Fast Digital Filter Rolloff",
444 FLAG_POLARITY_INVERT
);
448 ONYX_REG_ADC_HPF_BYPASS
,
450 FLAG_POLARITY_INVERT
);
453 "Digital De-Emphasis",
458 static int onyx_spdif_info(struct snd_kcontrol
*kcontrol
,
459 struct snd_ctl_elem_info
*uinfo
)
461 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_IEC958
;
466 static int onyx_spdif_mask_get(struct snd_kcontrol
*kcontrol
,
467 struct snd_ctl_elem_value
*ucontrol
)
469 /* datasheet page 30, all others are 0 */
470 ucontrol
->value
.iec958
.status
[0] = 0x3e;
471 ucontrol
->value
.iec958
.status
[1] = 0xff;
473 ucontrol
->value
.iec958
.status
[3] = 0x3f;
474 ucontrol
->value
.iec958
.status
[4] = 0x0f;
479 static struct snd_kcontrol_new onyx_spdif_mask
= {
480 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
481 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
482 .name
= SNDRV_CTL_NAME_IEC958("",PLAYBACK
,CON_MASK
),
483 .info
= onyx_spdif_info
,
484 .get
= onyx_spdif_mask_get
,
487 static int onyx_spdif_get(struct snd_kcontrol
*kcontrol
,
488 struct snd_ctl_elem_value
*ucontrol
)
490 struct onyx
*onyx
= snd_kcontrol_chip(kcontrol
);
493 mutex_lock(&onyx
->mutex
);
494 onyx_read_register(onyx
, ONYX_REG_DIG_INFO1
, &v
);
495 ucontrol
->value
.iec958
.status
[0] = v
& 0x3e;
497 onyx_read_register(onyx
, ONYX_REG_DIG_INFO2
, &v
);
498 ucontrol
->value
.iec958
.status
[1] = v
;
500 onyx_read_register(onyx
, ONYX_REG_DIG_INFO3
, &v
);
501 ucontrol
->value
.iec958
.status
[3] = v
& 0x3f;
503 onyx_read_register(onyx
, ONYX_REG_DIG_INFO4
, &v
);
504 ucontrol
->value
.iec958
.status
[4] = v
& 0x0f;
505 mutex_unlock(&onyx
->mutex
);
510 static int onyx_spdif_put(struct snd_kcontrol
*kcontrol
,
511 struct snd_ctl_elem_value
*ucontrol
)
513 struct onyx
*onyx
= snd_kcontrol_chip(kcontrol
);
516 mutex_lock(&onyx
->mutex
);
517 onyx_read_register(onyx
, ONYX_REG_DIG_INFO1
, &v
);
518 v
= (v
& ~0x3e) | (ucontrol
->value
.iec958
.status
[0] & 0x3e);
519 onyx_write_register(onyx
, ONYX_REG_DIG_INFO1
, v
);
521 v
= ucontrol
->value
.iec958
.status
[1];
522 onyx_write_register(onyx
, ONYX_REG_DIG_INFO2
, v
);
524 onyx_read_register(onyx
, ONYX_REG_DIG_INFO3
, &v
);
525 v
= (v
& ~0x3f) | (ucontrol
->value
.iec958
.status
[3] & 0x3f);
526 onyx_write_register(onyx
, ONYX_REG_DIG_INFO3
, v
);
528 onyx_read_register(onyx
, ONYX_REG_DIG_INFO4
, &v
);
529 v
= (v
& ~0x0f) | (ucontrol
->value
.iec958
.status
[4] & 0x0f);
530 onyx_write_register(onyx
, ONYX_REG_DIG_INFO4
, v
);
531 mutex_unlock(&onyx
->mutex
);
536 static struct snd_kcontrol_new onyx_spdif_ctrl
= {
537 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
538 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
539 .name
= SNDRV_CTL_NAME_IEC958("",PLAYBACK
,DEFAULT
),
540 .info
= onyx_spdif_info
,
541 .get
= onyx_spdif_get
,
542 .put
= onyx_spdif_put
,
547 static u8 register_map
[] = {
548 ONYX_REG_DAC_ATTEN_LEFT
,
549 ONYX_REG_DAC_ATTEN_RIGHT
,
551 ONYX_REG_DAC_CONTROL
,
554 ONYX_REG_DAC_OUTPHASE
,
555 ONYX_REG_ADC_CONTROL
,
556 ONYX_REG_ADC_HPF_BYPASS
,
563 static u8 initial_values
[ARRAY_SIZE(register_map
)] = {
564 0x80, 0x80, /* muted */
565 ONYX_MRST
| ONYX_SRST
, /* but handled specially! */
566 ONYX_MUTE_LEFT
| ONYX_MUTE_RIGHT
,
567 0, /* no deemphasis */
568 ONYX_DAC_FILTER_ALWAYS
,
569 ONYX_OUTPHASE_INVERTED
,
570 (-1 /*dB*/ + 8) & 0xF, /* line in selected, -1 dB gain*/
572 (1<<2), /* pcm audio */
573 2, /* category: pcm coder */
574 0, /* sampling frequency 44.1 kHz, clock accuracy level II */
578 /* reset registers of chip, either to initial or to previous values */
579 static int onyx_register_init(struct onyx
*onyx
)
583 u8 regs
[sizeof(initial_values
)];
585 if (!onyx
->initialised
) {
586 memcpy(regs
, initial_values
, sizeof(initial_values
));
587 if (onyx_read_register(onyx
, ONYX_REG_CONTROL
, &val
))
589 val
&= ~ONYX_SILICONVERSION
;
590 val
|= initial_values
[3];
593 for (i
=0; i
<sizeof(register_map
); i
++)
594 regs
[i
] = onyx
->cache
[register_map
[i
]-FIRSTREGISTER
];
597 for (i
=0; i
<sizeof(register_map
); i
++) {
598 if (onyx_write_register(onyx
, register_map
[i
], regs
[i
]))
601 onyx
->initialised
= 1;
605 static struct transfer_info onyx_transfers
[] = {
606 /* this is first so we can skip it if no input is present...
607 * No hardware exists with that, but it's here as an example
608 * of what to do :) */
611 .formats
= SNDRV_PCM_FMTBIT_S8
|
612 SNDRV_PCM_FMTBIT_S16_BE
|
613 SNDRV_PCM_FMTBIT_S24_BE
,
614 .rates
= SNDRV_PCM_RATE_8000_96000
,
616 .must_be_clock_source
= 0,
620 /* if analog and digital are currently off, anything should go,
621 * so this entry describes everything we can do... */
622 .formats
= SNDRV_PCM_FMTBIT_S8
|
623 SNDRV_PCM_FMTBIT_S16_BE
|
624 SNDRV_PCM_FMTBIT_S24_BE
625 #ifdef SNDRV_PCM_FMTBIT_COMPRESSED_16BE
626 | SNDRV_PCM_FMTBIT_COMPRESSED_16BE
629 .rates
= SNDRV_PCM_RATE_8000_96000
,
634 .formats
= SNDRV_PCM_FMTBIT_S8
|
635 SNDRV_PCM_FMTBIT_S16_BE
|
636 SNDRV_PCM_FMTBIT_S24_BE
,
637 .rates
= SNDRV_PCM_RATE_8000_96000
,
639 .must_be_clock_source
= 0,
643 /* digital pcm output, also possible for analog out */
644 .formats
= SNDRV_PCM_FMTBIT_S8
|
645 SNDRV_PCM_FMTBIT_S16_BE
|
646 SNDRV_PCM_FMTBIT_S24_BE
,
647 .rates
= SNDRV_PCM_RATE_32000
|
648 SNDRV_PCM_RATE_44100
|
649 SNDRV_PCM_RATE_48000
,
651 .must_be_clock_source
= 0,
654 #ifdef SNDRV_PCM_FMTBIT_COMPRESSED_16BE
655 /* Once alsa gets supports for this kind of thing we can add it... */
657 /* digital compressed output */
658 .formats
= SNDRV_PCM_FMTBIT_COMPRESSED_16BE
,
659 .rates
= SNDRV_PCM_RATE_32000
|
660 SNDRV_PCM_RATE_44100
|
661 SNDRV_PCM_RATE_48000
,
668 static int onyx_usable(struct codec_info_item
*cii
,
669 struct transfer_info
*ti
,
670 struct transfer_info
*out
)
673 struct onyx
*onyx
= cii
->codec_data
;
674 int spdif_enabled
, analog_enabled
;
676 mutex_lock(&onyx
->mutex
);
677 onyx_read_register(onyx
, ONYX_REG_DIG_INFO4
, &v
);
678 spdif_enabled
= !!(v
& ONYX_SPDIF_ENABLE
);
679 onyx_read_register(onyx
, ONYX_REG_DAC_CONTROL
, &v
);
681 (v
& (ONYX_MUTE_RIGHT
|ONYX_MUTE_LEFT
))
682 != (ONYX_MUTE_RIGHT
|ONYX_MUTE_LEFT
);
683 mutex_unlock(&onyx
->mutex
);
687 case 1: return analog_enabled
;
688 case 2: return spdif_enabled
;
693 static int onyx_prepare(struct codec_info_item
*cii
,
695 struct snd_pcm_substream
*substream
)
698 struct onyx
*onyx
= cii
->codec_data
;
701 mutex_lock(&onyx
->mutex
);
703 #ifdef SNDRV_PCM_FMTBIT_COMPRESSED_16BE
704 if (substream
->runtime
->format
== SNDRV_PCM_FMTBIT_COMPRESSED_16BE
) {
705 /* mute and lock analog output */
706 onyx_read_register(onyx
, ONYX_REG_DAC_CONTROL
, &v
);
707 if (onyx_write_register(onyx
,
708 ONYX_REG_DAC_CONTROL
,
709 v
| ONYX_MUTE_RIGHT
| ONYX_MUTE_LEFT
))
711 onyx
->analog_locked
= 1;
716 switch (substream
->runtime
->rate
) {
720 /* these rates are ok for all outputs */
721 /* FIXME: program spdif channel control bits here so that
722 * userspace doesn't have to if it only plays pcm! */
726 /* got some rate that the digital output can't do,
727 * so disable and lock it */
728 onyx_read_register(cii
->codec_data
, ONYX_REG_DIG_INFO4
, &v
);
729 if (onyx_write_register(onyx
,
731 v
& ~ONYX_SPDIF_ENABLE
))
733 onyx
->spdif_locked
= 1;
739 mutex_unlock(&onyx
->mutex
);
744 static int onyx_open(struct codec_info_item
*cii
,
745 struct snd_pcm_substream
*substream
)
747 struct onyx
*onyx
= cii
->codec_data
;
749 mutex_lock(&onyx
->mutex
);
751 mutex_unlock(&onyx
->mutex
);
756 static int onyx_close(struct codec_info_item
*cii
,
757 struct snd_pcm_substream
*substream
)
759 struct onyx
*onyx
= cii
->codec_data
;
761 mutex_lock(&onyx
->mutex
);
763 if (!onyx
->open_count
)
764 onyx
->spdif_locked
= onyx
->analog_locked
= 0;
765 mutex_unlock(&onyx
->mutex
);
770 static int onyx_switch_clock(struct codec_info_item
*cii
,
771 enum clock_switch what
)
773 struct onyx
*onyx
= cii
->codec_data
;
775 mutex_lock(&onyx
->mutex
);
776 /* this *MUST* be more elaborate later... */
778 case CLOCK_SWITCH_PREPARE_SLAVE
:
779 onyx
->codec
.gpio
->methods
->all_amps_off(onyx
->codec
.gpio
);
781 case CLOCK_SWITCH_SLAVE
:
782 onyx
->codec
.gpio
->methods
->all_amps_restore(onyx
->codec
.gpio
);
784 default: /* silence warning */
787 mutex_unlock(&onyx
->mutex
);
794 static int onyx_suspend(struct codec_info_item
*cii
, pm_message_t state
)
796 struct onyx
*onyx
= cii
->codec_data
;
800 mutex_lock(&onyx
->mutex
);
801 if (onyx_read_register(onyx
, ONYX_REG_CONTROL
, &v
))
803 onyx_write_register(onyx
, ONYX_REG_CONTROL
, v
| ONYX_ADPSV
| ONYX_DAPSV
);
804 /* Apple does a sleep here but the datasheet says to do it on resume */
807 mutex_unlock(&onyx
->mutex
);
812 static int onyx_resume(struct codec_info_item
*cii
)
814 struct onyx
*onyx
= cii
->codec_data
;
818 mutex_lock(&onyx
->mutex
);
821 onyx
->codec
.gpio
->methods
->set_hw_reset(onyx
->codec
.gpio
, 0);
823 onyx
->codec
.gpio
->methods
->set_hw_reset(onyx
->codec
.gpio
, 1);
825 onyx
->codec
.gpio
->methods
->set_hw_reset(onyx
->codec
.gpio
, 0);
828 /* take codec out of suspend (if it still is after reset) */
829 if (onyx_read_register(onyx
, ONYX_REG_CONTROL
, &v
))
831 onyx_write_register(onyx
, ONYX_REG_CONTROL
, v
& ~(ONYX_ADPSV
| ONYX_DAPSV
));
832 /* FIXME: should divide by sample rate, but 8k is the lowest we go */
833 msleep(2205000/8000);
834 /* reset all values */
835 onyx_register_init(onyx
);
838 mutex_unlock(&onyx
->mutex
);
843 #endif /* CONFIG_PM */
845 static struct codec_info onyx_codec_info
= {
846 .transfers
= onyx_transfers
,
847 .sysclock_factor
= 256,
849 .owner
= THIS_MODULE
,
850 .usable
= onyx_usable
,
851 .prepare
= onyx_prepare
,
854 .switch_clock
= onyx_switch_clock
,
856 .suspend
= onyx_suspend
,
857 .resume
= onyx_resume
,
861 static int onyx_init_codec(struct aoa_codec
*codec
)
863 struct onyx
*onyx
= codec_to_onyx(codec
);
864 struct snd_kcontrol
*ctl
;
865 struct codec_info
*ci
= &onyx_codec_info
;
869 if (!onyx
->codec
.gpio
|| !onyx
->codec
.gpio
->methods
) {
870 printk(KERN_ERR PFX
"gpios not assigned!!\n");
874 onyx
->codec
.gpio
->methods
->set_hw_reset(onyx
->codec
.gpio
, 0);
876 onyx
->codec
.gpio
->methods
->set_hw_reset(onyx
->codec
.gpio
, 1);
878 onyx
->codec
.gpio
->methods
->set_hw_reset(onyx
->codec
.gpio
, 0);
881 if (onyx_register_init(onyx
)) {
882 printk(KERN_ERR PFX
"failed to initialise onyx registers\n");
886 if (aoa_snd_device_new(SNDRV_DEV_CODEC
, onyx
, &ops
)) {
887 printk(KERN_ERR PFX
"failed to create onyx snd device!\n");
891 /* nothing connected? what a joke! */
892 if ((onyx
->codec
.connected
& 0xF) == 0)
895 /* if no inputs are present... */
896 if ((onyx
->codec
.connected
& 0xC) == 0) {
897 if (!onyx
->codec_info
)
898 onyx
->codec_info
= kmalloc(sizeof(struct codec_info
), GFP_KERNEL
);
899 if (!onyx
->codec_info
)
901 ci
= onyx
->codec_info
;
902 *ci
= onyx_codec_info
;
906 /* if no outputs are present... */
907 if ((onyx
->codec
.connected
& 3) == 0) {
908 if (!onyx
->codec_info
)
909 onyx
->codec_info
= kmalloc(sizeof(struct codec_info
), GFP_KERNEL
);
910 if (!onyx
->codec_info
)
912 ci
= onyx
->codec_info
;
913 /* this is fine as there have to be inputs
914 * if we end up in this part of the code */
915 *ci
= onyx_codec_info
;
916 ci
->transfers
[1].formats
= 0;
919 if (onyx
->codec
.soundbus_dev
->attach_codec(onyx
->codec
.soundbus_dev
,
922 printk(KERN_ERR PFX
"error creating onyx pcm\n");
927 ctl = snd_ctl_new1(&n, onyx); \
930 onyx->codec.soundbus_dev->pcm->device; \
931 err = aoa_snd_ctl_add(ctl); \
937 if (onyx
->codec
.soundbus_dev
->pcm
) {
938 /* give the user appropriate controls
939 * depending on what inputs are connected */
940 if ((onyx
->codec
.connected
& 0xC) == 0xC)
941 ADDCTL(capture_source_control
);
942 else if (onyx
->codec
.connected
& 4)
943 onyx_set_capture_source(onyx
, 0);
945 onyx_set_capture_source(onyx
, 1);
946 if (onyx
->codec
.connected
& 0xC)
947 ADDCTL(inputgain_control
);
949 /* depending on what output is connected,
950 * give the user appropriate controls */
951 if (onyx
->codec
.connected
& 1) {
952 ADDCTL(volume_control
);
953 ADDCTL(mute_control
);
954 ADDCTL(ovr1_control
);
955 ADDCTL(flt0_control
);
957 ADDCTL(dm12_control
);
958 /* spdif control defaults to off */
960 if (onyx
->codec
.connected
& 2) {
961 ADDCTL(onyx_spdif_mask
);
962 ADDCTL(onyx_spdif_ctrl
);
964 if ((onyx
->codec
.connected
& 3) == 3)
965 ADDCTL(spdif_control
);
966 /* if only S/PDIF is connected, enable it unconditionally */
967 if ((onyx
->codec
.connected
& 3) == 2) {
968 onyx_read_register(onyx
, ONYX_REG_DIG_INFO4
, &v
);
969 v
|= ONYX_SPDIF_ENABLE
;
970 onyx_write_register(onyx
, ONYX_REG_DIG_INFO4
, v
);
974 printk(KERN_INFO PFX
"attached to onyx codec via i2c\n");
978 onyx
->codec
.soundbus_dev
->detach_codec(onyx
->codec
.soundbus_dev
, onyx
);
979 snd_device_free(aoa_get_card(), onyx
);
983 static void onyx_exit_codec(struct aoa_codec
*codec
)
985 struct onyx
*onyx
= codec_to_onyx(codec
);
987 if (!onyx
->codec
.soundbus_dev
) {
988 printk(KERN_ERR PFX
"onyx_exit_codec called without soundbus_dev!\n");
991 onyx
->codec
.soundbus_dev
->detach_codec(onyx
->codec
.soundbus_dev
, onyx
);
994 static int onyx_i2c_probe(struct i2c_client
*client
,
995 const struct i2c_device_id
*id
)
997 struct device_node
*node
= client
->dev
.of_node
;
1001 onyx
= kzalloc(sizeof(struct onyx
), GFP_KERNEL
);
1006 mutex_init(&onyx
->mutex
);
1008 i2c_set_clientdata(client
, onyx
);
1010 /* we try to read from register ONYX_REG_CONTROL
1011 * to check if the codec is present */
1012 if (onyx_read_register(onyx
, ONYX_REG_CONTROL
, &dummy
) != 0) {
1013 printk(KERN_ERR PFX
"failed to read control register\n");
1017 strlcpy(onyx
->codec
.name
, "onyx", MAX_CODEC_NAME_LEN
);
1018 onyx
->codec
.owner
= THIS_MODULE
;
1019 onyx
->codec
.init
= onyx_init_codec
;
1020 onyx
->codec
.exit
= onyx_exit_codec
;
1021 onyx
->codec
.node
= of_node_get(node
);
1023 if (aoa_codec_register(&onyx
->codec
)) {
1026 printk(KERN_DEBUG PFX
"created and attached onyx instance\n");
1033 static int onyx_i2c_remove(struct i2c_client
*client
)
1035 struct onyx
*onyx
= i2c_get_clientdata(client
);
1037 aoa_codec_unregister(&onyx
->codec
);
1038 of_node_put(onyx
->codec
.node
);
1039 kfree(onyx
->codec_info
);
1044 static const struct i2c_device_id onyx_i2c_id
[] = {
1045 { "MAC,pcm3052", 0 },
1048 MODULE_DEVICE_TABLE(i2c
,onyx_i2c_id
);
1050 static struct i2c_driver onyx_driver
= {
1052 .name
= "aoa_codec_onyx",
1054 .probe
= onyx_i2c_probe
,
1055 .remove
= onyx_i2c_remove
,
1056 .id_table
= onyx_i2c_id
,
1059 module_i2c_driver(onyx_driver
);