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
);
82 onyx
->cache
[ONYX_REG_CONTROL
-FIRSTREGISTER
] = *value
;
86 static int onyx_write_register(struct onyx
*onyx
, u8 reg
, u8 value
)
90 result
= i2c_smbus_write_byte_data(onyx
->i2c
, reg
, value
);
92 onyx
->cache
[reg
-FIRSTREGISTER
] = value
;
98 static int onyx_dev_register(struct snd_device
*dev
)
103 static struct snd_device_ops ops
= {
104 .dev_register
= onyx_dev_register
,
107 /* this is necessary because most alsa mixer programs
108 * can't properly handle the negative range */
109 #define VOLUME_RANGE_SHIFT 128
111 static int onyx_snd_vol_info(struct snd_kcontrol
*kcontrol
,
112 struct snd_ctl_elem_info
*uinfo
)
114 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
116 uinfo
->value
.integer
.min
= -128 + VOLUME_RANGE_SHIFT
;
117 uinfo
->value
.integer
.max
= -1 + VOLUME_RANGE_SHIFT
;
121 static int onyx_snd_vol_get(struct snd_kcontrol
*kcontrol
,
122 struct snd_ctl_elem_value
*ucontrol
)
124 struct onyx
*onyx
= snd_kcontrol_chip(kcontrol
);
127 mutex_lock(&onyx
->mutex
);
128 onyx_read_register(onyx
, ONYX_REG_DAC_ATTEN_LEFT
, &l
);
129 onyx_read_register(onyx
, ONYX_REG_DAC_ATTEN_RIGHT
, &r
);
130 mutex_unlock(&onyx
->mutex
);
132 ucontrol
->value
.integer
.value
[0] = l
+ VOLUME_RANGE_SHIFT
;
133 ucontrol
->value
.integer
.value
[1] = r
+ VOLUME_RANGE_SHIFT
;
138 static int onyx_snd_vol_put(struct snd_kcontrol
*kcontrol
,
139 struct snd_ctl_elem_value
*ucontrol
)
141 struct onyx
*onyx
= snd_kcontrol_chip(kcontrol
);
144 if (ucontrol
->value
.integer
.value
[0] < -128 + VOLUME_RANGE_SHIFT
||
145 ucontrol
->value
.integer
.value
[0] > -1 + VOLUME_RANGE_SHIFT
)
147 if (ucontrol
->value
.integer
.value
[1] < -128 + VOLUME_RANGE_SHIFT
||
148 ucontrol
->value
.integer
.value
[1] > -1 + VOLUME_RANGE_SHIFT
)
151 mutex_lock(&onyx
->mutex
);
152 onyx_read_register(onyx
, ONYX_REG_DAC_ATTEN_LEFT
, &l
);
153 onyx_read_register(onyx
, ONYX_REG_DAC_ATTEN_RIGHT
, &r
);
155 if (l
+ VOLUME_RANGE_SHIFT
== ucontrol
->value
.integer
.value
[0] &&
156 r
+ VOLUME_RANGE_SHIFT
== ucontrol
->value
.integer
.value
[1]) {
157 mutex_unlock(&onyx
->mutex
);
161 onyx_write_register(onyx
, ONYX_REG_DAC_ATTEN_LEFT
,
162 ucontrol
->value
.integer
.value
[0]
163 - VOLUME_RANGE_SHIFT
);
164 onyx_write_register(onyx
, ONYX_REG_DAC_ATTEN_RIGHT
,
165 ucontrol
->value
.integer
.value
[1]
166 - VOLUME_RANGE_SHIFT
);
167 mutex_unlock(&onyx
->mutex
);
172 static const struct snd_kcontrol_new volume_control
= {
173 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
174 .name
= "Master Playback Volume",
175 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
176 .info
= onyx_snd_vol_info
,
177 .get
= onyx_snd_vol_get
,
178 .put
= onyx_snd_vol_put
,
181 /* like above, this is necessary because a lot
182 * of alsa mixer programs don't handle ranges
183 * that don't start at 0 properly.
184 * even alsamixer is one of them... */
185 #define INPUTGAIN_RANGE_SHIFT (-3)
187 static int onyx_snd_inputgain_info(struct snd_kcontrol
*kcontrol
,
188 struct snd_ctl_elem_info
*uinfo
)
190 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
192 uinfo
->value
.integer
.min
= 3 + INPUTGAIN_RANGE_SHIFT
;
193 uinfo
->value
.integer
.max
= 28 + INPUTGAIN_RANGE_SHIFT
;
197 static int onyx_snd_inputgain_get(struct snd_kcontrol
*kcontrol
,
198 struct snd_ctl_elem_value
*ucontrol
)
200 struct onyx
*onyx
= snd_kcontrol_chip(kcontrol
);
203 mutex_lock(&onyx
->mutex
);
204 onyx_read_register(onyx
, ONYX_REG_ADC_CONTROL
, &ig
);
205 mutex_unlock(&onyx
->mutex
);
207 ucontrol
->value
.integer
.value
[0] =
208 (ig
& ONYX_ADC_PGA_GAIN_MASK
) + INPUTGAIN_RANGE_SHIFT
;
213 static int onyx_snd_inputgain_put(struct snd_kcontrol
*kcontrol
,
214 struct snd_ctl_elem_value
*ucontrol
)
216 struct onyx
*onyx
= snd_kcontrol_chip(kcontrol
);
219 if (ucontrol
->value
.integer
.value
[0] < 3 + INPUTGAIN_RANGE_SHIFT
||
220 ucontrol
->value
.integer
.value
[0] > 28 + INPUTGAIN_RANGE_SHIFT
)
222 mutex_lock(&onyx
->mutex
);
223 onyx_read_register(onyx
, ONYX_REG_ADC_CONTROL
, &v
);
225 n
&= ~ONYX_ADC_PGA_GAIN_MASK
;
226 n
|= (ucontrol
->value
.integer
.value
[0] - INPUTGAIN_RANGE_SHIFT
)
227 & ONYX_ADC_PGA_GAIN_MASK
;
228 onyx_write_register(onyx
, ONYX_REG_ADC_CONTROL
, n
);
229 mutex_unlock(&onyx
->mutex
);
234 static const struct snd_kcontrol_new inputgain_control
= {
235 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
236 .name
= "Master Capture Volume",
237 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
238 .info
= onyx_snd_inputgain_info
,
239 .get
= onyx_snd_inputgain_get
,
240 .put
= onyx_snd_inputgain_put
,
243 static int onyx_snd_capture_source_info(struct snd_kcontrol
*kcontrol
,
244 struct snd_ctl_elem_info
*uinfo
)
246 static const char * const texts
[] = { "Line-In", "Microphone" };
248 return snd_ctl_enum_info(uinfo
, 1, 2, texts
);
251 static int onyx_snd_capture_source_get(struct snd_kcontrol
*kcontrol
,
252 struct snd_ctl_elem_value
*ucontrol
)
254 struct onyx
*onyx
= snd_kcontrol_chip(kcontrol
);
257 mutex_lock(&onyx
->mutex
);
258 onyx_read_register(onyx
, ONYX_REG_ADC_CONTROL
, &v
);
259 mutex_unlock(&onyx
->mutex
);
261 ucontrol
->value
.enumerated
.item
[0] = !!(v
&ONYX_ADC_INPUT_MIC
);
266 static void onyx_set_capture_source(struct onyx
*onyx
, int mic
)
270 mutex_lock(&onyx
->mutex
);
271 onyx_read_register(onyx
, ONYX_REG_ADC_CONTROL
, &v
);
272 v
&= ~ONYX_ADC_INPUT_MIC
;
274 v
|= ONYX_ADC_INPUT_MIC
;
275 onyx_write_register(onyx
, ONYX_REG_ADC_CONTROL
, v
);
276 mutex_unlock(&onyx
->mutex
);
279 static int onyx_snd_capture_source_put(struct snd_kcontrol
*kcontrol
,
280 struct snd_ctl_elem_value
*ucontrol
)
282 if (ucontrol
->value
.enumerated
.item
[0] > 1)
284 onyx_set_capture_source(snd_kcontrol_chip(kcontrol
),
285 ucontrol
->value
.enumerated
.item
[0]);
289 static const struct snd_kcontrol_new capture_source_control
= {
290 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
291 /* If we name this 'Input Source', it properly shows up in
292 * alsamixer as a selection, * but it's shown under the
293 * 'Playback' category.
294 * If I name it 'Capture Source', it shows up in strange
295 * ways (two bools of which one can be selected at a
296 * time) but at least it's shown in the 'Capture'
298 * I was told that this was due to backward compatibility,
299 * but I don't understand then why the mangling is *not*
300 * done when I name it "Input Source".....
302 .name
= "Capture Source",
303 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
304 .info
= onyx_snd_capture_source_info
,
305 .get
= onyx_snd_capture_source_get
,
306 .put
= onyx_snd_capture_source_put
,
309 #define onyx_snd_mute_info snd_ctl_boolean_stereo_info
311 static int onyx_snd_mute_get(struct snd_kcontrol
*kcontrol
,
312 struct snd_ctl_elem_value
*ucontrol
)
314 struct onyx
*onyx
= snd_kcontrol_chip(kcontrol
);
317 mutex_lock(&onyx
->mutex
);
318 onyx_read_register(onyx
, ONYX_REG_DAC_CONTROL
, &c
);
319 mutex_unlock(&onyx
->mutex
);
321 ucontrol
->value
.integer
.value
[0] = !(c
& ONYX_MUTE_LEFT
);
322 ucontrol
->value
.integer
.value
[1] = !(c
& ONYX_MUTE_RIGHT
);
327 static int onyx_snd_mute_put(struct snd_kcontrol
*kcontrol
,
328 struct snd_ctl_elem_value
*ucontrol
)
330 struct onyx
*onyx
= snd_kcontrol_chip(kcontrol
);
334 mutex_lock(&onyx
->mutex
);
335 if (onyx
->analog_locked
)
338 onyx_read_register(onyx
, ONYX_REG_DAC_CONTROL
, &v
);
340 c
&= ~(ONYX_MUTE_RIGHT
| ONYX_MUTE_LEFT
);
341 if (!ucontrol
->value
.integer
.value
[0])
343 if (!ucontrol
->value
.integer
.value
[1])
344 c
|= ONYX_MUTE_RIGHT
;
345 err
= onyx_write_register(onyx
, ONYX_REG_DAC_CONTROL
, c
);
348 mutex_unlock(&onyx
->mutex
);
350 return !err
? (v
!= c
) : err
;
353 static const struct snd_kcontrol_new mute_control
= {
354 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
355 .name
= "Master Playback Switch",
356 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
357 .info
= onyx_snd_mute_info
,
358 .get
= onyx_snd_mute_get
,
359 .put
= onyx_snd_mute_put
,
363 #define onyx_snd_single_bit_info snd_ctl_boolean_mono_info
365 #define FLAG_POLARITY_INVERT 1
366 #define FLAG_SPDIFLOCK 2
368 static int onyx_snd_single_bit_get(struct snd_kcontrol
*kcontrol
,
369 struct snd_ctl_elem_value
*ucontrol
)
371 struct onyx
*onyx
= snd_kcontrol_chip(kcontrol
);
373 long int pv
= kcontrol
->private_value
;
374 u8 polarity
= (pv
>> 16) & FLAG_POLARITY_INVERT
;
375 u8 address
= (pv
>> 8) & 0xff;
378 mutex_lock(&onyx
->mutex
);
379 onyx_read_register(onyx
, address
, &c
);
380 mutex_unlock(&onyx
->mutex
);
382 ucontrol
->value
.integer
.value
[0] = !!(c
& mask
) ^ polarity
;
387 static int onyx_snd_single_bit_put(struct snd_kcontrol
*kcontrol
,
388 struct snd_ctl_elem_value
*ucontrol
)
390 struct onyx
*onyx
= snd_kcontrol_chip(kcontrol
);
393 long int pv
= kcontrol
->private_value
;
394 u8 polarity
= (pv
>> 16) & FLAG_POLARITY_INVERT
;
395 u8 spdiflock
= (pv
>> 16) & FLAG_SPDIFLOCK
;
396 u8 address
= (pv
>> 8) & 0xff;
399 mutex_lock(&onyx
->mutex
);
400 if (spdiflock
&& onyx
->spdif_locked
) {
401 /* even if alsamixer doesn't care.. */
405 onyx_read_register(onyx
, address
, &v
);
408 if (!!ucontrol
->value
.integer
.value
[0] ^ polarity
)
410 err
= onyx_write_register(onyx
, address
, c
);
413 mutex_unlock(&onyx
->mutex
);
415 return !err
? (v
!= c
) : err
;
418 #define SINGLE_BIT(n, type, description, address, mask, flags) \
419 static struct snd_kcontrol_new n##_control = { \
420 .iface = SNDRV_CTL_ELEM_IFACE_##type, \
421 .name = description, \
422 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, \
423 .info = onyx_snd_single_bit_info, \
424 .get = onyx_snd_single_bit_get, \
425 .put = onyx_snd_single_bit_put, \
426 .private_value = (flags << 16) | (address << 8) | mask \
431 SNDRV_CTL_NAME_IEC958("", PLAYBACK
, SWITCH
),
438 ONYX_REG_DAC_CONTROL
,
443 "Fast Digital Filter Rolloff",
446 FLAG_POLARITY_INVERT
);
450 ONYX_REG_ADC_HPF_BYPASS
,
452 FLAG_POLARITY_INVERT
);
455 "Digital De-Emphasis",
460 static int onyx_spdif_info(struct snd_kcontrol
*kcontrol
,
461 struct snd_ctl_elem_info
*uinfo
)
463 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_IEC958
;
468 static int onyx_spdif_mask_get(struct snd_kcontrol
*kcontrol
,
469 struct snd_ctl_elem_value
*ucontrol
)
471 /* datasheet page 30, all others are 0 */
472 ucontrol
->value
.iec958
.status
[0] = 0x3e;
473 ucontrol
->value
.iec958
.status
[1] = 0xff;
475 ucontrol
->value
.iec958
.status
[3] = 0x3f;
476 ucontrol
->value
.iec958
.status
[4] = 0x0f;
481 static const struct snd_kcontrol_new onyx_spdif_mask
= {
482 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
483 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
484 .name
= SNDRV_CTL_NAME_IEC958("",PLAYBACK
,CON_MASK
),
485 .info
= onyx_spdif_info
,
486 .get
= onyx_spdif_mask_get
,
489 static int onyx_spdif_get(struct snd_kcontrol
*kcontrol
,
490 struct snd_ctl_elem_value
*ucontrol
)
492 struct onyx
*onyx
= snd_kcontrol_chip(kcontrol
);
495 mutex_lock(&onyx
->mutex
);
496 onyx_read_register(onyx
, ONYX_REG_DIG_INFO1
, &v
);
497 ucontrol
->value
.iec958
.status
[0] = v
& 0x3e;
499 onyx_read_register(onyx
, ONYX_REG_DIG_INFO2
, &v
);
500 ucontrol
->value
.iec958
.status
[1] = v
;
502 onyx_read_register(onyx
, ONYX_REG_DIG_INFO3
, &v
);
503 ucontrol
->value
.iec958
.status
[3] = v
& 0x3f;
505 onyx_read_register(onyx
, ONYX_REG_DIG_INFO4
, &v
);
506 ucontrol
->value
.iec958
.status
[4] = v
& 0x0f;
507 mutex_unlock(&onyx
->mutex
);
512 static int onyx_spdif_put(struct snd_kcontrol
*kcontrol
,
513 struct snd_ctl_elem_value
*ucontrol
)
515 struct onyx
*onyx
= snd_kcontrol_chip(kcontrol
);
518 mutex_lock(&onyx
->mutex
);
519 onyx_read_register(onyx
, ONYX_REG_DIG_INFO1
, &v
);
520 v
= (v
& ~0x3e) | (ucontrol
->value
.iec958
.status
[0] & 0x3e);
521 onyx_write_register(onyx
, ONYX_REG_DIG_INFO1
, v
);
523 v
= ucontrol
->value
.iec958
.status
[1];
524 onyx_write_register(onyx
, ONYX_REG_DIG_INFO2
, v
);
526 onyx_read_register(onyx
, ONYX_REG_DIG_INFO3
, &v
);
527 v
= (v
& ~0x3f) | (ucontrol
->value
.iec958
.status
[3] & 0x3f);
528 onyx_write_register(onyx
, ONYX_REG_DIG_INFO3
, v
);
530 onyx_read_register(onyx
, ONYX_REG_DIG_INFO4
, &v
);
531 v
= (v
& ~0x0f) | (ucontrol
->value
.iec958
.status
[4] & 0x0f);
532 onyx_write_register(onyx
, ONYX_REG_DIG_INFO4
, v
);
533 mutex_unlock(&onyx
->mutex
);
538 static const struct snd_kcontrol_new onyx_spdif_ctrl
= {
539 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
540 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
541 .name
= SNDRV_CTL_NAME_IEC958("",PLAYBACK
,DEFAULT
),
542 .info
= onyx_spdif_info
,
543 .get
= onyx_spdif_get
,
544 .put
= onyx_spdif_put
,
549 static u8 register_map
[] = {
550 ONYX_REG_DAC_ATTEN_LEFT
,
551 ONYX_REG_DAC_ATTEN_RIGHT
,
553 ONYX_REG_DAC_CONTROL
,
556 ONYX_REG_DAC_OUTPHASE
,
557 ONYX_REG_ADC_CONTROL
,
558 ONYX_REG_ADC_HPF_BYPASS
,
565 static u8 initial_values
[ARRAY_SIZE(register_map
)] = {
566 0x80, 0x80, /* muted */
567 ONYX_MRST
| ONYX_SRST
, /* but handled specially! */
568 ONYX_MUTE_LEFT
| ONYX_MUTE_RIGHT
,
569 0, /* no deemphasis */
570 ONYX_DAC_FILTER_ALWAYS
,
571 ONYX_OUTPHASE_INVERTED
,
572 (-1 /*dB*/ + 8) & 0xF, /* line in selected, -1 dB gain*/
574 (1<<2), /* pcm audio */
575 2, /* category: pcm coder */
576 0, /* sampling frequency 44.1 kHz, clock accuracy level II */
580 /* reset registers of chip, either to initial or to previous values */
581 static int onyx_register_init(struct onyx
*onyx
)
585 u8 regs
[sizeof(initial_values
)];
587 if (!onyx
->initialised
) {
588 memcpy(regs
, initial_values
, sizeof(initial_values
));
589 if (onyx_read_register(onyx
, ONYX_REG_CONTROL
, &val
))
591 val
&= ~ONYX_SILICONVERSION
;
592 val
|= initial_values
[3];
595 for (i
=0; i
<sizeof(register_map
); i
++)
596 regs
[i
] = onyx
->cache
[register_map
[i
]-FIRSTREGISTER
];
599 for (i
=0; i
<sizeof(register_map
); i
++) {
600 if (onyx_write_register(onyx
, register_map
[i
], regs
[i
]))
603 onyx
->initialised
= 1;
607 static struct transfer_info onyx_transfers
[] = {
608 /* this is first so we can skip it if no input is present...
609 * No hardware exists with that, but it's here as an example
610 * of what to do :) */
613 .formats
= SNDRV_PCM_FMTBIT_S8
|
614 SNDRV_PCM_FMTBIT_S16_BE
|
615 SNDRV_PCM_FMTBIT_S24_BE
,
616 .rates
= SNDRV_PCM_RATE_8000_96000
,
618 .must_be_clock_source
= 0,
622 /* if analog and digital are currently off, anything should go,
623 * so this entry describes everything we can do... */
624 .formats
= SNDRV_PCM_FMTBIT_S8
|
625 SNDRV_PCM_FMTBIT_S16_BE
|
626 SNDRV_PCM_FMTBIT_S24_BE
627 #ifdef SNDRV_PCM_FMTBIT_COMPRESSED_16BE
628 | SNDRV_PCM_FMTBIT_COMPRESSED_16BE
631 .rates
= SNDRV_PCM_RATE_8000_96000
,
636 .formats
= SNDRV_PCM_FMTBIT_S8
|
637 SNDRV_PCM_FMTBIT_S16_BE
|
638 SNDRV_PCM_FMTBIT_S24_BE
,
639 .rates
= SNDRV_PCM_RATE_8000_96000
,
641 .must_be_clock_source
= 0,
645 /* digital pcm output, also possible for analog out */
646 .formats
= SNDRV_PCM_FMTBIT_S8
|
647 SNDRV_PCM_FMTBIT_S16_BE
|
648 SNDRV_PCM_FMTBIT_S24_BE
,
649 .rates
= SNDRV_PCM_RATE_32000
|
650 SNDRV_PCM_RATE_44100
|
651 SNDRV_PCM_RATE_48000
,
653 .must_be_clock_source
= 0,
656 #ifdef SNDRV_PCM_FMTBIT_COMPRESSED_16BE
657 /* Once alsa gets supports for this kind of thing we can add it... */
659 /* digital compressed output */
660 .formats
= SNDRV_PCM_FMTBIT_COMPRESSED_16BE
,
661 .rates
= SNDRV_PCM_RATE_32000
|
662 SNDRV_PCM_RATE_44100
|
663 SNDRV_PCM_RATE_48000
,
670 static int onyx_usable(struct codec_info_item
*cii
,
671 struct transfer_info
*ti
,
672 struct transfer_info
*out
)
675 struct onyx
*onyx
= cii
->codec_data
;
676 int spdif_enabled
, analog_enabled
;
678 mutex_lock(&onyx
->mutex
);
679 onyx_read_register(onyx
, ONYX_REG_DIG_INFO4
, &v
);
680 spdif_enabled
= !!(v
& ONYX_SPDIF_ENABLE
);
681 onyx_read_register(onyx
, ONYX_REG_DAC_CONTROL
, &v
);
683 (v
& (ONYX_MUTE_RIGHT
|ONYX_MUTE_LEFT
))
684 != (ONYX_MUTE_RIGHT
|ONYX_MUTE_LEFT
);
685 mutex_unlock(&onyx
->mutex
);
689 case 1: return analog_enabled
;
690 case 2: return spdif_enabled
;
695 static int onyx_prepare(struct codec_info_item
*cii
,
697 struct snd_pcm_substream
*substream
)
700 struct onyx
*onyx
= cii
->codec_data
;
703 mutex_lock(&onyx
->mutex
);
705 #ifdef SNDRV_PCM_FMTBIT_COMPRESSED_16BE
706 if (substream
->runtime
->format
== SNDRV_PCM_FMTBIT_COMPRESSED_16BE
) {
707 /* mute and lock analog output */
708 onyx_read_register(onyx
, ONYX_REG_DAC_CONTROL
, &v
);
709 if (onyx_write_register(onyx
,
710 ONYX_REG_DAC_CONTROL
,
711 v
| ONYX_MUTE_RIGHT
| ONYX_MUTE_LEFT
))
713 onyx
->analog_locked
= 1;
718 switch (substream
->runtime
->rate
) {
722 /* these rates are ok for all outputs */
723 /* FIXME: program spdif channel control bits here so that
724 * userspace doesn't have to if it only plays pcm! */
728 /* got some rate that the digital output can't do,
729 * so disable and lock it */
730 onyx_read_register(cii
->codec_data
, ONYX_REG_DIG_INFO4
, &v
);
731 if (onyx_write_register(onyx
,
733 v
& ~ONYX_SPDIF_ENABLE
))
735 onyx
->spdif_locked
= 1;
741 mutex_unlock(&onyx
->mutex
);
746 static int onyx_open(struct codec_info_item
*cii
,
747 struct snd_pcm_substream
*substream
)
749 struct onyx
*onyx
= cii
->codec_data
;
751 mutex_lock(&onyx
->mutex
);
753 mutex_unlock(&onyx
->mutex
);
758 static int onyx_close(struct codec_info_item
*cii
,
759 struct snd_pcm_substream
*substream
)
761 struct onyx
*onyx
= cii
->codec_data
;
763 mutex_lock(&onyx
->mutex
);
765 if (!onyx
->open_count
)
766 onyx
->spdif_locked
= onyx
->analog_locked
= 0;
767 mutex_unlock(&onyx
->mutex
);
772 static int onyx_switch_clock(struct codec_info_item
*cii
,
773 enum clock_switch what
)
775 struct onyx
*onyx
= cii
->codec_data
;
777 mutex_lock(&onyx
->mutex
);
778 /* this *MUST* be more elaborate later... */
780 case CLOCK_SWITCH_PREPARE_SLAVE
:
781 onyx
->codec
.gpio
->methods
->all_amps_off(onyx
->codec
.gpio
);
783 case CLOCK_SWITCH_SLAVE
:
784 onyx
->codec
.gpio
->methods
->all_amps_restore(onyx
->codec
.gpio
);
786 default: /* silence warning */
789 mutex_unlock(&onyx
->mutex
);
796 static int onyx_suspend(struct codec_info_item
*cii
, pm_message_t state
)
798 struct onyx
*onyx
= cii
->codec_data
;
802 mutex_lock(&onyx
->mutex
);
803 if (onyx_read_register(onyx
, ONYX_REG_CONTROL
, &v
))
805 onyx_write_register(onyx
, ONYX_REG_CONTROL
, v
| ONYX_ADPSV
| ONYX_DAPSV
);
806 /* Apple does a sleep here but the datasheet says to do it on resume */
809 mutex_unlock(&onyx
->mutex
);
814 static int onyx_resume(struct codec_info_item
*cii
)
816 struct onyx
*onyx
= cii
->codec_data
;
820 mutex_lock(&onyx
->mutex
);
823 onyx
->codec
.gpio
->methods
->set_hw_reset(onyx
->codec
.gpio
, 0);
825 onyx
->codec
.gpio
->methods
->set_hw_reset(onyx
->codec
.gpio
, 1);
827 onyx
->codec
.gpio
->methods
->set_hw_reset(onyx
->codec
.gpio
, 0);
830 /* take codec out of suspend (if it still is after reset) */
831 if (onyx_read_register(onyx
, ONYX_REG_CONTROL
, &v
))
833 onyx_write_register(onyx
, ONYX_REG_CONTROL
, v
& ~(ONYX_ADPSV
| ONYX_DAPSV
));
834 /* FIXME: should divide by sample rate, but 8k is the lowest we go */
835 msleep(2205000/8000);
836 /* reset all values */
837 onyx_register_init(onyx
);
840 mutex_unlock(&onyx
->mutex
);
845 #endif /* CONFIG_PM */
847 static struct codec_info onyx_codec_info
= {
848 .transfers
= onyx_transfers
,
849 .sysclock_factor
= 256,
851 .owner
= THIS_MODULE
,
852 .usable
= onyx_usable
,
853 .prepare
= onyx_prepare
,
856 .switch_clock
= onyx_switch_clock
,
858 .suspend
= onyx_suspend
,
859 .resume
= onyx_resume
,
863 static int onyx_init_codec(struct aoa_codec
*codec
)
865 struct onyx
*onyx
= codec_to_onyx(codec
);
866 struct snd_kcontrol
*ctl
;
867 struct codec_info
*ci
= &onyx_codec_info
;
871 if (!onyx
->codec
.gpio
|| !onyx
->codec
.gpio
->methods
) {
872 printk(KERN_ERR PFX
"gpios not assigned!!\n");
876 onyx
->codec
.gpio
->methods
->set_hw_reset(onyx
->codec
.gpio
, 0);
878 onyx
->codec
.gpio
->methods
->set_hw_reset(onyx
->codec
.gpio
, 1);
880 onyx
->codec
.gpio
->methods
->set_hw_reset(onyx
->codec
.gpio
, 0);
883 if (onyx_register_init(onyx
)) {
884 printk(KERN_ERR PFX
"failed to initialise onyx registers\n");
888 if (aoa_snd_device_new(SNDRV_DEV_CODEC
, onyx
, &ops
)) {
889 printk(KERN_ERR PFX
"failed to create onyx snd device!\n");
893 /* nothing connected? what a joke! */
894 if ((onyx
->codec
.connected
& 0xF) == 0)
897 /* if no inputs are present... */
898 if ((onyx
->codec
.connected
& 0xC) == 0) {
899 if (!onyx
->codec_info
)
900 onyx
->codec_info
= kmalloc(sizeof(struct codec_info
), GFP_KERNEL
);
901 if (!onyx
->codec_info
)
903 ci
= onyx
->codec_info
;
904 *ci
= onyx_codec_info
;
908 /* if no outputs are present... */
909 if ((onyx
->codec
.connected
& 3) == 0) {
910 if (!onyx
->codec_info
)
911 onyx
->codec_info
= kmalloc(sizeof(struct codec_info
), GFP_KERNEL
);
912 if (!onyx
->codec_info
)
914 ci
= onyx
->codec_info
;
915 /* this is fine as there have to be inputs
916 * if we end up in this part of the code */
917 *ci
= onyx_codec_info
;
918 ci
->transfers
[1].formats
= 0;
921 if (onyx
->codec
.soundbus_dev
->attach_codec(onyx
->codec
.soundbus_dev
,
924 printk(KERN_ERR PFX
"error creating onyx pcm\n");
929 ctl = snd_ctl_new1(&n, onyx); \
932 onyx->codec.soundbus_dev->pcm->device; \
933 err = aoa_snd_ctl_add(ctl); \
939 if (onyx
->codec
.soundbus_dev
->pcm
) {
940 /* give the user appropriate controls
941 * depending on what inputs are connected */
942 if ((onyx
->codec
.connected
& 0xC) == 0xC)
943 ADDCTL(capture_source_control
);
944 else if (onyx
->codec
.connected
& 4)
945 onyx_set_capture_source(onyx
, 0);
947 onyx_set_capture_source(onyx
, 1);
948 if (onyx
->codec
.connected
& 0xC)
949 ADDCTL(inputgain_control
);
951 /* depending on what output is connected,
952 * give the user appropriate controls */
953 if (onyx
->codec
.connected
& 1) {
954 ADDCTL(volume_control
);
955 ADDCTL(mute_control
);
956 ADDCTL(ovr1_control
);
957 ADDCTL(flt0_control
);
959 ADDCTL(dm12_control
);
960 /* spdif control defaults to off */
962 if (onyx
->codec
.connected
& 2) {
963 ADDCTL(onyx_spdif_mask
);
964 ADDCTL(onyx_spdif_ctrl
);
966 if ((onyx
->codec
.connected
& 3) == 3)
967 ADDCTL(spdif_control
);
968 /* if only S/PDIF is connected, enable it unconditionally */
969 if ((onyx
->codec
.connected
& 3) == 2) {
970 onyx_read_register(onyx
, ONYX_REG_DIG_INFO4
, &v
);
971 v
|= ONYX_SPDIF_ENABLE
;
972 onyx_write_register(onyx
, ONYX_REG_DIG_INFO4
, v
);
976 printk(KERN_INFO PFX
"attached to onyx codec via i2c\n");
980 onyx
->codec
.soundbus_dev
->detach_codec(onyx
->codec
.soundbus_dev
, onyx
);
981 snd_device_free(aoa_get_card(), onyx
);
985 static void onyx_exit_codec(struct aoa_codec
*codec
)
987 struct onyx
*onyx
= codec_to_onyx(codec
);
989 if (!onyx
->codec
.soundbus_dev
) {
990 printk(KERN_ERR PFX
"onyx_exit_codec called without soundbus_dev!\n");
993 onyx
->codec
.soundbus_dev
->detach_codec(onyx
->codec
.soundbus_dev
, onyx
);
996 static int onyx_i2c_probe(struct i2c_client
*client
,
997 const struct i2c_device_id
*id
)
999 struct device_node
*node
= client
->dev
.of_node
;
1003 onyx
= kzalloc(sizeof(struct onyx
), GFP_KERNEL
);
1008 mutex_init(&onyx
->mutex
);
1010 i2c_set_clientdata(client
, onyx
);
1012 /* we try to read from register ONYX_REG_CONTROL
1013 * to check if the codec is present */
1014 if (onyx_read_register(onyx
, ONYX_REG_CONTROL
, &dummy
) != 0) {
1015 printk(KERN_ERR PFX
"failed to read control register\n");
1019 strlcpy(onyx
->codec
.name
, "onyx", MAX_CODEC_NAME_LEN
);
1020 onyx
->codec
.owner
= THIS_MODULE
;
1021 onyx
->codec
.init
= onyx_init_codec
;
1022 onyx
->codec
.exit
= onyx_exit_codec
;
1023 onyx
->codec
.node
= of_node_get(node
);
1025 if (aoa_codec_register(&onyx
->codec
)) {
1028 printk(KERN_DEBUG PFX
"created and attached onyx instance\n");
1035 static int onyx_i2c_remove(struct i2c_client
*client
)
1037 struct onyx
*onyx
= i2c_get_clientdata(client
);
1039 aoa_codec_unregister(&onyx
->codec
);
1040 of_node_put(onyx
->codec
.node
);
1041 kfree(onyx
->codec_info
);
1046 static const struct i2c_device_id onyx_i2c_id
[] = {
1047 { "MAC,pcm3052", 0 },
1050 MODULE_DEVICE_TABLE(i2c
,onyx_i2c_id
);
1052 static struct i2c_driver onyx_driver
= {
1054 .name
= "aoa_codec_onyx",
1056 .probe
= onyx_i2c_probe
,
1057 .remove
= onyx_i2c_remove
,
1058 .id_table
= onyx_i2c_id
,
1061 module_i2c_driver(onyx_driver
);