[MIPS] Use ARRAY_SIZE macro when appropriate
[sfrench/cifs-2.6.git] / sound / aoa / codecs / snd-aoa-codec-onyx.c
1 /*
2  * Apple Onboard Audio driver for Onyx codec
3  *
4  * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
5  *
6  * GPL v2, can be found in COPYING.
7  *
8  *
9  * This is a driver for the pcm3052 codec chip (codenamed Onyx)
10  * that is present in newer Apple hardware (with digital output).
11  *
12  * The Onyx codec has the following connections (listed by the bit
13  * to be used in aoa_codec.connected):
14  *  0: analog output
15  *  1: digital output
16  *  2: line input
17  *  3: microphone input
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.
22  *
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...
32  *
33  */
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");
39
40 #include "snd-aoa-codec-onyx.h"
41 #include "../aoa.h"
42 #include "../soundbus/soundbus.h"
43
44
45 #define PFX "snd-aoa-codec-onyx: "
46
47 struct onyx {
48         /* cache registers 65 to 80, they are write-only! */
49         u8                      cache[16];
50         struct i2c_client       i2c;
51         struct aoa_codec        codec;
52         u32                     initialised:1,
53                                 spdif_locked:1,
54                                 analog_locked:1,
55                                 original_mute:2;
56         int                     open_count;
57         struct codec_info       *codec_info;
58
59         /* mutex serializes concurrent access to the device
60          * and this structure.
61          */
62         struct mutex mutex;
63 };
64 #define codec_to_onyx(c) container_of(c, struct onyx, codec)
65
66 /* both return 0 if all ok, else on error */
67 static int onyx_read_register(struct onyx *onyx, u8 reg, u8 *value)
68 {
69         s32 v;
70
71         if (reg != ONYX_REG_CONTROL) {
72                 *value = onyx->cache[reg-FIRSTREGISTER];
73                 return 0;
74         }
75         v = i2c_smbus_read_byte_data(&onyx->i2c, reg);
76         if (v < 0)
77                 return -1;
78         *value = (u8)v;
79         onyx->cache[ONYX_REG_CONTROL-FIRSTREGISTER] = *value;
80         return 0;
81 }
82
83 static int onyx_write_register(struct onyx *onyx, u8 reg, u8 value)
84 {
85         int result;
86
87         result = i2c_smbus_write_byte_data(&onyx->i2c, reg, value);
88         if (!result)
89                 onyx->cache[reg-FIRSTREGISTER] = value;
90         return result;
91 }
92
93 /* alsa stuff */
94
95 static int onyx_dev_register(struct snd_device *dev)
96 {
97         return 0;
98 }
99
100 static struct snd_device_ops ops = {
101         .dev_register = onyx_dev_register,
102 };
103
104 /* this is necessary because most alsa mixer programs
105  * can't properly handle the negative range */
106 #define VOLUME_RANGE_SHIFT      128
107
108 static int onyx_snd_vol_info(struct snd_kcontrol *kcontrol,
109         struct snd_ctl_elem_info *uinfo)
110 {
111         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
112         uinfo->count = 2;
113         uinfo->value.integer.min = -128 + VOLUME_RANGE_SHIFT;
114         uinfo->value.integer.max = -1 + VOLUME_RANGE_SHIFT;
115         return 0;
116 }
117
118 static int onyx_snd_vol_get(struct snd_kcontrol *kcontrol,
119         struct snd_ctl_elem_value *ucontrol)
120 {
121         struct onyx *onyx = snd_kcontrol_chip(kcontrol);
122         s8 l, r;
123
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);
128
129         ucontrol->value.integer.value[0] = l + VOLUME_RANGE_SHIFT;
130         ucontrol->value.integer.value[1] = r + VOLUME_RANGE_SHIFT;
131
132         return 0;
133 }
134
135 static int onyx_snd_vol_put(struct snd_kcontrol *kcontrol,
136         struct snd_ctl_elem_value *ucontrol)
137 {
138         struct onyx *onyx = snd_kcontrol_chip(kcontrol);
139         s8 l, r;
140
141         mutex_lock(&onyx->mutex);
142         onyx_read_register(onyx, ONYX_REG_DAC_ATTEN_LEFT, &l);
143         onyx_read_register(onyx, ONYX_REG_DAC_ATTEN_RIGHT, &r);
144
145         if (l + VOLUME_RANGE_SHIFT == ucontrol->value.integer.value[0] &&
146             r + VOLUME_RANGE_SHIFT == ucontrol->value.integer.value[1]) {
147                 mutex_unlock(&onyx->mutex);
148                 return 0;
149         }
150
151         onyx_write_register(onyx, ONYX_REG_DAC_ATTEN_LEFT,
152                             ucontrol->value.integer.value[0]
153                              - VOLUME_RANGE_SHIFT);
154         onyx_write_register(onyx, ONYX_REG_DAC_ATTEN_RIGHT,
155                             ucontrol->value.integer.value[1]
156                              - VOLUME_RANGE_SHIFT);
157         mutex_unlock(&onyx->mutex);
158
159         return 1;
160 }
161
162 static struct snd_kcontrol_new volume_control = {
163         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
164         .name = "Master Playback Volume",
165         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
166         .info = onyx_snd_vol_info,
167         .get = onyx_snd_vol_get,
168         .put = onyx_snd_vol_put,
169 };
170
171 /* like above, this is necessary because a lot
172  * of alsa mixer programs don't handle ranges
173  * that don't start at 0 properly.
174  * even alsamixer is one of them... */
175 #define INPUTGAIN_RANGE_SHIFT   (-3)
176
177 static int onyx_snd_inputgain_info(struct snd_kcontrol *kcontrol,
178         struct snd_ctl_elem_info *uinfo)
179 {
180         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
181         uinfo->count = 1;
182         uinfo->value.integer.min = 3 + INPUTGAIN_RANGE_SHIFT;
183         uinfo->value.integer.max = 28 + INPUTGAIN_RANGE_SHIFT;
184         return 0;
185 }
186
187 static int onyx_snd_inputgain_get(struct snd_kcontrol *kcontrol,
188         struct snd_ctl_elem_value *ucontrol)
189 {
190         struct onyx *onyx = snd_kcontrol_chip(kcontrol);
191         u8 ig;
192
193         mutex_lock(&onyx->mutex);
194         onyx_read_register(onyx, ONYX_REG_ADC_CONTROL, &ig);
195         mutex_unlock(&onyx->mutex);
196
197         ucontrol->value.integer.value[0] =
198                 (ig & ONYX_ADC_PGA_GAIN_MASK) + INPUTGAIN_RANGE_SHIFT;
199
200         return 0;
201 }
202
203 static int onyx_snd_inputgain_put(struct snd_kcontrol *kcontrol,
204         struct snd_ctl_elem_value *ucontrol)
205 {
206         struct onyx *onyx = snd_kcontrol_chip(kcontrol);
207         u8 v, n;
208
209         mutex_lock(&onyx->mutex);
210         onyx_read_register(onyx, ONYX_REG_ADC_CONTROL, &v);
211         n = v;
212         n &= ~ONYX_ADC_PGA_GAIN_MASK;
213         n |= (ucontrol->value.integer.value[0] - INPUTGAIN_RANGE_SHIFT)
214                 & ONYX_ADC_PGA_GAIN_MASK;
215         onyx_write_register(onyx, ONYX_REG_ADC_CONTROL, n);
216         mutex_unlock(&onyx->mutex);
217
218         return n != v;
219 }
220
221 static struct snd_kcontrol_new inputgain_control = {
222         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
223         .name = "Master Capture Volume",
224         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
225         .info = onyx_snd_inputgain_info,
226         .get = onyx_snd_inputgain_get,
227         .put = onyx_snd_inputgain_put,
228 };
229
230 static int onyx_snd_capture_source_info(struct snd_kcontrol *kcontrol,
231         struct snd_ctl_elem_info *uinfo)
232 {
233         static char *texts[] = { "Line-In", "Microphone" };
234
235         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
236         uinfo->count = 1;
237         uinfo->value.enumerated.items = 2;
238         if (uinfo->value.enumerated.item > 1)
239                 uinfo->value.enumerated.item = 1;
240         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
241         return 0;
242 }
243
244 static int onyx_snd_capture_source_get(struct snd_kcontrol *kcontrol,
245         struct snd_ctl_elem_value *ucontrol)
246 {
247         struct onyx *onyx = snd_kcontrol_chip(kcontrol);
248         s8 v;
249
250         mutex_lock(&onyx->mutex);
251         onyx_read_register(onyx, ONYX_REG_ADC_CONTROL, &v);
252         mutex_unlock(&onyx->mutex);
253
254         ucontrol->value.enumerated.item[0] = !!(v&ONYX_ADC_INPUT_MIC);
255
256         return 0;
257 }
258
259 static void onyx_set_capture_source(struct onyx *onyx, int mic)
260 {
261         s8 v;
262
263         mutex_lock(&onyx->mutex);
264         onyx_read_register(onyx, ONYX_REG_ADC_CONTROL, &v);
265         v &= ~ONYX_ADC_INPUT_MIC;
266         if (mic)
267                 v |= ONYX_ADC_INPUT_MIC;
268         onyx_write_register(onyx, ONYX_REG_ADC_CONTROL, v);
269         mutex_unlock(&onyx->mutex);
270 }
271
272 static int onyx_snd_capture_source_put(struct snd_kcontrol *kcontrol,
273         struct snd_ctl_elem_value *ucontrol)
274 {
275         onyx_set_capture_source(snd_kcontrol_chip(kcontrol),
276                                 ucontrol->value.enumerated.item[0]);
277         return 1;
278 }
279
280 static struct snd_kcontrol_new capture_source_control = {
281         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
282         /* If we name this 'Input Source', it properly shows up in
283          * alsamixer as a selection, * but it's shown under the 
284          * 'Playback' category.
285          * If I name it 'Capture Source', it shows up in strange
286          * ways (two bools of which one can be selected at a
287          * time) but at least it's shown in the 'Capture'
288          * category.
289          * I was told that this was due to backward compatibility,
290          * but I don't understand then why the mangling is *not*
291          * done when I name it "Input Source".....
292          */
293         .name = "Capture Source",
294         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
295         .info = onyx_snd_capture_source_info,
296         .get = onyx_snd_capture_source_get,
297         .put = onyx_snd_capture_source_put,
298 };
299
300 static int onyx_snd_mute_info(struct snd_kcontrol *kcontrol,
301         struct snd_ctl_elem_info *uinfo)
302 {
303         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
304         uinfo->count = 2;
305         uinfo->value.integer.min = 0;
306         uinfo->value.integer.max = 1;
307         return 0;
308 }
309
310 static int onyx_snd_mute_get(struct snd_kcontrol *kcontrol,
311         struct snd_ctl_elem_value *ucontrol)
312 {
313         struct onyx *onyx = snd_kcontrol_chip(kcontrol);
314         u8 c;
315
316         mutex_lock(&onyx->mutex);
317         onyx_read_register(onyx, ONYX_REG_DAC_CONTROL, &c);
318         mutex_unlock(&onyx->mutex);
319
320         ucontrol->value.integer.value[0] = !(c & ONYX_MUTE_LEFT);
321         ucontrol->value.integer.value[1] = !(c & ONYX_MUTE_RIGHT);
322
323         return 0;
324 }
325
326 static int onyx_snd_mute_put(struct snd_kcontrol *kcontrol,
327         struct snd_ctl_elem_value *ucontrol)
328 {
329         struct onyx *onyx = snd_kcontrol_chip(kcontrol);
330         u8 v = 0, c = 0;
331         int err = -EBUSY;
332
333         mutex_lock(&onyx->mutex);
334         if (onyx->analog_locked)
335                 goto out_unlock;
336
337         onyx_read_register(onyx, ONYX_REG_DAC_CONTROL, &v);
338         c = v;
339         c &= ~(ONYX_MUTE_RIGHT | ONYX_MUTE_LEFT);
340         if (!ucontrol->value.integer.value[0])
341                 c |= ONYX_MUTE_LEFT;
342         if (!ucontrol->value.integer.value[1])
343                 c |= ONYX_MUTE_RIGHT;
344         err = onyx_write_register(onyx, ONYX_REG_DAC_CONTROL, c);
345
346  out_unlock:
347         mutex_unlock(&onyx->mutex);
348
349         return !err ? (v != c) : err;
350 }
351
352 static struct snd_kcontrol_new mute_control = {
353         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
354         .name = "Master Playback Switch",
355         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
356         .info = onyx_snd_mute_info,
357         .get = onyx_snd_mute_get,
358         .put = onyx_snd_mute_put,
359 };
360
361
362 static int onyx_snd_single_bit_info(struct snd_kcontrol *kcontrol,
363         struct snd_ctl_elem_info *uinfo)
364 {
365         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
366         uinfo->count = 1;
367         uinfo->value.integer.min = 0;
368         uinfo->value.integer.max = 1;
369         return 0;
370 }
371
372 #define FLAG_POLARITY_INVERT    1
373 #define FLAG_SPDIFLOCK          2
374
375 static int onyx_snd_single_bit_get(struct snd_kcontrol *kcontrol,
376         struct snd_ctl_elem_value *ucontrol)
377 {
378         struct onyx *onyx = snd_kcontrol_chip(kcontrol);
379         u8 c;
380         long int pv = kcontrol->private_value;
381         u8 polarity = (pv >> 16) & FLAG_POLARITY_INVERT;
382         u8 address = (pv >> 8) & 0xff;
383         u8 mask = pv & 0xff;
384
385         mutex_lock(&onyx->mutex);
386         onyx_read_register(onyx, address, &c);
387         mutex_unlock(&onyx->mutex);
388
389         ucontrol->value.integer.value[0] = !!(c & mask) ^ polarity;
390
391         return 0;
392 }
393
394 static int onyx_snd_single_bit_put(struct snd_kcontrol *kcontrol,
395         struct snd_ctl_elem_value *ucontrol)
396 {
397         struct onyx *onyx = snd_kcontrol_chip(kcontrol);
398         u8 v = 0, c = 0;
399         int err;
400         long int pv = kcontrol->private_value;
401         u8 polarity = (pv >> 16) & FLAG_POLARITY_INVERT;
402         u8 spdiflock = (pv >> 16) & FLAG_SPDIFLOCK;
403         u8 address = (pv >> 8) & 0xff;
404         u8 mask = pv & 0xff;
405
406         mutex_lock(&onyx->mutex);
407         if (spdiflock && onyx->spdif_locked) {
408                 /* even if alsamixer doesn't care.. */
409                 err = -EBUSY;
410                 goto out_unlock;
411         }
412         onyx_read_register(onyx, address, &v);
413         c = v;
414         c &= ~(mask);
415         if (!!ucontrol->value.integer.value[0] ^ polarity)
416                 c |= mask;
417         err = onyx_write_register(onyx, address, c);
418
419  out_unlock:
420         mutex_unlock(&onyx->mutex);
421
422         return !err ? (v != c) : err;
423 }
424
425 #define SINGLE_BIT(n, type, description, address, mask, flags)          \
426 static struct snd_kcontrol_new n##_control = {                          \
427         .iface = SNDRV_CTL_ELEM_IFACE_##type,                           \
428         .name = description,                                            \
429         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,                      \
430         .info = onyx_snd_single_bit_info,                               \
431         .get = onyx_snd_single_bit_get,                                 \
432         .put = onyx_snd_single_bit_put,                                 \
433         .private_value = (flags << 16) | (address << 8) | mask          \
434 }
435
436 SINGLE_BIT(spdif,
437            MIXER,
438            SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
439            ONYX_REG_DIG_INFO4,
440            ONYX_SPDIF_ENABLE,
441            FLAG_SPDIFLOCK);
442 SINGLE_BIT(ovr1,
443            MIXER,
444            "Oversampling Rate",
445            ONYX_REG_DAC_CONTROL,
446            ONYX_OVR1,
447            0);
448 SINGLE_BIT(flt0,
449            MIXER,
450            "Fast Digital Filter Rolloff",
451            ONYX_REG_DAC_FILTER,
452            ONYX_ROLLOFF_FAST,
453            FLAG_POLARITY_INVERT);
454 SINGLE_BIT(hpf,
455            MIXER,
456            "Highpass Filter",
457            ONYX_REG_ADC_HPF_BYPASS,
458            ONYX_HPF_DISABLE,
459            FLAG_POLARITY_INVERT);
460 SINGLE_BIT(dm12,
461            MIXER,
462            "Digital De-Emphasis",
463            ONYX_REG_DAC_DEEMPH,
464            ONYX_DIGDEEMPH_CTRL,
465            0);
466
467 static int onyx_spdif_info(struct snd_kcontrol *kcontrol,
468                            struct snd_ctl_elem_info *uinfo)
469 {
470         uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
471         uinfo->count = 1;
472         return 0;
473 }
474
475 static int onyx_spdif_mask_get(struct snd_kcontrol *kcontrol,
476                                struct snd_ctl_elem_value *ucontrol)
477 {
478         /* datasheet page 30, all others are 0 */
479         ucontrol->value.iec958.status[0] = 0x3e;
480         ucontrol->value.iec958.status[1] = 0xff;
481
482         ucontrol->value.iec958.status[3] = 0x3f;
483         ucontrol->value.iec958.status[4] = 0x0f;
484         
485         return 0;
486 }
487
488 static struct snd_kcontrol_new onyx_spdif_mask = {
489         .access =       SNDRV_CTL_ELEM_ACCESS_READ,
490         .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
491         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
492         .info =         onyx_spdif_info,
493         .get =          onyx_spdif_mask_get,
494 };
495
496 static int onyx_spdif_get(struct snd_kcontrol *kcontrol,
497                           struct snd_ctl_elem_value *ucontrol)
498 {
499         struct onyx *onyx = snd_kcontrol_chip(kcontrol);
500         u8 v;
501
502         mutex_lock(&onyx->mutex);
503         onyx_read_register(onyx, ONYX_REG_DIG_INFO1, &v);
504         ucontrol->value.iec958.status[0] = v & 0x3e;
505
506         onyx_read_register(onyx, ONYX_REG_DIG_INFO2, &v);
507         ucontrol->value.iec958.status[1] = v;
508
509         onyx_read_register(onyx, ONYX_REG_DIG_INFO3, &v);
510         ucontrol->value.iec958.status[3] = v & 0x3f;
511
512         onyx_read_register(onyx, ONYX_REG_DIG_INFO4, &v);
513         ucontrol->value.iec958.status[4] = v & 0x0f;
514         mutex_unlock(&onyx->mutex);
515
516         return 0;
517 }
518
519 static int onyx_spdif_put(struct snd_kcontrol *kcontrol,
520                           struct snd_ctl_elem_value *ucontrol)
521 {
522         struct onyx *onyx = snd_kcontrol_chip(kcontrol);
523         u8 v;
524
525         mutex_lock(&onyx->mutex);
526         onyx_read_register(onyx, ONYX_REG_DIG_INFO1, &v);
527         v = (v & ~0x3e) | (ucontrol->value.iec958.status[0] & 0x3e);
528         onyx_write_register(onyx, ONYX_REG_DIG_INFO1, v);
529
530         v = ucontrol->value.iec958.status[1];
531         onyx_write_register(onyx, ONYX_REG_DIG_INFO2, v);
532
533         onyx_read_register(onyx, ONYX_REG_DIG_INFO3, &v);
534         v = (v & ~0x3f) | (ucontrol->value.iec958.status[3] & 0x3f);
535         onyx_write_register(onyx, ONYX_REG_DIG_INFO3, v);
536
537         onyx_read_register(onyx, ONYX_REG_DIG_INFO4, &v);
538         v = (v & ~0x0f) | (ucontrol->value.iec958.status[4] & 0x0f);
539         onyx_write_register(onyx, ONYX_REG_DIG_INFO4, v);
540         mutex_unlock(&onyx->mutex);
541
542         return 1;
543 }
544
545 static struct snd_kcontrol_new onyx_spdif_ctrl = {
546         .access =       SNDRV_CTL_ELEM_ACCESS_READWRITE,
547         .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
548         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
549         .info =         onyx_spdif_info,
550         .get =          onyx_spdif_get,
551         .put =          onyx_spdif_put,
552 };
553
554 /* our registers */
555
556 static u8 register_map[] = {
557         ONYX_REG_DAC_ATTEN_LEFT,
558         ONYX_REG_DAC_ATTEN_RIGHT,
559         ONYX_REG_CONTROL,
560         ONYX_REG_DAC_CONTROL,
561         ONYX_REG_DAC_DEEMPH,
562         ONYX_REG_DAC_FILTER,
563         ONYX_REG_DAC_OUTPHASE,
564         ONYX_REG_ADC_CONTROL,
565         ONYX_REG_ADC_HPF_BYPASS,
566         ONYX_REG_DIG_INFO1,
567         ONYX_REG_DIG_INFO2,
568         ONYX_REG_DIG_INFO3,
569         ONYX_REG_DIG_INFO4
570 };
571
572 static u8 initial_values[ARRAY_SIZE(register_map)] = {
573         0x80, 0x80, /* muted */
574         ONYX_MRST | ONYX_SRST, /* but handled specially! */
575         ONYX_MUTE_LEFT | ONYX_MUTE_RIGHT,
576         0, /* no deemphasis */
577         ONYX_DAC_FILTER_ALWAYS,
578         ONYX_OUTPHASE_INVERTED,
579         (-1 /*dB*/ + 8) & 0xF, /* line in selected, -1 dB gain*/
580         ONYX_ADC_HPF_ALWAYS,
581         (1<<2), /* pcm audio */
582         2,      /* category: pcm coder */
583         0,      /* sampling frequency 44.1 kHz, clock accuracy level II */
584         1       /* 24 bit depth */
585 };
586
587 /* reset registers of chip, either to initial or to previous values */
588 static int onyx_register_init(struct onyx *onyx)
589 {
590         int i;
591         u8 val;
592         u8 regs[sizeof(initial_values)];
593
594         if (!onyx->initialised) {
595                 memcpy(regs, initial_values, sizeof(initial_values));
596                 if (onyx_read_register(onyx, ONYX_REG_CONTROL, &val))
597                         return -1;
598                 val &= ~ONYX_SILICONVERSION;
599                 val |= initial_values[3];
600                 regs[3] = val;
601         } else {
602                 for (i=0; i<sizeof(register_map); i++)
603                         regs[i] = onyx->cache[register_map[i]-FIRSTREGISTER];
604         }
605
606         for (i=0; i<sizeof(register_map); i++) {
607                 if (onyx_write_register(onyx, register_map[i], regs[i]))
608                         return -1;
609         }
610         onyx->initialised = 1;
611         return 0;
612 }
613
614 static struct transfer_info onyx_transfers[] = {
615         /* this is first so we can skip it if no input is present...
616          * No hardware exists with that, but it's here as an example
617          * of what to do :) */
618         {
619                 /* analog input */
620                 .formats = SNDRV_PCM_FMTBIT_S8 |
621                            SNDRV_PCM_FMTBIT_S16_BE |
622                            SNDRV_PCM_FMTBIT_S24_BE,
623                 .rates = SNDRV_PCM_RATE_8000_96000,
624                 .transfer_in = 1,
625                 .must_be_clock_source = 0,
626                 .tag = 0,
627         },
628         {
629                 /* if analog and digital are currently off, anything should go,
630                  * so this entry describes everything we can do... */
631                 .formats = SNDRV_PCM_FMTBIT_S8 |
632                            SNDRV_PCM_FMTBIT_S16_BE |
633                            SNDRV_PCM_FMTBIT_S24_BE
634 #ifdef SNDRV_PCM_FMTBIT_COMPRESSED_16BE
635                            | SNDRV_PCM_FMTBIT_COMPRESSED_16BE
636 #endif
637                 ,
638                 .rates = SNDRV_PCM_RATE_8000_96000,
639                 .tag = 0,
640         },
641         {
642                 /* analog output */
643                 .formats = SNDRV_PCM_FMTBIT_S8 |
644                            SNDRV_PCM_FMTBIT_S16_BE |
645                            SNDRV_PCM_FMTBIT_S24_BE,
646                 .rates = SNDRV_PCM_RATE_8000_96000,
647                 .transfer_in = 0,
648                 .must_be_clock_source = 0,
649                 .tag = 1,
650         },
651         {
652                 /* digital pcm output, also possible for analog out */
653                 .formats = SNDRV_PCM_FMTBIT_S8 |
654                            SNDRV_PCM_FMTBIT_S16_BE |
655                            SNDRV_PCM_FMTBIT_S24_BE,
656                 .rates = SNDRV_PCM_RATE_32000 |
657                          SNDRV_PCM_RATE_44100 |
658                          SNDRV_PCM_RATE_48000,
659                 .transfer_in = 0,
660                 .must_be_clock_source = 0,
661                 .tag = 2,
662         },
663 #ifdef SNDRV_PCM_FMTBIT_COMPRESSED_16BE
664 Once alsa gets supports for this kind of thing we can add it...
665         {
666                 /* digital compressed output */
667                 .formats =  SNDRV_PCM_FMTBIT_COMPRESSED_16BE,
668                 .rates = SNDRV_PCM_RATE_32000 |
669                          SNDRV_PCM_RATE_44100 |
670                          SNDRV_PCM_RATE_48000,
671                 .tag = 2,
672         },
673 #endif
674         {}
675 };
676
677 static int onyx_usable(struct codec_info_item *cii,
678                        struct transfer_info *ti,
679                        struct transfer_info *out)
680 {
681         u8 v;
682         struct onyx *onyx = cii->codec_data;
683         int spdif_enabled, analog_enabled;
684
685         mutex_lock(&onyx->mutex);
686         onyx_read_register(onyx, ONYX_REG_DIG_INFO4, &v);
687         spdif_enabled = !!(v & ONYX_SPDIF_ENABLE);
688         onyx_read_register(onyx, ONYX_REG_DAC_CONTROL, &v);
689         analog_enabled = 
690                 (v & (ONYX_MUTE_RIGHT|ONYX_MUTE_LEFT))
691                  != (ONYX_MUTE_RIGHT|ONYX_MUTE_LEFT);
692         mutex_unlock(&onyx->mutex);
693
694         switch (ti->tag) {
695         case 0: return 1;
696         case 1: return analog_enabled;
697         case 2: return spdif_enabled;
698         }
699         return 1;
700 }
701
702 static int onyx_prepare(struct codec_info_item *cii,
703                         struct bus_info *bi,
704                         struct snd_pcm_substream *substream)
705 {
706         u8 v;
707         struct onyx *onyx = cii->codec_data;
708         int err = -EBUSY;
709
710         mutex_lock(&onyx->mutex);
711
712 #ifdef SNDRV_PCM_FMTBIT_COMPRESSED_16BE
713         if (substream->runtime->format == SNDRV_PCM_FMTBIT_COMPRESSED_16BE) {
714                 /* mute and lock analog output */
715                 onyx_read_register(onyx, ONYX_REG_DAC_CONTROL, &v);
716                 if (onyx_write_register(onyx
717                                         ONYX_REG_DAC_CONTROL,
718                                         v | ONYX_MUTE_RIGHT | ONYX_MUTE_LEFT))
719                         goto out_unlock;
720                 onyx->analog_locked = 1;
721                 err = 0;
722                 goto out_unlock;
723         }
724 #endif
725         switch (substream->runtime->rate) {
726         case 32000:
727         case 44100:
728         case 48000:
729                 /* these rates are ok for all outputs */
730                 /* FIXME: program spdif channel control bits here so that
731                  *        userspace doesn't have to if it only plays pcm! */
732                 err = 0;
733                 goto out_unlock;
734         default:
735                 /* got some rate that the digital output can't do,
736                  * so disable and lock it */
737                 onyx_read_register(cii->codec_data, ONYX_REG_DIG_INFO4, &v);
738                 if (onyx_write_register(onyx,
739                                         ONYX_REG_DIG_INFO4,
740                                         v & ~ONYX_SPDIF_ENABLE))
741                         goto out_unlock;
742                 onyx->spdif_locked = 1;
743                 err = 0;
744                 goto out_unlock;
745         }
746
747  out_unlock:
748         mutex_unlock(&onyx->mutex);
749
750         return err;
751 }
752
753 static int onyx_open(struct codec_info_item *cii,
754                      struct snd_pcm_substream *substream)
755 {
756         struct onyx *onyx = cii->codec_data;
757
758         mutex_lock(&onyx->mutex);
759         onyx->open_count++;
760         mutex_unlock(&onyx->mutex);
761
762         return 0;
763 }
764
765 static int onyx_close(struct codec_info_item *cii,
766                       struct snd_pcm_substream *substream)
767 {
768         struct onyx *onyx = cii->codec_data;
769
770         mutex_lock(&onyx->mutex);
771         onyx->open_count--;
772         if (!onyx->open_count)
773                 onyx->spdif_locked = onyx->analog_locked = 0;
774         mutex_unlock(&onyx->mutex);
775
776         return 0;
777 }
778
779 static int onyx_switch_clock(struct codec_info_item *cii,
780                              enum clock_switch what)
781 {
782         struct onyx *onyx = cii->codec_data;
783
784         mutex_lock(&onyx->mutex);
785         /* this *MUST* be more elaborate later... */
786         switch (what) {
787         case CLOCK_SWITCH_PREPARE_SLAVE:
788                 onyx->codec.gpio->methods->all_amps_off(onyx->codec.gpio);
789                 break;
790         case CLOCK_SWITCH_SLAVE:
791                 onyx->codec.gpio->methods->all_amps_restore(onyx->codec.gpio);
792                 break;
793         default: /* silence warning */
794                 break;
795         }
796         mutex_unlock(&onyx->mutex);
797
798         return 0;
799 }
800
801 #ifdef CONFIG_PM
802
803 static int onyx_suspend(struct codec_info_item *cii, pm_message_t state)
804 {
805         struct onyx *onyx = cii->codec_data;
806         u8 v;
807         int err = -ENXIO;
808
809         mutex_lock(&onyx->mutex);
810         if (onyx_read_register(onyx, ONYX_REG_CONTROL, &v))
811                 goto out_unlock;
812         onyx_write_register(onyx, ONYX_REG_CONTROL, v | ONYX_ADPSV | ONYX_DAPSV);
813         /* Apple does a sleep here but the datasheet says to do it on resume */
814         err = 0;
815  out_unlock:
816         mutex_unlock(&onyx->mutex);
817
818         return err;
819 }
820
821 static int onyx_resume(struct codec_info_item *cii)
822 {
823         struct onyx *onyx = cii->codec_data;
824         u8 v;
825         int err = -ENXIO;
826
827         mutex_lock(&onyx->mutex);
828         /* take codec out of suspend */
829         if (onyx_read_register(onyx, ONYX_REG_CONTROL, &v))
830                 goto out_unlock;
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);
836         err = 0;
837  out_unlock:
838         mutex_unlock(&onyx->mutex);
839
840         return err;
841 }
842
843 #endif /* CONFIG_PM */
844
845 static struct codec_info onyx_codec_info = {
846         .transfers = onyx_transfers,
847         .sysclock_factor = 256,
848         .bus_factor = 64,
849         .owner = THIS_MODULE,
850         .usable = onyx_usable,
851         .prepare = onyx_prepare,
852         .open = onyx_open,
853         .close = onyx_close,
854         .switch_clock = onyx_switch_clock,
855 #ifdef CONFIG_PM
856         .suspend = onyx_suspend,
857         .resume = onyx_resume,
858 #endif
859 };
860
861 static int onyx_init_codec(struct aoa_codec *codec)
862 {
863         struct onyx *onyx = codec_to_onyx(codec);
864         struct snd_kcontrol *ctl;
865         struct codec_info *ci = &onyx_codec_info;
866         u8 v;
867         int err;
868
869         if (!onyx->codec.gpio || !onyx->codec.gpio->methods) {
870                 printk(KERN_ERR PFX "gpios not assigned!!\n");
871                 return -EINVAL;
872         }
873
874         onyx->codec.gpio->methods->set_hw_reset(onyx->codec.gpio, 0);
875         msleep(1);
876         onyx->codec.gpio->methods->set_hw_reset(onyx->codec.gpio, 1);
877         msleep(1);
878         onyx->codec.gpio->methods->set_hw_reset(onyx->codec.gpio, 0);
879         msleep(1);
880         
881         if (onyx_register_init(onyx)) {
882                 printk(KERN_ERR PFX "failed to initialise onyx registers\n");
883                 return -ENODEV;
884         }
885
886         if (aoa_snd_device_new(SNDRV_DEV_LOWLEVEL, onyx, &ops)) {
887                 printk(KERN_ERR PFX "failed to create onyx snd device!\n");
888                 return -ENODEV;
889         }
890
891         /* nothing connected? what a joke! */
892         if ((onyx->codec.connected & 0xF) == 0)
893                 return -ENOTCONN;
894
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)
900                         return -ENOMEM;
901                 ci = onyx->codec_info;
902                 *ci = onyx_codec_info;
903                 ci->transfers++;
904         }
905
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)
911                         return -ENOMEM;
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;
917         }
918
919         if (onyx->codec.soundbus_dev->attach_codec(onyx->codec.soundbus_dev,
920                                                    aoa_get_card(),
921                                                    ci, onyx)) {
922                 printk(KERN_ERR PFX "error creating onyx pcm\n");
923                 return -ENODEV;
924         }
925 #define ADDCTL(n)                                                       \
926         do {                                                            \
927                 ctl = snd_ctl_new1(&n, onyx);                           \
928                 if (ctl) {                                              \
929                         ctl->id.device =                                \
930                                 onyx->codec.soundbus_dev->pcm->device;  \
931                         err = aoa_snd_ctl_add(ctl);                     \
932                         if (err)                                        \
933                                 goto error;                             \
934                 }                                                       \
935         } while (0)
936
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);
944                 else
945                         onyx_set_capture_source(onyx, 1);
946                 if (onyx->codec.connected & 0xC)
947                         ADDCTL(inputgain_control);
948
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);
956                         ADDCTL(hpf_control);
957                         ADDCTL(dm12_control);
958                         /* spdif control defaults to off */
959                 }
960                 if (onyx->codec.connected & 2) {
961                         ADDCTL(onyx_spdif_mask);
962                         ADDCTL(onyx_spdif_ctrl);
963                 }
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);
971                 }
972         }
973 #undef ADDCTL
974         printk(KERN_INFO PFX "attached to onyx codec via i2c\n");
975
976         return 0;
977  error:
978         onyx->codec.soundbus_dev->detach_codec(onyx->codec.soundbus_dev, onyx);
979         snd_device_free(aoa_get_card(), onyx);
980         return err;
981 }
982
983 static void onyx_exit_codec(struct aoa_codec *codec)
984 {
985         struct onyx *onyx = codec_to_onyx(codec);
986
987         if (!onyx->codec.soundbus_dev) {
988                 printk(KERN_ERR PFX "onyx_exit_codec called without soundbus_dev!\n");
989                 return;
990         }
991         onyx->codec.soundbus_dev->detach_codec(onyx->codec.soundbus_dev, onyx);
992 }
993
994 static struct i2c_driver onyx_driver;
995
996 static int onyx_create(struct i2c_adapter *adapter,
997                        struct device_node *node,
998                        int addr)
999 {
1000         struct onyx *onyx;
1001         u8 dummy;
1002
1003         onyx = kzalloc(sizeof(struct onyx), GFP_KERNEL);
1004
1005         if (!onyx)
1006                 return -ENOMEM;
1007
1008         mutex_init(&onyx->mutex);
1009         onyx->i2c.driver = &onyx_driver;
1010         onyx->i2c.adapter = adapter;
1011         onyx->i2c.addr = addr & 0x7f;
1012         strlcpy(onyx->i2c.name, "onyx audio codec", I2C_NAME_SIZE-1);
1013
1014         if (i2c_attach_client(&onyx->i2c)) {
1015                 printk(KERN_ERR PFX "failed to attach to i2c\n");
1016                 goto fail;
1017         }
1018
1019         /* we try to read from register ONYX_REG_CONTROL
1020          * to check if the codec is present */
1021         if (onyx_read_register(onyx, ONYX_REG_CONTROL, &dummy) != 0) {
1022                 i2c_detach_client(&onyx->i2c);
1023                 printk(KERN_ERR PFX "failed to read control register\n");
1024                 goto fail;
1025         }
1026
1027         strlcpy(onyx->codec.name, "onyx", MAX_CODEC_NAME_LEN-1);
1028         onyx->codec.owner = THIS_MODULE;
1029         onyx->codec.init = onyx_init_codec;
1030         onyx->codec.exit = onyx_exit_codec;
1031         onyx->codec.node = of_node_get(node);
1032
1033         if (aoa_codec_register(&onyx->codec)) {
1034                 i2c_detach_client(&onyx->i2c);
1035                 goto fail;
1036         }
1037         printk(KERN_DEBUG PFX "created and attached onyx instance\n");
1038         return 0;
1039  fail:
1040         kfree(onyx);
1041         return -EINVAL;
1042 }
1043
1044 static int onyx_i2c_attach(struct i2c_adapter *adapter)
1045 {
1046         struct device_node *busnode, *dev = NULL;
1047         struct pmac_i2c_bus *bus;
1048
1049         bus = pmac_i2c_adapter_to_bus(adapter);
1050         if (bus == NULL)
1051                 return -ENODEV;
1052         busnode = pmac_i2c_get_bus_node(bus);
1053
1054         while ((dev = of_get_next_child(busnode, dev)) != NULL) {
1055                 if (device_is_compatible(dev, "pcm3052")) {
1056                         u32 *addr;
1057                         printk(KERN_DEBUG PFX "found pcm3052\n");
1058                         addr = (u32 *) get_property(dev, "reg", NULL);
1059                         if (!addr)
1060                                 return -ENODEV;
1061                         return onyx_create(adapter, dev, (*addr)>>1);
1062                 }
1063         }
1064
1065         /* if that didn't work, try desperate mode for older
1066          * machines that have stuff missing from the device tree */
1067         
1068         if (!device_is_compatible(busnode, "k2-i2c"))
1069                 return -ENODEV;
1070
1071         printk(KERN_DEBUG PFX "found k2-i2c, checking if onyx chip is on it\n");
1072         /* probe both possible addresses for the onyx chip */
1073         if (onyx_create(adapter, NULL, 0x46) == 0)
1074                 return 0;
1075         return onyx_create(adapter, NULL, 0x47);
1076 }
1077
1078 static int onyx_i2c_detach(struct i2c_client *client)
1079 {
1080         struct onyx *onyx = container_of(client, struct onyx, i2c);
1081         int err;
1082
1083         if ((err = i2c_detach_client(client)))
1084                 return err;
1085         aoa_codec_unregister(&onyx->codec);
1086         of_node_put(onyx->codec.node);
1087         if (onyx->codec_info)
1088                 kfree(onyx->codec_info);
1089         kfree(onyx);
1090         return 0;
1091 }
1092
1093 static struct i2c_driver onyx_driver = {
1094         .driver = {
1095                 .name = "aoa_codec_onyx",
1096                 .owner = THIS_MODULE,
1097         },
1098         .attach_adapter = onyx_i2c_attach,
1099         .detach_client = onyx_i2c_detach,
1100 };
1101
1102 static int __init onyx_init(void)
1103 {
1104         return i2c_add_driver(&onyx_driver);
1105 }
1106
1107 static void __exit onyx_exit(void)
1108 {
1109         i2c_del_driver(&onyx_driver);
1110 }
1111
1112 module_init(onyx_init);
1113 module_exit(onyx_exit);