xref: /linux/sound/aoa/codecs/onyx.c (revision 9551af27f8167bbb5f862a12f7f9bc5830e8f4e1)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Apple Onboard Audio driver for Onyx codec
4  *
5  * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
6  *
7  * This is a driver for the pcm3052 codec chip (codenamed Onyx)
8  * that is present in newer Apple hardware (with digital output).
9  *
10  * The Onyx codec has the following connections (listed by the bit
11  * to be used in aoa_codec.connected):
12  *  0: analog output
13  *  1: digital output
14  *  2: line input
15  *  3: microphone input
16  * Note that even though I know of no machine that has for example
17  * the digital output connected but not the analog, I have handled
18  * all the different cases in the code so that this driver may serve
19  * as a good example of what to do.
20  *
21  * NOTE: This driver assumes that there's at most one chip to be
22  * 	 used with one alsa card, in form of creating all kinds
23  *	 of mixer elements without regard for their existence.
24  *	 But snd-aoa assumes that there's at most one card, so
25  *	 this means you can only have one onyx on a system. This
26  *	 should probably be fixed by changing the assumption of
27  *	 having just a single card on a system, and making the
28  *	 'card' pointer accessible to anyone who needs it instead
29  *	 of hiding it in the aoa_snd_* functions...
30  */
31 #include <linux/delay.h>
32 #include <linux/module.h>
33 #include <linux/of.h>
34 #include <linux/slab.h>
35 #include <sound/asoundef.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 "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 		*value = 0;
78 		return -1;
79 	}
80 	*value = (u8)v;
81 	onyx->cache[ONYX_REG_CONTROL-FIRSTREGISTER] = *value;
82 	return 0;
83 }
84 
85 static int onyx_write_register(struct onyx *onyx, u8 reg, u8 value)
86 {
87 	int result;
88 
89 	result = i2c_smbus_write_byte_data(onyx->i2c, reg, value);
90 	if (!result)
91 		onyx->cache[reg-FIRSTREGISTER] = value;
92 	return result;
93 }
94 
95 /* alsa stuff */
96 
97 static int onyx_dev_register(struct snd_device *dev)
98 {
99 	return 0;
100 }
101 
102 static const struct snd_device_ops ops = {
103 	.dev_register = onyx_dev_register,
104 };
105 
106 /* this is necessary because most alsa mixer programs
107  * can't properly handle the negative range */
108 #define VOLUME_RANGE_SHIFT	128
109 
110 static int onyx_snd_vol_info(struct snd_kcontrol *kcontrol,
111 	struct snd_ctl_elem_info *uinfo)
112 {
113 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
114 	uinfo->count = 2;
115 	uinfo->value.integer.min = -128 + VOLUME_RANGE_SHIFT;
116 	uinfo->value.integer.max = -1 + VOLUME_RANGE_SHIFT;
117 	return 0;
118 }
119 
120 static int onyx_snd_vol_get(struct snd_kcontrol *kcontrol,
121 	struct snd_ctl_elem_value *ucontrol)
122 {
123 	struct onyx *onyx = snd_kcontrol_chip(kcontrol);
124 	s8 l, r;
125 
126 	guard(mutex)(&onyx->mutex);
127 	onyx_read_register(onyx, ONYX_REG_DAC_ATTEN_LEFT, &l);
128 	onyx_read_register(onyx, ONYX_REG_DAC_ATTEN_RIGHT, &r);
129 
130 	ucontrol->value.integer.value[0] = l + VOLUME_RANGE_SHIFT;
131 	ucontrol->value.integer.value[1] = r + VOLUME_RANGE_SHIFT;
132 
133 	return 0;
134 }
135 
136 static int onyx_snd_vol_put(struct snd_kcontrol *kcontrol,
137 	struct snd_ctl_elem_value *ucontrol)
138 {
139 	struct onyx *onyx = snd_kcontrol_chip(kcontrol);
140 	s8 l, r;
141 
142 	if (ucontrol->value.integer.value[0] < -128 + VOLUME_RANGE_SHIFT ||
143 	    ucontrol->value.integer.value[0] > -1 + VOLUME_RANGE_SHIFT)
144 		return -EINVAL;
145 	if (ucontrol->value.integer.value[1] < -128 + VOLUME_RANGE_SHIFT ||
146 	    ucontrol->value.integer.value[1] > -1 + VOLUME_RANGE_SHIFT)
147 		return -EINVAL;
148 
149 	guard(mutex)(&onyx->mutex);
150 	onyx_read_register(onyx, ONYX_REG_DAC_ATTEN_LEFT, &l);
151 	onyx_read_register(onyx, ONYX_REG_DAC_ATTEN_RIGHT, &r);
152 
153 	if (l + VOLUME_RANGE_SHIFT == ucontrol->value.integer.value[0] &&
154 	    r + VOLUME_RANGE_SHIFT == ucontrol->value.integer.value[1])
155 		return 0;
156 
157 	onyx_write_register(onyx, ONYX_REG_DAC_ATTEN_LEFT,
158 			    ucontrol->value.integer.value[0]
159 			     - VOLUME_RANGE_SHIFT);
160 	onyx_write_register(onyx, ONYX_REG_DAC_ATTEN_RIGHT,
161 			    ucontrol->value.integer.value[1]
162 			     - VOLUME_RANGE_SHIFT);
163 
164 	return 1;
165 }
166 
167 static const struct snd_kcontrol_new volume_control = {
168 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
169 	.name = "Master Playback Volume",
170 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
171 	.info = onyx_snd_vol_info,
172 	.get = onyx_snd_vol_get,
173 	.put = onyx_snd_vol_put,
174 };
175 
176 /* like above, this is necessary because a lot
177  * of alsa mixer programs don't handle ranges
178  * that don't start at 0 properly.
179  * even alsamixer is one of them... */
180 #define INPUTGAIN_RANGE_SHIFT	(-3)
181 
182 static int onyx_snd_inputgain_info(struct snd_kcontrol *kcontrol,
183 	struct snd_ctl_elem_info *uinfo)
184 {
185 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
186 	uinfo->count = 1;
187 	uinfo->value.integer.min = 3 + INPUTGAIN_RANGE_SHIFT;
188 	uinfo->value.integer.max = 28 + INPUTGAIN_RANGE_SHIFT;
189 	return 0;
190 }
191 
192 static int onyx_snd_inputgain_get(struct snd_kcontrol *kcontrol,
193 	struct snd_ctl_elem_value *ucontrol)
194 {
195 	struct onyx *onyx = snd_kcontrol_chip(kcontrol);
196 	u8 ig;
197 
198 	guard(mutex)(&onyx->mutex);
199 	onyx_read_register(onyx, ONYX_REG_ADC_CONTROL, &ig);
200 
201 	ucontrol->value.integer.value[0] =
202 		(ig & ONYX_ADC_PGA_GAIN_MASK) + INPUTGAIN_RANGE_SHIFT;
203 
204 	return 0;
205 }
206 
207 static int onyx_snd_inputgain_put(struct snd_kcontrol *kcontrol,
208 	struct snd_ctl_elem_value *ucontrol)
209 {
210 	struct onyx *onyx = snd_kcontrol_chip(kcontrol);
211 	u8 v, n;
212 
213 	if (ucontrol->value.integer.value[0] < 3 + INPUTGAIN_RANGE_SHIFT ||
214 	    ucontrol->value.integer.value[0] > 28 + INPUTGAIN_RANGE_SHIFT)
215 		return -EINVAL;
216 	guard(mutex)(&onyx->mutex);
217 	onyx_read_register(onyx, ONYX_REG_ADC_CONTROL, &v);
218 	n = v;
219 	n &= ~ONYX_ADC_PGA_GAIN_MASK;
220 	n |= (ucontrol->value.integer.value[0] - INPUTGAIN_RANGE_SHIFT)
221 		& ONYX_ADC_PGA_GAIN_MASK;
222 	onyx_write_register(onyx, ONYX_REG_ADC_CONTROL, n);
223 
224 	return n != v;
225 }
226 
227 static const struct snd_kcontrol_new inputgain_control = {
228 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
229 	.name = "Master Capture Volume",
230 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
231 	.info = onyx_snd_inputgain_info,
232 	.get = onyx_snd_inputgain_get,
233 	.put = onyx_snd_inputgain_put,
234 };
235 
236 static int onyx_snd_capture_source_info(struct snd_kcontrol *kcontrol,
237 	struct snd_ctl_elem_info *uinfo)
238 {
239 	static const char * const texts[] = { "Line-In", "Microphone" };
240 
241 	return snd_ctl_enum_info(uinfo, 1, 2, texts);
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 	guard(mutex)(&onyx->mutex);
251 	onyx_read_register(onyx, ONYX_REG_ADC_CONTROL, &v);
252 
253 	ucontrol->value.enumerated.item[0] = !!(v&ONYX_ADC_INPUT_MIC);
254 
255 	return 0;
256 }
257 
258 static void onyx_set_capture_source(struct onyx *onyx, int mic)
259 {
260 	s8 v;
261 
262 	guard(mutex)(&onyx->mutex);
263 	onyx_read_register(onyx, ONYX_REG_ADC_CONTROL, &v);
264 	v &= ~ONYX_ADC_INPUT_MIC;
265 	if (mic)
266 		v |= ONYX_ADC_INPUT_MIC;
267 	onyx_write_register(onyx, ONYX_REG_ADC_CONTROL, v);
268 }
269 
270 static int onyx_snd_capture_source_put(struct snd_kcontrol *kcontrol,
271 	struct snd_ctl_elem_value *ucontrol)
272 {
273 	if (ucontrol->value.enumerated.item[0] > 1)
274 		return -EINVAL;
275 	onyx_set_capture_source(snd_kcontrol_chip(kcontrol),
276 				ucontrol->value.enumerated.item[0]);
277 	return 1;
278 }
279 
280 static const 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 #define onyx_snd_mute_info	snd_ctl_boolean_stereo_info
301 
302 static int onyx_snd_mute_get(struct snd_kcontrol *kcontrol,
303 	struct snd_ctl_elem_value *ucontrol)
304 {
305 	struct onyx *onyx = snd_kcontrol_chip(kcontrol);
306 	u8 c;
307 
308 	guard(mutex)(&onyx->mutex);
309 	onyx_read_register(onyx, ONYX_REG_DAC_CONTROL, &c);
310 
311 	ucontrol->value.integer.value[0] = !(c & ONYX_MUTE_LEFT);
312 	ucontrol->value.integer.value[1] = !(c & ONYX_MUTE_RIGHT);
313 
314 	return 0;
315 }
316 
317 static int onyx_snd_mute_put(struct snd_kcontrol *kcontrol,
318 	struct snd_ctl_elem_value *ucontrol)
319 {
320 	struct onyx *onyx = snd_kcontrol_chip(kcontrol);
321 	u8 v = 0, c = 0;
322 	int err = -EBUSY;
323 
324 	guard(mutex)(&onyx->mutex);
325 	if (onyx->analog_locked)
326 		return -EBUSY;
327 
328 	onyx_read_register(onyx, ONYX_REG_DAC_CONTROL, &v);
329 	c = v;
330 	c &= ~(ONYX_MUTE_RIGHT | ONYX_MUTE_LEFT);
331 	if (!ucontrol->value.integer.value[0])
332 		c |= ONYX_MUTE_LEFT;
333 	if (!ucontrol->value.integer.value[1])
334 		c |= ONYX_MUTE_RIGHT;
335 	err = onyx_write_register(onyx, ONYX_REG_DAC_CONTROL, c);
336 
337 	return !err ? (v != c) : err;
338 }
339 
340 static const struct snd_kcontrol_new mute_control = {
341 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
342 	.name = "Master Playback Switch",
343 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
344 	.info = onyx_snd_mute_info,
345 	.get = onyx_snd_mute_get,
346 	.put = onyx_snd_mute_put,
347 };
348 
349 
350 #define onyx_snd_single_bit_info	snd_ctl_boolean_mono_info
351 
352 #define FLAG_POLARITY_INVERT	1
353 #define FLAG_SPDIFLOCK		2
354 
355 static int onyx_snd_single_bit_get(struct snd_kcontrol *kcontrol,
356 	struct snd_ctl_elem_value *ucontrol)
357 {
358 	struct onyx *onyx = snd_kcontrol_chip(kcontrol);
359 	u8 c;
360 	long int pv = kcontrol->private_value;
361 	u8 polarity = (pv >> 16) & FLAG_POLARITY_INVERT;
362 	u8 address = (pv >> 8) & 0xff;
363 	u8 mask = pv & 0xff;
364 
365 	guard(mutex)(&onyx->mutex);
366 	onyx_read_register(onyx, address, &c);
367 
368 	ucontrol->value.integer.value[0] = !!(c & mask) ^ polarity;
369 
370 	return 0;
371 }
372 
373 static int onyx_snd_single_bit_put(struct snd_kcontrol *kcontrol,
374 	struct snd_ctl_elem_value *ucontrol)
375 {
376 	struct onyx *onyx = snd_kcontrol_chip(kcontrol);
377 	u8 v = 0, c = 0;
378 	int err;
379 	long int pv = kcontrol->private_value;
380 	u8 polarity = (pv >> 16) & FLAG_POLARITY_INVERT;
381 	u8 spdiflock = (pv >> 16) & FLAG_SPDIFLOCK;
382 	u8 address = (pv >> 8) & 0xff;
383 	u8 mask = pv & 0xff;
384 
385 	guard(mutex)(&onyx->mutex);
386 	if (spdiflock && onyx->spdif_locked) {
387 		/* even if alsamixer doesn't care.. */
388 		return -EBUSY;
389 	}
390 	onyx_read_register(onyx, address, &v);
391 	c = v;
392 	c &= ~(mask);
393 	if (!!ucontrol->value.integer.value[0] ^ polarity)
394 		c |= mask;
395 	err = onyx_write_register(onyx, address, c);
396 
397 	return !err ? (v != c) : err;
398 }
399 
400 #define SINGLE_BIT(n, type, description, address, mask, flags)	 	\
401 static const struct snd_kcontrol_new n##_control = {			\
402 	.iface = SNDRV_CTL_ELEM_IFACE_##type,				\
403 	.name = description,						\
404 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,			\
405 	.info = onyx_snd_single_bit_info,				\
406 	.get = onyx_snd_single_bit_get,					\
407 	.put = onyx_snd_single_bit_put,					\
408 	.private_value = (flags << 16) | (address << 8) | mask		\
409 }
410 
411 SINGLE_BIT(spdif,
412 	   MIXER,
413 	   SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
414 	   ONYX_REG_DIG_INFO4,
415 	   ONYX_SPDIF_ENABLE,
416 	   FLAG_SPDIFLOCK);
417 SINGLE_BIT(ovr1,
418 	   MIXER,
419 	   "Oversampling Rate",
420 	   ONYX_REG_DAC_CONTROL,
421 	   ONYX_OVR1,
422 	   0);
423 SINGLE_BIT(flt0,
424 	   MIXER,
425 	   "Fast Digital Filter Rolloff",
426 	   ONYX_REG_DAC_FILTER,
427 	   ONYX_ROLLOFF_FAST,
428 	   FLAG_POLARITY_INVERT);
429 SINGLE_BIT(hpf,
430 	   MIXER,
431 	   "Highpass Filter",
432 	   ONYX_REG_ADC_HPF_BYPASS,
433 	   ONYX_HPF_DISABLE,
434 	   FLAG_POLARITY_INVERT);
435 SINGLE_BIT(dm12,
436 	   MIXER,
437 	   "Digital De-Emphasis",
438 	   ONYX_REG_DAC_DEEMPH,
439 	   ONYX_DIGDEEMPH_CTRL,
440 	   0);
441 
442 static int onyx_spdif_info(struct snd_kcontrol *kcontrol,
443 			   struct snd_ctl_elem_info *uinfo)
444 {
445 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
446 	uinfo->count = 1;
447 	return 0;
448 }
449 
450 static int onyx_spdif_mask_get(struct snd_kcontrol *kcontrol,
451 			       struct snd_ctl_elem_value *ucontrol)
452 {
453 	/* datasheet page 30, all others are 0 */
454 	ucontrol->value.iec958.status[0] = 0x3e;
455 	ucontrol->value.iec958.status[1] = 0xff;
456 
457 	ucontrol->value.iec958.status[3] = 0x3f;
458 	ucontrol->value.iec958.status[4] = 0x0f;
459 
460 	return 0;
461 }
462 
463 static const struct snd_kcontrol_new onyx_spdif_mask = {
464 	.access =	SNDRV_CTL_ELEM_ACCESS_READ,
465 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
466 	.name =		SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
467 	.info =		onyx_spdif_info,
468 	.get =		onyx_spdif_mask_get,
469 };
470 
471 static int onyx_spdif_get(struct snd_kcontrol *kcontrol,
472 			  struct snd_ctl_elem_value *ucontrol)
473 {
474 	struct onyx *onyx = snd_kcontrol_chip(kcontrol);
475 	u8 v;
476 
477 	guard(mutex)(&onyx->mutex);
478 	onyx_read_register(onyx, ONYX_REG_DIG_INFO1, &v);
479 	ucontrol->value.iec958.status[0] = v & 0x3e;
480 
481 	onyx_read_register(onyx, ONYX_REG_DIG_INFO2, &v);
482 	ucontrol->value.iec958.status[1] = v;
483 
484 	onyx_read_register(onyx, ONYX_REG_DIG_INFO3, &v);
485 	ucontrol->value.iec958.status[3] = v & 0x3f;
486 
487 	onyx_read_register(onyx, ONYX_REG_DIG_INFO4, &v);
488 	ucontrol->value.iec958.status[4] = v & 0x0f;
489 
490 	return 0;
491 }
492 
493 static int onyx_spdif_put(struct snd_kcontrol *kcontrol,
494 			  struct snd_ctl_elem_value *ucontrol)
495 {
496 	struct onyx *onyx = snd_kcontrol_chip(kcontrol);
497 	u8 v;
498 
499 	guard(mutex)(&onyx->mutex);
500 	onyx_read_register(onyx, ONYX_REG_DIG_INFO1, &v);
501 	v = (v & ~0x3e) | (ucontrol->value.iec958.status[0] & 0x3e);
502 	onyx_write_register(onyx, ONYX_REG_DIG_INFO1, v);
503 
504 	v = ucontrol->value.iec958.status[1];
505 	onyx_write_register(onyx, ONYX_REG_DIG_INFO2, v);
506 
507 	onyx_read_register(onyx, ONYX_REG_DIG_INFO3, &v);
508 	v = (v & ~0x3f) | (ucontrol->value.iec958.status[3] & 0x3f);
509 	onyx_write_register(onyx, ONYX_REG_DIG_INFO3, v);
510 
511 	onyx_read_register(onyx, ONYX_REG_DIG_INFO4, &v);
512 	v = (v & ~0x0f) | (ucontrol->value.iec958.status[4] & 0x0f);
513 	onyx_write_register(onyx, ONYX_REG_DIG_INFO4, v);
514 
515 	return 1;
516 }
517 
518 static int onyx_set_spdif_pcm_rate(struct onyx *onyx, unsigned int rate)
519 {
520 	u8 dig_info3, fs;
521 
522 	switch (rate) {
523 	case 32000:
524 		fs = IEC958_AES3_CON_FS_32000;
525 		break;
526 	case 44100:
527 		fs = IEC958_AES3_CON_FS_44100;
528 		break;
529 	case 48000:
530 		fs = IEC958_AES3_CON_FS_48000;
531 		break;
532 	default:
533 		return -EINVAL;
534 	}
535 
536 	if (onyx_read_register(onyx, ONYX_REG_DIG_INFO3, &dig_info3))
537 		return -EBUSY;
538 	dig_info3 = (dig_info3 & ~IEC958_AES3_CON_FS) | fs;
539 	if (onyx_write_register(onyx, ONYX_REG_DIG_INFO3, dig_info3))
540 		return -EBUSY;
541 
542 	return 0;
543 }
544 
545 static const struct snd_kcontrol_new onyx_spdif_ctrl = {
546 	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE |
547 			SNDRV_CTL_ELEM_ACCESS_VOLATILE,
548 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
549 	.name =		SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
550 	.info =		onyx_spdif_info,
551 	.get =		onyx_spdif_get,
552 	.put =		onyx_spdif_put,
553 };
554 
555 /* our registers */
556 
557 static const u8 register_map[] = {
558 	ONYX_REG_DAC_ATTEN_LEFT,
559 	ONYX_REG_DAC_ATTEN_RIGHT,
560 	ONYX_REG_CONTROL,
561 	ONYX_REG_DAC_CONTROL,
562 	ONYX_REG_DAC_DEEMPH,
563 	ONYX_REG_DAC_FILTER,
564 	ONYX_REG_DAC_OUTPHASE,
565 	ONYX_REG_ADC_CONTROL,
566 	ONYX_REG_ADC_HPF_BYPASS,
567 	ONYX_REG_DIG_INFO1,
568 	ONYX_REG_DIG_INFO2,
569 	ONYX_REG_DIG_INFO3,
570 	ONYX_REG_DIG_INFO4
571 };
572 
573 static const u8 initial_values[ARRAY_SIZE(register_map)] = {
574 	0x80, 0x80, /* muted */
575 	ONYX_MRST | ONYX_SRST, /* but handled specially! */
576 	ONYX_MUTE_LEFT | ONYX_MUTE_RIGHT,
577 	0, /* no deemphasis */
578 	ONYX_DAC_FILTER_ALWAYS,
579 	ONYX_OUTPHASE_INVERTED,
580 	(-1 /*dB*/ + 8) & 0xF, /* line in selected, -1 dB gain*/
581 	ONYX_ADC_HPF_ALWAYS,
582 	(1<<2),	/* pcm audio */
583 	2,	/* category: pcm coder */
584 	0,	/* sampling frequency 44.1 kHz, clock accuracy level II */
585 	1	/* 24 bit depth */
586 };
587 
588 /* reset registers of chip, either to initial or to previous values */
589 static int onyx_register_init(struct onyx *onyx)
590 {
591 	int i;
592 	u8 val;
593 	u8 regs[sizeof(initial_values)];
594 
595 	if (!onyx->initialised) {
596 		memcpy(regs, initial_values, sizeof(initial_values));
597 		if (onyx_read_register(onyx, ONYX_REG_CONTROL, &val))
598 			return -1;
599 		val &= ~ONYX_SILICONVERSION;
600 		val |= initial_values[3];
601 		regs[3] = val;
602 	} else {
603 		for (i=0; i<sizeof(register_map); i++)
604 			regs[i] = onyx->cache[register_map[i]-FIRSTREGISTER];
605 	}
606 
607 	for (i=0; i<sizeof(register_map); i++) {
608 		if (onyx_write_register(onyx, register_map[i], regs[i]))
609 			return -1;
610 	}
611 	onyx->initialised = 1;
612 	return 0;
613 }
614 
615 static struct transfer_info onyx_transfers[] = {
616 	/* this is first so we can skip it if no input is present...
617 	 * No hardware exists with that, but it's here as an example
618 	 * of what to do :) */
619 	{
620 		/* analog input */
621 		.formats = SNDRV_PCM_FMTBIT_S8 |
622 			   SNDRV_PCM_FMTBIT_S16_BE |
623 			   SNDRV_PCM_FMTBIT_S24_BE,
624 		.rates = SNDRV_PCM_RATE_8000_96000,
625 		.transfer_in = 1,
626 		.must_be_clock_source = 0,
627 		.tag = 0,
628 	},
629 	{
630 		/* if analog and digital are currently off, anything should go,
631 		 * so this entry describes everything we can do... */
632 		.formats = SNDRV_PCM_FMTBIT_S8 |
633 			   SNDRV_PCM_FMTBIT_S16_BE |
634 			   SNDRV_PCM_FMTBIT_S24_BE
635 #ifdef SNDRV_PCM_FMTBIT_COMPRESSED_16BE
636 			   | SNDRV_PCM_FMTBIT_COMPRESSED_16BE
637 #endif
638 		,
639 		.rates = SNDRV_PCM_RATE_8000_96000,
640 		.tag = 0,
641 	},
642 	{
643 		/* analog output */
644 		.formats = SNDRV_PCM_FMTBIT_S8 |
645 			   SNDRV_PCM_FMTBIT_S16_BE |
646 			   SNDRV_PCM_FMTBIT_S24_BE,
647 		.rates = SNDRV_PCM_RATE_8000_96000,
648 		.transfer_in = 0,
649 		.must_be_clock_source = 0,
650 		.tag = 1,
651 	},
652 	{
653 		/* digital pcm output, also possible for analog out */
654 		.formats = SNDRV_PCM_FMTBIT_S8 |
655 			   SNDRV_PCM_FMTBIT_S16_BE |
656 			   SNDRV_PCM_FMTBIT_S24_BE,
657 		.rates = SNDRV_PCM_RATE_32000 |
658 			 SNDRV_PCM_RATE_44100 |
659 			 SNDRV_PCM_RATE_48000,
660 		.transfer_in = 0,
661 		.must_be_clock_source = 0,
662 		.tag = 2,
663 	},
664 #ifdef SNDRV_PCM_FMTBIT_COMPRESSED_16BE
665 	/* Once alsa gets supports for this kind of thing we can add it... */
666 	{
667 		/* digital compressed output */
668 		.formats =  SNDRV_PCM_FMTBIT_COMPRESSED_16BE,
669 		.rates = SNDRV_PCM_RATE_32000 |
670 			 SNDRV_PCM_RATE_44100 |
671 			 SNDRV_PCM_RATE_48000,
672 		.tag = 2,
673 	},
674 #endif
675 	{}
676 };
677 
678 static int onyx_usable(struct codec_info_item *cii,
679 		       struct transfer_info *ti,
680 		       struct transfer_info *out)
681 {
682 	u8 v;
683 	struct onyx *onyx = cii->codec_data;
684 	int spdif_enabled, analog_enabled;
685 
686 	guard(mutex)(&onyx->mutex);
687 	onyx_read_register(onyx, ONYX_REG_DIG_INFO4, &v);
688 	spdif_enabled = !!(v & ONYX_SPDIF_ENABLE);
689 	onyx_read_register(onyx, ONYX_REG_DAC_CONTROL, &v);
690 	analog_enabled =
691 		(v & (ONYX_MUTE_RIGHT|ONYX_MUTE_LEFT))
692 		 != (ONYX_MUTE_RIGHT|ONYX_MUTE_LEFT);
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 
709 	guard(mutex)(&onyx->mutex);
710 
711 #ifdef SNDRV_PCM_FMTBIT_COMPRESSED_16BE
712 	if (substream->runtime->format == SNDRV_PCM_FMTBIT_COMPRESSED_16BE) {
713 		/* mute and lock analog output */
714 		onyx_read_register(onyx, ONYX_REG_DAC_CONTROL, &v);
715 		if (onyx_write_register(onyx,
716 					ONYX_REG_DAC_CONTROL,
717 					v | ONYX_MUTE_RIGHT | ONYX_MUTE_LEFT))
718 			return -EBUSY;
719 		onyx->analog_locked = 1;
720 		return 0;
721 	}
722 #endif
723 	switch (substream->runtime->rate) {
724 	case 32000:
725 	case 44100:
726 	case 48000:
727 		if (onyx->codec.connected & 2)
728 			return onyx_set_spdif_pcm_rate(onyx,
729 						       substream->runtime->rate);
730 		return 0;
731 	default:
732 		/* got some rate that the digital output can't do,
733 		 * so disable and lock it */
734 		onyx_read_register(cii->codec_data, ONYX_REG_DIG_INFO4, &v);
735 		if (onyx_write_register(onyx,
736 					ONYX_REG_DIG_INFO4,
737 					v & ~ONYX_SPDIF_ENABLE))
738 			return -EBUSY;
739 		onyx->spdif_locked = 1;
740 		return 0;
741 	}
742 
743 	return -EBUSY;
744 }
745 
746 static int onyx_open(struct codec_info_item *cii,
747 		     struct snd_pcm_substream *substream)
748 {
749 	struct onyx *onyx = cii->codec_data;
750 
751 	guard(mutex)(&onyx->mutex);
752 	onyx->open_count++;
753 
754 	return 0;
755 }
756 
757 static int onyx_close(struct codec_info_item *cii,
758 		      struct snd_pcm_substream *substream)
759 {
760 	struct onyx *onyx = cii->codec_data;
761 
762 	guard(mutex)(&onyx->mutex);
763 	onyx->open_count--;
764 	if (!onyx->open_count)
765 		onyx->spdif_locked = onyx->analog_locked = 0;
766 
767 	return 0;
768 }
769 
770 static int onyx_switch_clock(struct codec_info_item *cii,
771 			     enum clock_switch what)
772 {
773 	struct onyx *onyx = cii->codec_data;
774 
775 	guard(mutex)(&onyx->mutex);
776 	/* this *MUST* be more elaborate later... */
777 	switch (what) {
778 	case CLOCK_SWITCH_PREPARE_SLAVE:
779 		onyx->codec.gpio->methods->all_amps_off(onyx->codec.gpio);
780 		break;
781 	case CLOCK_SWITCH_SLAVE:
782 		onyx->codec.gpio->methods->all_amps_restore(onyx->codec.gpio);
783 		break;
784 	default: /* silence warning */
785 		break;
786 	}
787 
788 	return 0;
789 }
790 
791 #ifdef CONFIG_PM
792 
793 static int onyx_suspend(struct codec_info_item *cii, pm_message_t state)
794 {
795 	struct onyx *onyx = cii->codec_data;
796 	u8 v;
797 
798 	guard(mutex)(&onyx->mutex);
799 	if (onyx_read_register(onyx, ONYX_REG_CONTROL, &v))
800 		return -ENXIO;
801 	onyx_write_register(onyx, ONYX_REG_CONTROL, v | ONYX_ADPSV | ONYX_DAPSV);
802 	/* Apple does a sleep here but the datasheet says to do it on resume */
803 	return 0;
804 }
805 
806 static int onyx_resume(struct codec_info_item *cii)
807 {
808 	struct onyx *onyx = cii->codec_data;
809 	u8 v;
810 
811 	guard(mutex)(&onyx->mutex);
812 
813 	/* reset codec */
814 	onyx->codec.gpio->methods->set_hw_reset(onyx->codec.gpio, 0);
815 	msleep(1);
816 	onyx->codec.gpio->methods->set_hw_reset(onyx->codec.gpio, 1);
817 	msleep(1);
818 	onyx->codec.gpio->methods->set_hw_reset(onyx->codec.gpio, 0);
819 	msleep(1);
820 
821 	/* take codec out of suspend (if it still is after reset) */
822 	if (onyx_read_register(onyx, ONYX_REG_CONTROL, &v))
823 		return -ENXIO;
824 	onyx_write_register(onyx, ONYX_REG_CONTROL, v & ~(ONYX_ADPSV | ONYX_DAPSV));
825 	/* FIXME: should divide by sample rate, but 8k is the lowest we go */
826 	msleep(2205000/8000);
827 	/* reset all values */
828 	onyx_register_init(onyx);
829 	return 0;
830 }
831 
832 #endif /* CONFIG_PM */
833 
834 static struct codec_info onyx_codec_info = {
835 	.transfers = onyx_transfers,
836 	.sysclock_factor = 256,
837 	.bus_factor = 64,
838 	.owner = THIS_MODULE,
839 	.usable = onyx_usable,
840 	.prepare = onyx_prepare,
841 	.open = onyx_open,
842 	.close = onyx_close,
843 	.switch_clock = onyx_switch_clock,
844 #ifdef CONFIG_PM
845 	.suspend = onyx_suspend,
846 	.resume = onyx_resume,
847 #endif
848 };
849 
850 static int onyx_init_codec(struct aoa_codec *codec)
851 {
852 	struct onyx *onyx = codec_to_onyx(codec);
853 	struct snd_kcontrol *ctl;
854 	struct codec_info *ci = &onyx_codec_info;
855 	u8 v;
856 	int err;
857 
858 	if (!onyx->codec.gpio || !onyx->codec.gpio->methods) {
859 		printk(KERN_ERR PFX "gpios not assigned!!\n");
860 		return -EINVAL;
861 	}
862 
863 	onyx->codec.gpio->methods->set_hw_reset(onyx->codec.gpio, 0);
864 	msleep(1);
865 	onyx->codec.gpio->methods->set_hw_reset(onyx->codec.gpio, 1);
866 	msleep(1);
867 	onyx->codec.gpio->methods->set_hw_reset(onyx->codec.gpio, 0);
868 	msleep(1);
869 
870 	if (onyx_register_init(onyx)) {
871 		printk(KERN_ERR PFX "failed to initialise onyx registers\n");
872 		return -ENODEV;
873 	}
874 
875 	if (aoa_snd_device_new(SNDRV_DEV_CODEC, onyx, &ops)) {
876 		printk(KERN_ERR PFX "failed to create onyx snd device!\n");
877 		return -ENODEV;
878 	}
879 
880 	/* nothing connected? what a joke! */
881 	if ((onyx->codec.connected & 0xF) == 0)
882 		return -ENOTCONN;
883 
884 	/* if no inputs are present... */
885 	if ((onyx->codec.connected & 0xC) == 0) {
886 		if (!onyx->codec_info)
887 			onyx->codec_info = kmalloc_obj(struct codec_info);
888 		if (!onyx->codec_info)
889 			return -ENOMEM;
890 		ci = onyx->codec_info;
891 		*ci = onyx_codec_info;
892 		ci->transfers++;
893 	}
894 
895 	/* if no outputs are present... */
896 	if ((onyx->codec.connected & 3) == 0) {
897 		if (!onyx->codec_info)
898 			onyx->codec_info = kmalloc_obj(struct codec_info);
899 		if (!onyx->codec_info)
900 			return -ENOMEM;
901 		ci = onyx->codec_info;
902 		/* this is fine as there have to be inputs
903 		 * if we end up in this part of the code */
904 		*ci = onyx_codec_info;
905 		ci->transfers[1].formats = 0;
906 	}
907 
908 	if (onyx->codec.soundbus_dev->attach_codec(onyx->codec.soundbus_dev,
909 						   aoa_get_card(),
910 						   ci, onyx)) {
911 		printk(KERN_ERR PFX "error creating onyx pcm\n");
912 		return -ENODEV;
913 	}
914 #define ADDCTL(n)							\
915 	do {								\
916 		ctl = snd_ctl_new1(&n, onyx);				\
917 		if (ctl) {						\
918 			ctl->id.device =				\
919 				onyx->codec.soundbus_dev->pcm->device;	\
920 			err = aoa_snd_ctl_add(ctl);			\
921 			if (err)					\
922 				goto error;				\
923 		}							\
924 	} while (0)
925 
926 	if (onyx->codec.soundbus_dev->pcm) {
927 		/* give the user appropriate controls
928 		 * depending on what inputs are connected */
929 		if ((onyx->codec.connected & 0xC) == 0xC)
930 			ADDCTL(capture_source_control);
931 		else if (onyx->codec.connected & 4)
932 			onyx_set_capture_source(onyx, 0);
933 		else
934 			onyx_set_capture_source(onyx, 1);
935 		if (onyx->codec.connected & 0xC)
936 			ADDCTL(inputgain_control);
937 
938 		/* depending on what output is connected,
939 		 * give the user appropriate controls */
940 		if (onyx->codec.connected & 1) {
941 			ADDCTL(volume_control);
942 			ADDCTL(mute_control);
943 			ADDCTL(ovr1_control);
944 			ADDCTL(flt0_control);
945 			ADDCTL(hpf_control);
946 			ADDCTL(dm12_control);
947 			/* spdif control defaults to off */
948 		}
949 		if (onyx->codec.connected & 2) {
950 			ADDCTL(onyx_spdif_mask);
951 			ADDCTL(onyx_spdif_ctrl);
952 		}
953 		if ((onyx->codec.connected & 3) == 3)
954 			ADDCTL(spdif_control);
955 		/* if only S/PDIF is connected, enable it unconditionally */
956 		if ((onyx->codec.connected & 3) == 2) {
957 			onyx_read_register(onyx, ONYX_REG_DIG_INFO4, &v);
958 			v |= ONYX_SPDIF_ENABLE;
959 			onyx_write_register(onyx, ONYX_REG_DIG_INFO4, v);
960 		}
961 	}
962 #undef ADDCTL
963 	printk(KERN_INFO PFX "attached to onyx codec via i2c\n");
964 
965 	return 0;
966  error:
967 	onyx->codec.soundbus_dev->detach_codec(onyx->codec.soundbus_dev, onyx);
968 	snd_device_free(aoa_get_card(), onyx);
969 	return err;
970 }
971 
972 static void onyx_exit_codec(struct aoa_codec *codec)
973 {
974 	struct onyx *onyx = codec_to_onyx(codec);
975 
976 	if (!onyx->codec.soundbus_dev) {
977 		printk(KERN_ERR PFX "onyx_exit_codec called without soundbus_dev!\n");
978 		return;
979 	}
980 	onyx->codec.soundbus_dev->detach_codec(onyx->codec.soundbus_dev, onyx);
981 }
982 
983 static int onyx_i2c_probe(struct i2c_client *client)
984 {
985 	struct device_node *node = client->dev.of_node;
986 	struct onyx *onyx;
987 	u8 dummy;
988 
989 	onyx = kzalloc_obj(struct onyx);
990 
991 	if (!onyx)
992 		return -ENOMEM;
993 
994 	mutex_init(&onyx->mutex);
995 	onyx->i2c = client;
996 	i2c_set_clientdata(client, onyx);
997 
998 	/* we try to read from register ONYX_REG_CONTROL
999 	 * to check if the codec is present */
1000 	if (onyx_read_register(onyx, ONYX_REG_CONTROL, &dummy) != 0) {
1001 		printk(KERN_ERR PFX "failed to read control register\n");
1002 		goto fail;
1003 	}
1004 
1005 	strscpy(onyx->codec.name, "onyx");
1006 	onyx->codec.owner = THIS_MODULE;
1007 	onyx->codec.init = onyx_init_codec;
1008 	onyx->codec.exit = onyx_exit_codec;
1009 	onyx->codec.node = of_node_get(node);
1010 
1011 	if (aoa_codec_register(&onyx->codec)) {
1012 		goto fail_put;
1013 	}
1014 	printk(KERN_DEBUG PFX "created and attached onyx instance\n");
1015 	return 0;
1016  fail_put:
1017 	of_node_put(onyx->codec.node);
1018  fail:
1019 	kfree(onyx);
1020 	return -ENODEV;
1021 }
1022 
1023 static void onyx_i2c_remove(struct i2c_client *client)
1024 {
1025 	struct onyx *onyx = i2c_get_clientdata(client);
1026 
1027 	aoa_codec_unregister(&onyx->codec);
1028 	of_node_put(onyx->codec.node);
1029 	kfree(onyx->codec_info);
1030 	kfree(onyx);
1031 }
1032 
1033 static const struct i2c_device_id onyx_i2c_id[] = {
1034 	{ "MAC,pcm3052" },
1035 	{ }
1036 };
1037 MODULE_DEVICE_TABLE(i2c,onyx_i2c_id);
1038 
1039 static struct i2c_driver onyx_driver = {
1040 	.driver = {
1041 		.name = "aoa_codec_onyx",
1042 	},
1043 	.probe = onyx_i2c_probe,
1044 	.remove = onyx_i2c_remove,
1045 	.id_table = onyx_i2c_id,
1046 };
1047 
1048 module_i2c_driver(onyx_driver);
1049