• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #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");
40 
41 #include "onyx.h"
42 #include "../aoa.h"
43 #include "../soundbus/soundbus.h"
44 
45 
46 #define PFX "snd-aoa-codec-onyx: "
47 
48 struct onyx {
49 	/* cache registers 65 to 80, they are write-only! */
50 	u8			cache[16];
51 	struct i2c_client	*i2c;
52 	struct aoa_codec	codec;
53 	u32			initialised:1,
54 				spdif_locked:1,
55 				analog_locked:1,
56 				original_mute:2;
57 	int			open_count;
58 	struct codec_info	*codec_info;
59 
60 	/* mutex serializes concurrent access to the device
61 	 * and this structure.
62 	 */
63 	struct mutex mutex;
64 };
65 #define codec_to_onyx(c) container_of(c, struct onyx, codec)
66 
67 /* both return 0 if all ok, else on error */
onyx_read_register(struct onyx * onyx,u8 reg,u8 * value)68 static int onyx_read_register(struct onyx *onyx, u8 reg, u8 *value)
69 {
70 	s32 v;
71 
72 	if (reg != ONYX_REG_CONTROL) {
73 		*value = onyx->cache[reg-FIRSTREGISTER];
74 		return 0;
75 	}
76 	v = i2c_smbus_read_byte_data(onyx->i2c, reg);
77 	if (v < 0) {
78 		*value = 0;
79 		return -1;
80 	}
81 	*value = (u8)v;
82 	onyx->cache[ONYX_REG_CONTROL-FIRSTREGISTER] = *value;
83 	return 0;
84 }
85 
onyx_write_register(struct onyx * onyx,u8 reg,u8 value)86 static int onyx_write_register(struct onyx *onyx, u8 reg, u8 value)
87 {
88 	int result;
89 
90 	result = i2c_smbus_write_byte_data(onyx->i2c, reg, value);
91 	if (!result)
92 		onyx->cache[reg-FIRSTREGISTER] = value;
93 	return result;
94 }
95 
96 /* alsa stuff */
97 
onyx_dev_register(struct snd_device * dev)98 static int onyx_dev_register(struct snd_device *dev)
99 {
100 	return 0;
101 }
102 
103 static struct snd_device_ops ops = {
104 	.dev_register = onyx_dev_register,
105 };
106 
107 /* this is necessary because most alsa mixer programs
108  * can't properly handle the negative range */
109 #define VOLUME_RANGE_SHIFT	128
110 
onyx_snd_vol_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)111 static int onyx_snd_vol_info(struct snd_kcontrol *kcontrol,
112 	struct snd_ctl_elem_info *uinfo)
113 {
114 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
115 	uinfo->count = 2;
116 	uinfo->value.integer.min = -128 + VOLUME_RANGE_SHIFT;
117 	uinfo->value.integer.max = -1 + VOLUME_RANGE_SHIFT;
118 	return 0;
119 }
120 
onyx_snd_vol_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)121 static int onyx_snd_vol_get(struct snd_kcontrol *kcontrol,
122 	struct snd_ctl_elem_value *ucontrol)
123 {
124 	struct onyx *onyx = snd_kcontrol_chip(kcontrol);
125 	s8 l, r;
126 
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);
131 
132 	ucontrol->value.integer.value[0] = l + VOLUME_RANGE_SHIFT;
133 	ucontrol->value.integer.value[1] = r + VOLUME_RANGE_SHIFT;
134 
135 	return 0;
136 }
137 
onyx_snd_vol_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)138 static int onyx_snd_vol_put(struct snd_kcontrol *kcontrol,
139 	struct snd_ctl_elem_value *ucontrol)
140 {
141 	struct onyx *onyx = snd_kcontrol_chip(kcontrol);
142 	s8 l, r;
143 
144 	if (ucontrol->value.integer.value[0] < -128 + VOLUME_RANGE_SHIFT ||
145 	    ucontrol->value.integer.value[0] > -1 + VOLUME_RANGE_SHIFT)
146 		return -EINVAL;
147 	if (ucontrol->value.integer.value[1] < -128 + VOLUME_RANGE_SHIFT ||
148 	    ucontrol->value.integer.value[1] > -1 + VOLUME_RANGE_SHIFT)
149 		return -EINVAL;
150 
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);
154 
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);
158 		return 0;
159 	}
160 
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);
168 
169 	return 1;
170 }
171 
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,
179 };
180 
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)
186 
onyx_snd_inputgain_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)187 static int onyx_snd_inputgain_info(struct snd_kcontrol *kcontrol,
188 	struct snd_ctl_elem_info *uinfo)
189 {
190 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
191 	uinfo->count = 1;
192 	uinfo->value.integer.min = 3 + INPUTGAIN_RANGE_SHIFT;
193 	uinfo->value.integer.max = 28 + INPUTGAIN_RANGE_SHIFT;
194 	return 0;
195 }
196 
onyx_snd_inputgain_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)197 static int onyx_snd_inputgain_get(struct snd_kcontrol *kcontrol,
198 	struct snd_ctl_elem_value *ucontrol)
199 {
200 	struct onyx *onyx = snd_kcontrol_chip(kcontrol);
201 	u8 ig;
202 
203 	mutex_lock(&onyx->mutex);
204 	onyx_read_register(onyx, ONYX_REG_ADC_CONTROL, &ig);
205 	mutex_unlock(&onyx->mutex);
206 
207 	ucontrol->value.integer.value[0] =
208 		(ig & ONYX_ADC_PGA_GAIN_MASK) + INPUTGAIN_RANGE_SHIFT;
209 
210 	return 0;
211 }
212 
onyx_snd_inputgain_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)213 static int onyx_snd_inputgain_put(struct snd_kcontrol *kcontrol,
214 	struct snd_ctl_elem_value *ucontrol)
215 {
216 	struct onyx *onyx = snd_kcontrol_chip(kcontrol);
217 	u8 v, n;
218 
219 	if (ucontrol->value.integer.value[0] < 3 + INPUTGAIN_RANGE_SHIFT ||
220 	    ucontrol->value.integer.value[0] > 28 + INPUTGAIN_RANGE_SHIFT)
221 		return -EINVAL;
222 	mutex_lock(&onyx->mutex);
223 	onyx_read_register(onyx, ONYX_REG_ADC_CONTROL, &v);
224 	n = 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);
230 
231 	return n != v;
232 }
233 
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,
241 };
242 
onyx_snd_capture_source_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)243 static int onyx_snd_capture_source_info(struct snd_kcontrol *kcontrol,
244 	struct snd_ctl_elem_info *uinfo)
245 {
246 	static const char * const texts[] = { "Line-In", "Microphone" };
247 
248 	return snd_ctl_enum_info(uinfo, 1, 2, texts);
249 }
250 
onyx_snd_capture_source_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)251 static int onyx_snd_capture_source_get(struct snd_kcontrol *kcontrol,
252 	struct snd_ctl_elem_value *ucontrol)
253 {
254 	struct onyx *onyx = snd_kcontrol_chip(kcontrol);
255 	s8 v;
256 
257 	mutex_lock(&onyx->mutex);
258 	onyx_read_register(onyx, ONYX_REG_ADC_CONTROL, &v);
259 	mutex_unlock(&onyx->mutex);
260 
261 	ucontrol->value.enumerated.item[0] = !!(v&ONYX_ADC_INPUT_MIC);
262 
263 	return 0;
264 }
265 
onyx_set_capture_source(struct onyx * onyx,int mic)266 static void onyx_set_capture_source(struct onyx *onyx, int mic)
267 {
268 	s8 v;
269 
270 	mutex_lock(&onyx->mutex);
271 	onyx_read_register(onyx, ONYX_REG_ADC_CONTROL, &v);
272 	v &= ~ONYX_ADC_INPUT_MIC;
273 	if (mic)
274 		v |= ONYX_ADC_INPUT_MIC;
275 	onyx_write_register(onyx, ONYX_REG_ADC_CONTROL, v);
276 	mutex_unlock(&onyx->mutex);
277 }
278 
onyx_snd_capture_source_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)279 static int onyx_snd_capture_source_put(struct snd_kcontrol *kcontrol,
280 	struct snd_ctl_elem_value *ucontrol)
281 {
282 	if (ucontrol->value.enumerated.item[0] > 1)
283 		return -EINVAL;
284 	onyx_set_capture_source(snd_kcontrol_chip(kcontrol),
285 				ucontrol->value.enumerated.item[0]);
286 	return 1;
287 }
288 
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'
297 	 * category.
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".....
301 	 */
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,
307 };
308 
309 #define onyx_snd_mute_info	snd_ctl_boolean_stereo_info
310 
onyx_snd_mute_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)311 static int onyx_snd_mute_get(struct snd_kcontrol *kcontrol,
312 	struct snd_ctl_elem_value *ucontrol)
313 {
314 	struct onyx *onyx = snd_kcontrol_chip(kcontrol);
315 	u8 c;
316 
317 	mutex_lock(&onyx->mutex);
318 	onyx_read_register(onyx, ONYX_REG_DAC_CONTROL, &c);
319 	mutex_unlock(&onyx->mutex);
320 
321 	ucontrol->value.integer.value[0] = !(c & ONYX_MUTE_LEFT);
322 	ucontrol->value.integer.value[1] = !(c & ONYX_MUTE_RIGHT);
323 
324 	return 0;
325 }
326 
onyx_snd_mute_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)327 static int onyx_snd_mute_put(struct snd_kcontrol *kcontrol,
328 	struct snd_ctl_elem_value *ucontrol)
329 {
330 	struct onyx *onyx = snd_kcontrol_chip(kcontrol);
331 	u8 v = 0, c = 0;
332 	int err = -EBUSY;
333 
334 	mutex_lock(&onyx->mutex);
335 	if (onyx->analog_locked)
336 		goto out_unlock;
337 
338 	onyx_read_register(onyx, ONYX_REG_DAC_CONTROL, &v);
339 	c = v;
340 	c &= ~(ONYX_MUTE_RIGHT | ONYX_MUTE_LEFT);
341 	if (!ucontrol->value.integer.value[0])
342 		c |= ONYX_MUTE_LEFT;
343 	if (!ucontrol->value.integer.value[1])
344 		c |= ONYX_MUTE_RIGHT;
345 	err = onyx_write_register(onyx, ONYX_REG_DAC_CONTROL, c);
346 
347  out_unlock:
348 	mutex_unlock(&onyx->mutex);
349 
350 	return !err ? (v != c) : err;
351 }
352 
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,
360 };
361 
362 
363 #define onyx_snd_single_bit_info	snd_ctl_boolean_mono_info
364 
365 #define FLAG_POLARITY_INVERT	1
366 #define FLAG_SPDIFLOCK		2
367 
onyx_snd_single_bit_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)368 static int onyx_snd_single_bit_get(struct snd_kcontrol *kcontrol,
369 	struct snd_ctl_elem_value *ucontrol)
370 {
371 	struct onyx *onyx = snd_kcontrol_chip(kcontrol);
372 	u8 c;
373 	long int pv = kcontrol->private_value;
374 	u8 polarity = (pv >> 16) & FLAG_POLARITY_INVERT;
375 	u8 address = (pv >> 8) & 0xff;
376 	u8 mask = pv & 0xff;
377 
378 	mutex_lock(&onyx->mutex);
379 	onyx_read_register(onyx, address, &c);
380 	mutex_unlock(&onyx->mutex);
381 
382 	ucontrol->value.integer.value[0] = !!(c & mask) ^ polarity;
383 
384 	return 0;
385 }
386 
onyx_snd_single_bit_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)387 static int onyx_snd_single_bit_put(struct snd_kcontrol *kcontrol,
388 	struct snd_ctl_elem_value *ucontrol)
389 {
390 	struct onyx *onyx = snd_kcontrol_chip(kcontrol);
391 	u8 v = 0, c = 0;
392 	int err;
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;
397 	u8 mask = pv & 0xff;
398 
399 	mutex_lock(&onyx->mutex);
400 	if (spdiflock && onyx->spdif_locked) {
401 		/* even if alsamixer doesn't care.. */
402 		err = -EBUSY;
403 		goto out_unlock;
404 	}
405 	onyx_read_register(onyx, address, &v);
406 	c = v;
407 	c &= ~(mask);
408 	if (!!ucontrol->value.integer.value[0] ^ polarity)
409 		c |= mask;
410 	err = onyx_write_register(onyx, address, c);
411 
412  out_unlock:
413 	mutex_unlock(&onyx->mutex);
414 
415 	return !err ? (v != c) : err;
416 }
417 
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		\
427 }
428 
429 SINGLE_BIT(spdif,
430 	   MIXER,
431 	   SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
432 	   ONYX_REG_DIG_INFO4,
433 	   ONYX_SPDIF_ENABLE,
434 	   FLAG_SPDIFLOCK);
435 SINGLE_BIT(ovr1,
436 	   MIXER,
437 	   "Oversampling Rate",
438 	   ONYX_REG_DAC_CONTROL,
439 	   ONYX_OVR1,
440 	   0);
441 SINGLE_BIT(flt0,
442 	   MIXER,
443 	   "Fast Digital Filter Rolloff",
444 	   ONYX_REG_DAC_FILTER,
445 	   ONYX_ROLLOFF_FAST,
446 	   FLAG_POLARITY_INVERT);
447 SINGLE_BIT(hpf,
448 	   MIXER,
449 	   "Highpass Filter",
450 	   ONYX_REG_ADC_HPF_BYPASS,
451 	   ONYX_HPF_DISABLE,
452 	   FLAG_POLARITY_INVERT);
453 SINGLE_BIT(dm12,
454 	   MIXER,
455 	   "Digital De-Emphasis",
456 	   ONYX_REG_DAC_DEEMPH,
457 	   ONYX_DIGDEEMPH_CTRL,
458 	   0);
459 
onyx_spdif_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)460 static int onyx_spdif_info(struct snd_kcontrol *kcontrol,
461 			   struct snd_ctl_elem_info *uinfo)
462 {
463 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
464 	uinfo->count = 1;
465 	return 0;
466 }
467 
onyx_spdif_mask_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)468 static int onyx_spdif_mask_get(struct snd_kcontrol *kcontrol,
469 			       struct snd_ctl_elem_value *ucontrol)
470 {
471 	/* datasheet page 30, all others are 0 */
472 	ucontrol->value.iec958.status[0] = 0x3e;
473 	ucontrol->value.iec958.status[1] = 0xff;
474 
475 	ucontrol->value.iec958.status[3] = 0x3f;
476 	ucontrol->value.iec958.status[4] = 0x0f;
477 
478 	return 0;
479 }
480 
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,
487 };
488 
onyx_spdif_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)489 static int onyx_spdif_get(struct snd_kcontrol *kcontrol,
490 			  struct snd_ctl_elem_value *ucontrol)
491 {
492 	struct onyx *onyx = snd_kcontrol_chip(kcontrol);
493 	u8 v;
494 
495 	mutex_lock(&onyx->mutex);
496 	onyx_read_register(onyx, ONYX_REG_DIG_INFO1, &v);
497 	ucontrol->value.iec958.status[0] = v & 0x3e;
498 
499 	onyx_read_register(onyx, ONYX_REG_DIG_INFO2, &v);
500 	ucontrol->value.iec958.status[1] = v;
501 
502 	onyx_read_register(onyx, ONYX_REG_DIG_INFO3, &v);
503 	ucontrol->value.iec958.status[3] = v & 0x3f;
504 
505 	onyx_read_register(onyx, ONYX_REG_DIG_INFO4, &v);
506 	ucontrol->value.iec958.status[4] = v & 0x0f;
507 	mutex_unlock(&onyx->mutex);
508 
509 	return 0;
510 }
511 
onyx_spdif_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)512 static int onyx_spdif_put(struct snd_kcontrol *kcontrol,
513 			  struct snd_ctl_elem_value *ucontrol)
514 {
515 	struct onyx *onyx = snd_kcontrol_chip(kcontrol);
516 	u8 v;
517 
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);
522 
523 	v = ucontrol->value.iec958.status[1];
524 	onyx_write_register(onyx, ONYX_REG_DIG_INFO2, v);
525 
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);
529 
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);
534 
535 	return 1;
536 }
537 
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,
545 };
546 
547 /* our registers */
548 
549 static u8 register_map[] = {
550 	ONYX_REG_DAC_ATTEN_LEFT,
551 	ONYX_REG_DAC_ATTEN_RIGHT,
552 	ONYX_REG_CONTROL,
553 	ONYX_REG_DAC_CONTROL,
554 	ONYX_REG_DAC_DEEMPH,
555 	ONYX_REG_DAC_FILTER,
556 	ONYX_REG_DAC_OUTPHASE,
557 	ONYX_REG_ADC_CONTROL,
558 	ONYX_REG_ADC_HPF_BYPASS,
559 	ONYX_REG_DIG_INFO1,
560 	ONYX_REG_DIG_INFO2,
561 	ONYX_REG_DIG_INFO3,
562 	ONYX_REG_DIG_INFO4
563 };
564 
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*/
573 	ONYX_ADC_HPF_ALWAYS,
574 	(1<<2),	/* pcm audio */
575 	2,	/* category: pcm coder */
576 	0,	/* sampling frequency 44.1 kHz, clock accuracy level II */
577 	1	/* 24 bit depth */
578 };
579 
580 /* reset registers of chip, either to initial or to previous values */
onyx_register_init(struct onyx * onyx)581 static int onyx_register_init(struct onyx *onyx)
582 {
583 	int i;
584 	u8 val;
585 	u8 regs[sizeof(initial_values)];
586 
587 	if (!onyx->initialised) {
588 		memcpy(regs, initial_values, sizeof(initial_values));
589 		if (onyx_read_register(onyx, ONYX_REG_CONTROL, &val))
590 			return -1;
591 		val &= ~ONYX_SILICONVERSION;
592 		val |= initial_values[3];
593 		regs[3] = val;
594 	} else {
595 		for (i=0; i<sizeof(register_map); i++)
596 			regs[i] = onyx->cache[register_map[i]-FIRSTREGISTER];
597 	}
598 
599 	for (i=0; i<sizeof(register_map); i++) {
600 		if (onyx_write_register(onyx, register_map[i], regs[i]))
601 			return -1;
602 	}
603 	onyx->initialised = 1;
604 	return 0;
605 }
606 
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 :) */
611 	{
612 		/* analog input */
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,
617 		.transfer_in = 1,
618 		.must_be_clock_source = 0,
619 		.tag = 0,
620 	},
621 	{
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
629 #endif
630 		,
631 		.rates = SNDRV_PCM_RATE_8000_96000,
632 		.tag = 0,
633 	},
634 	{
635 		/* analog output */
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,
640 		.transfer_in = 0,
641 		.must_be_clock_source = 0,
642 		.tag = 1,
643 	},
644 	{
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,
652 		.transfer_in = 0,
653 		.must_be_clock_source = 0,
654 		.tag = 2,
655 	},
656 #ifdef SNDRV_PCM_FMTBIT_COMPRESSED_16BE
657 	/* Once alsa gets supports for this kind of thing we can add it... */
658 	{
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,
664 		.tag = 2,
665 	},
666 #endif
667 	{}
668 };
669 
onyx_usable(struct codec_info_item * cii,struct transfer_info * ti,struct transfer_info * out)670 static int onyx_usable(struct codec_info_item *cii,
671 		       struct transfer_info *ti,
672 		       struct transfer_info *out)
673 {
674 	u8 v;
675 	struct onyx *onyx = cii->codec_data;
676 	int spdif_enabled, analog_enabled;
677 
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);
682 	analog_enabled =
683 		(v & (ONYX_MUTE_RIGHT|ONYX_MUTE_LEFT))
684 		 != (ONYX_MUTE_RIGHT|ONYX_MUTE_LEFT);
685 	mutex_unlock(&onyx->mutex);
686 
687 	switch (ti->tag) {
688 	case 0: return 1;
689 	case 1:	return analog_enabled;
690 	case 2: return spdif_enabled;
691 	}
692 	return 1;
693 }
694 
onyx_prepare(struct codec_info_item * cii,struct bus_info * bi,struct snd_pcm_substream * substream)695 static int onyx_prepare(struct codec_info_item *cii,
696 			struct bus_info *bi,
697 			struct snd_pcm_substream *substream)
698 {
699 	u8 v;
700 	struct onyx *onyx = cii->codec_data;
701 	int err = -EBUSY;
702 
703 	mutex_lock(&onyx->mutex);
704 
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))
712 			goto out_unlock;
713 		onyx->analog_locked = 1;
714 		err = 0;
715 		goto out_unlock;
716 	}
717 #endif
718 	switch (substream->runtime->rate) {
719 	case 32000:
720 	case 44100:
721 	case 48000:
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! */
725 		err = 0;
726 		goto out_unlock;
727 	default:
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,
732 					ONYX_REG_DIG_INFO4,
733 					v & ~ONYX_SPDIF_ENABLE))
734 			goto out_unlock;
735 		onyx->spdif_locked = 1;
736 		err = 0;
737 		goto out_unlock;
738 	}
739 
740  out_unlock:
741 	mutex_unlock(&onyx->mutex);
742 
743 	return err;
744 }
745 
onyx_open(struct codec_info_item * cii,struct snd_pcm_substream * substream)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 	mutex_lock(&onyx->mutex);
752 	onyx->open_count++;
753 	mutex_unlock(&onyx->mutex);
754 
755 	return 0;
756 }
757 
onyx_close(struct codec_info_item * cii,struct snd_pcm_substream * substream)758 static int onyx_close(struct codec_info_item *cii,
759 		      struct snd_pcm_substream *substream)
760 {
761 	struct onyx *onyx = cii->codec_data;
762 
763 	mutex_lock(&onyx->mutex);
764 	onyx->open_count--;
765 	if (!onyx->open_count)
766 		onyx->spdif_locked = onyx->analog_locked = 0;
767 	mutex_unlock(&onyx->mutex);
768 
769 	return 0;
770 }
771 
onyx_switch_clock(struct codec_info_item * cii,enum clock_switch what)772 static int onyx_switch_clock(struct codec_info_item *cii,
773 			     enum clock_switch what)
774 {
775 	struct onyx *onyx = cii->codec_data;
776 
777 	mutex_lock(&onyx->mutex);
778 	/* this *MUST* be more elaborate later... */
779 	switch (what) {
780 	case CLOCK_SWITCH_PREPARE_SLAVE:
781 		onyx->codec.gpio->methods->all_amps_off(onyx->codec.gpio);
782 		break;
783 	case CLOCK_SWITCH_SLAVE:
784 		onyx->codec.gpio->methods->all_amps_restore(onyx->codec.gpio);
785 		break;
786 	default: /* silence warning */
787 		break;
788 	}
789 	mutex_unlock(&onyx->mutex);
790 
791 	return 0;
792 }
793 
794 #ifdef CONFIG_PM
795 
onyx_suspend(struct codec_info_item * cii,pm_message_t state)796 static int onyx_suspend(struct codec_info_item *cii, pm_message_t state)
797 {
798 	struct onyx *onyx = cii->codec_data;
799 	u8 v;
800 	int err = -ENXIO;
801 
802 	mutex_lock(&onyx->mutex);
803 	if (onyx_read_register(onyx, ONYX_REG_CONTROL, &v))
804 		goto out_unlock;
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 */
807 	err = 0;
808  out_unlock:
809 	mutex_unlock(&onyx->mutex);
810 
811 	return err;
812 }
813 
onyx_resume(struct codec_info_item * cii)814 static int onyx_resume(struct codec_info_item *cii)
815 {
816 	struct onyx *onyx = cii->codec_data;
817 	u8 v;
818 	int err = -ENXIO;
819 
820 	mutex_lock(&onyx->mutex);
821 
822 	/* reset codec */
823 	onyx->codec.gpio->methods->set_hw_reset(onyx->codec.gpio, 0);
824 	msleep(1);
825 	onyx->codec.gpio->methods->set_hw_reset(onyx->codec.gpio, 1);
826 	msleep(1);
827 	onyx->codec.gpio->methods->set_hw_reset(onyx->codec.gpio, 0);
828 	msleep(1);
829 
830 	/* take codec out of suspend (if it still is after reset) */
831 	if (onyx_read_register(onyx, ONYX_REG_CONTROL, &v))
832 		goto out_unlock;
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);
838 	err = 0;
839  out_unlock:
840 	mutex_unlock(&onyx->mutex);
841 
842 	return err;
843 }
844 
845 #endif /* CONFIG_PM */
846 
847 static struct codec_info onyx_codec_info = {
848 	.transfers = onyx_transfers,
849 	.sysclock_factor = 256,
850 	.bus_factor = 64,
851 	.owner = THIS_MODULE,
852 	.usable = onyx_usable,
853 	.prepare = onyx_prepare,
854 	.open = onyx_open,
855 	.close = onyx_close,
856 	.switch_clock = onyx_switch_clock,
857 #ifdef CONFIG_PM
858 	.suspend = onyx_suspend,
859 	.resume = onyx_resume,
860 #endif
861 };
862 
onyx_init_codec(struct aoa_codec * codec)863 static int onyx_init_codec(struct aoa_codec *codec)
864 {
865 	struct onyx *onyx = codec_to_onyx(codec);
866 	struct snd_kcontrol *ctl;
867 	struct codec_info *ci = &onyx_codec_info;
868 	u8 v;
869 	int err;
870 
871 	if (!onyx->codec.gpio || !onyx->codec.gpio->methods) {
872 		printk(KERN_ERR PFX "gpios not assigned!!\n");
873 		return -EINVAL;
874 	}
875 
876 	onyx->codec.gpio->methods->set_hw_reset(onyx->codec.gpio, 0);
877 	msleep(1);
878 	onyx->codec.gpio->methods->set_hw_reset(onyx->codec.gpio, 1);
879 	msleep(1);
880 	onyx->codec.gpio->methods->set_hw_reset(onyx->codec.gpio, 0);
881 	msleep(1);
882 
883 	if (onyx_register_init(onyx)) {
884 		printk(KERN_ERR PFX "failed to initialise onyx registers\n");
885 		return -ENODEV;
886 	}
887 
888 	if (aoa_snd_device_new(SNDRV_DEV_CODEC, onyx, &ops)) {
889 		printk(KERN_ERR PFX "failed to create onyx snd device!\n");
890 		return -ENODEV;
891 	}
892 
893 	/* nothing connected? what a joke! */
894 	if ((onyx->codec.connected & 0xF) == 0)
895 		return -ENOTCONN;
896 
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)
902 			return -ENOMEM;
903 		ci = onyx->codec_info;
904 		*ci = onyx_codec_info;
905 		ci->transfers++;
906 	}
907 
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)
913 			return -ENOMEM;
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;
919 	}
920 
921 	if (onyx->codec.soundbus_dev->attach_codec(onyx->codec.soundbus_dev,
922 						   aoa_get_card(),
923 						   ci, onyx)) {
924 		printk(KERN_ERR PFX "error creating onyx pcm\n");
925 		return -ENODEV;
926 	}
927 #define ADDCTL(n)							\
928 	do {								\
929 		ctl = snd_ctl_new1(&n, onyx);				\
930 		if (ctl) {						\
931 			ctl->id.device =				\
932 				onyx->codec.soundbus_dev->pcm->device;	\
933 			err = aoa_snd_ctl_add(ctl);			\
934 			if (err)					\
935 				goto error;				\
936 		}							\
937 	} while (0)
938 
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);
946 		else
947 			onyx_set_capture_source(onyx, 1);
948 		if (onyx->codec.connected & 0xC)
949 			ADDCTL(inputgain_control);
950 
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);
958 			ADDCTL(hpf_control);
959 			ADDCTL(dm12_control);
960 			/* spdif control defaults to off */
961 		}
962 		if (onyx->codec.connected & 2) {
963 			ADDCTL(onyx_spdif_mask);
964 			ADDCTL(onyx_spdif_ctrl);
965 		}
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);
973 		}
974 	}
975 #undef ADDCTL
976 	printk(KERN_INFO PFX "attached to onyx codec via i2c\n");
977 
978 	return 0;
979  error:
980 	onyx->codec.soundbus_dev->detach_codec(onyx->codec.soundbus_dev, onyx);
981 	snd_device_free(aoa_get_card(), onyx);
982 	return err;
983 }
984 
onyx_exit_codec(struct aoa_codec * codec)985 static void onyx_exit_codec(struct aoa_codec *codec)
986 {
987 	struct onyx *onyx = codec_to_onyx(codec);
988 
989 	if (!onyx->codec.soundbus_dev) {
990 		printk(KERN_ERR PFX "onyx_exit_codec called without soundbus_dev!\n");
991 		return;
992 	}
993 	onyx->codec.soundbus_dev->detach_codec(onyx->codec.soundbus_dev, onyx);
994 }
995 
onyx_i2c_probe(struct i2c_client * client,const struct i2c_device_id * id)996 static int onyx_i2c_probe(struct i2c_client *client,
997 			  const struct i2c_device_id *id)
998 {
999 	struct device_node *node = client->dev.of_node;
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 = client;
1010 	i2c_set_clientdata(client, onyx);
1011 
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");
1016 		goto fail;
1017 	}
1018 
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);
1024 
1025 	if (aoa_codec_register(&onyx->codec)) {
1026 		goto fail;
1027 	}
1028 	printk(KERN_DEBUG PFX "created and attached onyx instance\n");
1029 	return 0;
1030  fail:
1031 	kfree(onyx);
1032 	return -ENODEV;
1033 }
1034 
onyx_i2c_remove(struct i2c_client * client)1035 static int onyx_i2c_remove(struct i2c_client *client)
1036 {
1037 	struct onyx *onyx = i2c_get_clientdata(client);
1038 
1039 	aoa_codec_unregister(&onyx->codec);
1040 	of_node_put(onyx->codec.node);
1041 	kfree(onyx->codec_info);
1042 	kfree(onyx);
1043 	return 0;
1044 }
1045 
1046 static const struct i2c_device_id onyx_i2c_id[] = {
1047 	{ "MAC,pcm3052", 0 },
1048 	{ }
1049 };
1050 MODULE_DEVICE_TABLE(i2c,onyx_i2c_id);
1051 
1052 static struct i2c_driver onyx_driver = {
1053 	.driver = {
1054 		.name = "aoa_codec_onyx",
1055 	},
1056 	.probe = onyx_i2c_probe,
1057 	.remove = onyx_i2c_remove,
1058 	.id_table = onyx_i2c_id,
1059 };
1060 
1061 module_i2c_driver(onyx_driver);
1062