xref: /linux/sound/soc/codecs/uda1380.c (revision b7482f52789266e2548be5d0f6420c9fc12428d8)
1*b7482f52SPhilipp Zabel /*
2*b7482f52SPhilipp Zabel  * uda1380.c - Philips UDA1380 ALSA SoC audio driver
3*b7482f52SPhilipp Zabel  *
4*b7482f52SPhilipp Zabel  * This program is free software; you can redistribute it and/or modify
5*b7482f52SPhilipp Zabel  * it under the terms of the GNU General Public License version 2 as
6*b7482f52SPhilipp Zabel  * published by the Free Software Foundation.
7*b7482f52SPhilipp Zabel  *
8*b7482f52SPhilipp Zabel  * Copyright (c) 2007 Philipp Zabel <philipp.zabel@gmail.com>
9*b7482f52SPhilipp Zabel  * Improved support for DAPM and audio routing/mixing capabilities,
10*b7482f52SPhilipp Zabel  * added TLV support.
11*b7482f52SPhilipp Zabel  *
12*b7482f52SPhilipp Zabel  * Modified by Richard Purdie <richard@openedhand.com> to fit into SoC
13*b7482f52SPhilipp Zabel  * codec model.
14*b7482f52SPhilipp Zabel  *
15*b7482f52SPhilipp Zabel  * Copyright (c) 2005 Giorgio Padrin <giorgio@mandarinlogiq.org>
16*b7482f52SPhilipp Zabel  * Copyright 2005 Openedhand Ltd.
17*b7482f52SPhilipp Zabel  */
18*b7482f52SPhilipp Zabel 
19*b7482f52SPhilipp Zabel #include <linux/module.h>
20*b7482f52SPhilipp Zabel #include <linux/init.h>
21*b7482f52SPhilipp Zabel #include <linux/types.h>
22*b7482f52SPhilipp Zabel #include <linux/string.h>
23*b7482f52SPhilipp Zabel #include <linux/slab.h>
24*b7482f52SPhilipp Zabel #include <linux/errno.h>
25*b7482f52SPhilipp Zabel #include <linux/ioctl.h>
26*b7482f52SPhilipp Zabel #include <linux/delay.h>
27*b7482f52SPhilipp Zabel #include <linux/i2c.h>
28*b7482f52SPhilipp Zabel #include <sound/core.h>
29*b7482f52SPhilipp Zabel #include <sound/control.h>
30*b7482f52SPhilipp Zabel #include <sound/initval.h>
31*b7482f52SPhilipp Zabel #include <sound/info.h>
32*b7482f52SPhilipp Zabel #include <sound/soc.h>
33*b7482f52SPhilipp Zabel #include <sound/soc-dapm.h>
34*b7482f52SPhilipp Zabel #include <sound/tlv.h>
35*b7482f52SPhilipp Zabel 
36*b7482f52SPhilipp Zabel #include "uda1380.h"
37*b7482f52SPhilipp Zabel 
38*b7482f52SPhilipp Zabel #define UDA1380_VERSION "0.6"
39*b7482f52SPhilipp Zabel #define AUDIO_NAME "uda1380"
40*b7482f52SPhilipp Zabel 
41*b7482f52SPhilipp Zabel /*
42*b7482f52SPhilipp Zabel  * uda1380 register cache
43*b7482f52SPhilipp Zabel  */
44*b7482f52SPhilipp Zabel static const u16 uda1380_reg[UDA1380_CACHEREGNUM] = {
45*b7482f52SPhilipp Zabel 	0x0502, 0x0000, 0x0000, 0x3f3f,
46*b7482f52SPhilipp Zabel 	0x0202, 0x0000, 0x0000, 0x0000,
47*b7482f52SPhilipp Zabel 	0x0000, 0x0000, 0x0000, 0x0000,
48*b7482f52SPhilipp Zabel 	0x0000, 0x0000, 0x0000, 0x0000,
49*b7482f52SPhilipp Zabel 	0x0000, 0xff00, 0x0000, 0x4800,
50*b7482f52SPhilipp Zabel 	0x0000, 0x0000, 0x0000, 0x0000,
51*b7482f52SPhilipp Zabel 	0x0000, 0x0000, 0x0000, 0x0000,
52*b7482f52SPhilipp Zabel 	0x0000, 0x0000, 0x0000, 0x0000,
53*b7482f52SPhilipp Zabel 	0x0000, 0x8000, 0x0002, 0x0000,
54*b7482f52SPhilipp Zabel };
55*b7482f52SPhilipp Zabel 
56*b7482f52SPhilipp Zabel /*
57*b7482f52SPhilipp Zabel  * read uda1380 register cache
58*b7482f52SPhilipp Zabel  */
59*b7482f52SPhilipp Zabel static inline unsigned int uda1380_read_reg_cache(struct snd_soc_codec *codec,
60*b7482f52SPhilipp Zabel 	unsigned int reg)
61*b7482f52SPhilipp Zabel {
62*b7482f52SPhilipp Zabel 	u16 *cache = codec->reg_cache;
63*b7482f52SPhilipp Zabel 	if (reg == UDA1380_RESET)
64*b7482f52SPhilipp Zabel 		return 0;
65*b7482f52SPhilipp Zabel 	if (reg >= UDA1380_CACHEREGNUM)
66*b7482f52SPhilipp Zabel 		return -1;
67*b7482f52SPhilipp Zabel 	return cache[reg];
68*b7482f52SPhilipp Zabel }
69*b7482f52SPhilipp Zabel 
70*b7482f52SPhilipp Zabel /*
71*b7482f52SPhilipp Zabel  * write uda1380 register cache
72*b7482f52SPhilipp Zabel  */
73*b7482f52SPhilipp Zabel static inline void uda1380_write_reg_cache(struct snd_soc_codec *codec,
74*b7482f52SPhilipp Zabel 	u16 reg, unsigned int value)
75*b7482f52SPhilipp Zabel {
76*b7482f52SPhilipp Zabel 	u16 *cache = codec->reg_cache;
77*b7482f52SPhilipp Zabel 	if (reg >= UDA1380_CACHEREGNUM)
78*b7482f52SPhilipp Zabel 		return;
79*b7482f52SPhilipp Zabel 	cache[reg] = value;
80*b7482f52SPhilipp Zabel }
81*b7482f52SPhilipp Zabel 
82*b7482f52SPhilipp Zabel /*
83*b7482f52SPhilipp Zabel  * write to the UDA1380 register space
84*b7482f52SPhilipp Zabel  */
85*b7482f52SPhilipp Zabel static int uda1380_write(struct snd_soc_codec *codec, unsigned int reg,
86*b7482f52SPhilipp Zabel 	unsigned int value)
87*b7482f52SPhilipp Zabel {
88*b7482f52SPhilipp Zabel 	u8 data[3];
89*b7482f52SPhilipp Zabel 
90*b7482f52SPhilipp Zabel 	/* data is
91*b7482f52SPhilipp Zabel 	 *   data[0] is register offset
92*b7482f52SPhilipp Zabel 	 *   data[1] is MS byte
93*b7482f52SPhilipp Zabel 	 *   data[2] is LS byte
94*b7482f52SPhilipp Zabel 	 */
95*b7482f52SPhilipp Zabel 	data[0] = reg;
96*b7482f52SPhilipp Zabel 	data[1] = (value & 0xff00) >> 8;
97*b7482f52SPhilipp Zabel 	data[2] = value & 0x00ff;
98*b7482f52SPhilipp Zabel 
99*b7482f52SPhilipp Zabel 	uda1380_write_reg_cache(codec, reg, value);
100*b7482f52SPhilipp Zabel 
101*b7482f52SPhilipp Zabel 	/* the interpolator & decimator regs must only be written when the
102*b7482f52SPhilipp Zabel 	 * codec DAI is active.
103*b7482f52SPhilipp Zabel 	 */
104*b7482f52SPhilipp Zabel 	if (!codec->active && (reg >= UDA1380_MVOL))
105*b7482f52SPhilipp Zabel 		return 0;
106*b7482f52SPhilipp Zabel 	pr_debug("uda1380: hw write %x val %x\n", reg, value);
107*b7482f52SPhilipp Zabel 	if (codec->hw_write(codec->control_data, data, 3) == 3) {
108*b7482f52SPhilipp Zabel 		unsigned int val;
109*b7482f52SPhilipp Zabel 		i2c_master_send(codec->control_data, data, 1);
110*b7482f52SPhilipp Zabel 		i2c_master_recv(codec->control_data, data, 2);
111*b7482f52SPhilipp Zabel 		val = (data[0]<<8) | data[1];
112*b7482f52SPhilipp Zabel 		if (val != value) {
113*b7482f52SPhilipp Zabel 			pr_debug("uda1380: READ BACK VAL %x\n",
114*b7482f52SPhilipp Zabel 					(data[0]<<8) | data[1]);
115*b7482f52SPhilipp Zabel 			return -EIO;
116*b7482f52SPhilipp Zabel 		}
117*b7482f52SPhilipp Zabel 		return 0;
118*b7482f52SPhilipp Zabel 	} else
119*b7482f52SPhilipp Zabel 		return -EIO;
120*b7482f52SPhilipp Zabel }
121*b7482f52SPhilipp Zabel 
122*b7482f52SPhilipp Zabel #define uda1380_reset(c)	uda1380_write(c, UDA1380_RESET, 0)
123*b7482f52SPhilipp Zabel 
124*b7482f52SPhilipp Zabel /* declarations of ALSA reg_elem_REAL controls */
125*b7482f52SPhilipp Zabel static const char *uda1380_deemp[] = {
126*b7482f52SPhilipp Zabel 	"None",
127*b7482f52SPhilipp Zabel 	"32kHz",
128*b7482f52SPhilipp Zabel 	"44.1kHz",
129*b7482f52SPhilipp Zabel 	"48kHz",
130*b7482f52SPhilipp Zabel 	"96kHz",
131*b7482f52SPhilipp Zabel };
132*b7482f52SPhilipp Zabel static const char *uda1380_input_sel[] = {
133*b7482f52SPhilipp Zabel 	"Line",
134*b7482f52SPhilipp Zabel 	"Mic + Line R",
135*b7482f52SPhilipp Zabel 	"Line L",
136*b7482f52SPhilipp Zabel 	"Mic",
137*b7482f52SPhilipp Zabel };
138*b7482f52SPhilipp Zabel static const char *uda1380_output_sel[] = {
139*b7482f52SPhilipp Zabel 	"DAC",
140*b7482f52SPhilipp Zabel 	"Analog Mixer",
141*b7482f52SPhilipp Zabel };
142*b7482f52SPhilipp Zabel static const char *uda1380_spf_mode[] = {
143*b7482f52SPhilipp Zabel 	"Flat",
144*b7482f52SPhilipp Zabel 	"Minimum1",
145*b7482f52SPhilipp Zabel 	"Minimum2",
146*b7482f52SPhilipp Zabel 	"Maximum"
147*b7482f52SPhilipp Zabel };
148*b7482f52SPhilipp Zabel static const char *uda1380_capture_sel[] = {
149*b7482f52SPhilipp Zabel 	"ADC",
150*b7482f52SPhilipp Zabel 	"Digital Mixer"
151*b7482f52SPhilipp Zabel };
152*b7482f52SPhilipp Zabel static const char *uda1380_sel_ns[] = {
153*b7482f52SPhilipp Zabel 	"3rd-order",
154*b7482f52SPhilipp Zabel 	"5th-order"
155*b7482f52SPhilipp Zabel };
156*b7482f52SPhilipp Zabel static const char *uda1380_mix_control[] = {
157*b7482f52SPhilipp Zabel 	"off",
158*b7482f52SPhilipp Zabel 	"PCM only",
159*b7482f52SPhilipp Zabel 	"before sound processing",
160*b7482f52SPhilipp Zabel 	"after sound processing"
161*b7482f52SPhilipp Zabel };
162*b7482f52SPhilipp Zabel static const char *uda1380_sdet_setting[] = {
163*b7482f52SPhilipp Zabel 	"3200",
164*b7482f52SPhilipp Zabel 	"4800",
165*b7482f52SPhilipp Zabel 	"9600",
166*b7482f52SPhilipp Zabel 	"19200"
167*b7482f52SPhilipp Zabel };
168*b7482f52SPhilipp Zabel static const char *uda1380_os_setting[] = {
169*b7482f52SPhilipp Zabel 	"single-speed",
170*b7482f52SPhilipp Zabel 	"double-speed (no mixing)",
171*b7482f52SPhilipp Zabel 	"quad-speed (no mixing)"
172*b7482f52SPhilipp Zabel };
173*b7482f52SPhilipp Zabel 
174*b7482f52SPhilipp Zabel static const struct soc_enum uda1380_deemp_enum[] = {
175*b7482f52SPhilipp Zabel 	SOC_ENUM_SINGLE(UDA1380_DEEMP, 8, 5, uda1380_deemp),
176*b7482f52SPhilipp Zabel 	SOC_ENUM_SINGLE(UDA1380_DEEMP, 0, 5, uda1380_deemp),
177*b7482f52SPhilipp Zabel };
178*b7482f52SPhilipp Zabel static const struct soc_enum uda1380_input_sel_enum =
179*b7482f52SPhilipp Zabel 	SOC_ENUM_SINGLE(UDA1380_ADC, 2, 4, uda1380_input_sel);		/* SEL_MIC, SEL_LNA */
180*b7482f52SPhilipp Zabel static const struct soc_enum uda1380_output_sel_enum =
181*b7482f52SPhilipp Zabel 	SOC_ENUM_SINGLE(UDA1380_PM, 7, 2, uda1380_output_sel);		/* R02_EN_AVC */
182*b7482f52SPhilipp Zabel static const struct soc_enum uda1380_spf_enum =
183*b7482f52SPhilipp Zabel 	SOC_ENUM_SINGLE(UDA1380_MODE, 14, 4, uda1380_spf_mode);		/* M */
184*b7482f52SPhilipp Zabel static const struct soc_enum uda1380_capture_sel_enum =
185*b7482f52SPhilipp Zabel 	SOC_ENUM_SINGLE(UDA1380_IFACE, 6, 2, uda1380_capture_sel);	/* SEL_SOURCE */
186*b7482f52SPhilipp Zabel static const struct soc_enum uda1380_sel_ns_enum =
187*b7482f52SPhilipp Zabel 	SOC_ENUM_SINGLE(UDA1380_MIXER, 14, 2, uda1380_sel_ns);		/* SEL_NS */
188*b7482f52SPhilipp Zabel static const struct soc_enum uda1380_mix_enum =
189*b7482f52SPhilipp Zabel 	SOC_ENUM_SINGLE(UDA1380_MIXER, 12, 4, uda1380_mix_control);	/* MIX, MIX_POS */
190*b7482f52SPhilipp Zabel static const struct soc_enum uda1380_sdet_enum =
191*b7482f52SPhilipp Zabel 	SOC_ENUM_SINGLE(UDA1380_MIXER, 4, 4, uda1380_sdet_setting);	/* SD_VALUE */
192*b7482f52SPhilipp Zabel static const struct soc_enum uda1380_os_enum =
193*b7482f52SPhilipp Zabel 	SOC_ENUM_SINGLE(UDA1380_MIXER, 0, 3, uda1380_os_setting);	/* OS */
194*b7482f52SPhilipp Zabel 
195*b7482f52SPhilipp Zabel /*
196*b7482f52SPhilipp Zabel  * from -48 dB in 1.5 dB steps (mute instead of -49.5 dB)
197*b7482f52SPhilipp Zabel  */
198*b7482f52SPhilipp Zabel static DECLARE_TLV_DB_SCALE(amix_tlv, -4950, 150, 1);
199*b7482f52SPhilipp Zabel 
200*b7482f52SPhilipp Zabel /*
201*b7482f52SPhilipp Zabel  * from -78 dB in 1 dB steps (3 dB steps, really. LSB are ignored),
202*b7482f52SPhilipp Zabel  * from -66 dB in 0.5 dB steps (2 dB steps, really) and
203*b7482f52SPhilipp Zabel  * from -52 dB in 0.25 dB steps
204*b7482f52SPhilipp Zabel  */
205*b7482f52SPhilipp Zabel static const unsigned int mvol_tlv[] = {
206*b7482f52SPhilipp Zabel 	TLV_DB_RANGE_HEAD(3),
207*b7482f52SPhilipp Zabel 	0, 15, TLV_DB_SCALE_ITEM(-8200, 100, 1),
208*b7482f52SPhilipp Zabel 	16, 43, TLV_DB_SCALE_ITEM(-6600, 50, 0),
209*b7482f52SPhilipp Zabel 	44, 252, TLV_DB_SCALE_ITEM(-5200, 25, 0),
210*b7482f52SPhilipp Zabel };
211*b7482f52SPhilipp Zabel 
212*b7482f52SPhilipp Zabel /*
213*b7482f52SPhilipp Zabel  * from -72 dB in 1.5 dB steps (6 dB steps really),
214*b7482f52SPhilipp Zabel  * from -66 dB in 0.75 dB steps (3 dB steps really),
215*b7482f52SPhilipp Zabel  * from -60 dB in 0.5 dB steps (2 dB steps really) and
216*b7482f52SPhilipp Zabel  * from -46 dB in 0.25 dB steps
217*b7482f52SPhilipp Zabel  */
218*b7482f52SPhilipp Zabel static const unsigned int vc_tlv[] = {
219*b7482f52SPhilipp Zabel 	TLV_DB_RANGE_HEAD(4),
220*b7482f52SPhilipp Zabel 	0, 7, TLV_DB_SCALE_ITEM(-7800, 150, 1),
221*b7482f52SPhilipp Zabel 	8, 15, TLV_DB_SCALE_ITEM(-6600, 75, 0),
222*b7482f52SPhilipp Zabel 	16, 43, TLV_DB_SCALE_ITEM(-6000, 50, 0),
223*b7482f52SPhilipp Zabel 	44, 228, TLV_DB_SCALE_ITEM(-4600, 25, 0),
224*b7482f52SPhilipp Zabel };
225*b7482f52SPhilipp Zabel 
226*b7482f52SPhilipp Zabel /* from 0 to 6 dB in 2 dB steps if SPF mode != flat */
227*b7482f52SPhilipp Zabel static DECLARE_TLV_DB_SCALE(tr_tlv, 0, 200, 0);
228*b7482f52SPhilipp Zabel 
229*b7482f52SPhilipp Zabel /* from 0 to 24 dB in 2 dB steps, if SPF mode == maximum, otherwise cuts
230*b7482f52SPhilipp Zabel  * off at 18 dB max) */
231*b7482f52SPhilipp Zabel static DECLARE_TLV_DB_SCALE(bb_tlv, 0, 200, 0);
232*b7482f52SPhilipp Zabel 
233*b7482f52SPhilipp Zabel /* from -63 to 24 dB in 0.5 dB steps (-128...48) */
234*b7482f52SPhilipp Zabel static DECLARE_TLV_DB_SCALE(dec_tlv, -6400, 50, 1);
235*b7482f52SPhilipp Zabel 
236*b7482f52SPhilipp Zabel /* from 0 to 24 dB in 3 dB steps */
237*b7482f52SPhilipp Zabel static DECLARE_TLV_DB_SCALE(pga_tlv, 0, 300, 0);
238*b7482f52SPhilipp Zabel 
239*b7482f52SPhilipp Zabel /* from 0 to 30 dB in 2 dB steps */
240*b7482f52SPhilipp Zabel static DECLARE_TLV_DB_SCALE(vga_tlv, 0, 200, 0);
241*b7482f52SPhilipp Zabel 
242*b7482f52SPhilipp Zabel static const struct snd_kcontrol_new uda1380_snd_controls[] = {
243*b7482f52SPhilipp Zabel 	SOC_DOUBLE_TLV("Analog Mixer Volume", UDA1380_AMIX, 0, 8, 44, 1, amix_tlv),	/* AVCR, AVCL */
244*b7482f52SPhilipp Zabel 	SOC_DOUBLE_TLV("Master Playback Volume", UDA1380_MVOL, 0, 8, 252, 1, mvol_tlv),	/* MVCL, MVCR */
245*b7482f52SPhilipp Zabel 	SOC_SINGLE_TLV("ADC Playback Volume", UDA1380_MIXVOL, 8, 228, 1, vc_tlv),	/* VC2 */
246*b7482f52SPhilipp Zabel 	SOC_SINGLE_TLV("PCM Playback Volume", UDA1380_MIXVOL, 0, 228, 1, vc_tlv),	/* VC1 */
247*b7482f52SPhilipp Zabel 	SOC_ENUM("Sound Processing Filter", uda1380_spf_enum),				/* M */
248*b7482f52SPhilipp Zabel 	SOC_DOUBLE_TLV("Tone Control - Treble", UDA1380_MODE, 4, 12, 3, 0, tr_tlv), 	/* TRL, TRR */
249*b7482f52SPhilipp Zabel 	SOC_DOUBLE_TLV("Tone Control - Bass", UDA1380_MODE, 0, 8, 15, 0, bb_tlv),	/* BBL, BBR */
250*b7482f52SPhilipp Zabel /**/	SOC_SINGLE("Master Playback Switch", UDA1380_DEEMP, 14, 1, 1),		/* MTM */
251*b7482f52SPhilipp Zabel 	SOC_SINGLE("ADC Playback Switch", UDA1380_DEEMP, 11, 1, 1),		/* MT2 from decimation filter */
252*b7482f52SPhilipp Zabel 	SOC_ENUM("ADC Playback De-emphasis", uda1380_deemp_enum[0]),		/* DE2 */
253*b7482f52SPhilipp Zabel 	SOC_SINGLE("PCM Playback Switch", UDA1380_DEEMP, 3, 1, 1),		/* MT1, from digital data input */
254*b7482f52SPhilipp Zabel 	SOC_ENUM("PCM Playback De-emphasis", uda1380_deemp_enum[1]),		/* DE1 */
255*b7482f52SPhilipp Zabel 	SOC_SINGLE("DAC Polarity inverting Switch", UDA1380_MIXER, 15, 1, 0),	/* DA_POL_INV */
256*b7482f52SPhilipp Zabel 	SOC_ENUM("Noise Shaper", uda1380_sel_ns_enum),				/* SEL_NS */
257*b7482f52SPhilipp Zabel 	SOC_ENUM("Digital Mixer Signal Control", uda1380_mix_enum),		/* MIX_POS, MIX */
258*b7482f52SPhilipp Zabel 	SOC_SINGLE("Silence Switch", UDA1380_MIXER, 7, 1, 0),			/* SILENCE, force DAC output to silence */
259*b7482f52SPhilipp Zabel 	SOC_SINGLE("Silence Detector Switch", UDA1380_MIXER, 6, 1, 0),		/* SDET_ON */
260*b7482f52SPhilipp Zabel 	SOC_ENUM("Silence Detector Setting", uda1380_sdet_enum),		/* SD_VALUE */
261*b7482f52SPhilipp Zabel 	SOC_ENUM("Oversampling Input", uda1380_os_enum),			/* OS */
262*b7482f52SPhilipp Zabel 	SOC_DOUBLE_S8_TLV("ADC Capture Volume", UDA1380_DEC, -128, 48, dec_tlv),	/* ML_DEC, MR_DEC */
263*b7482f52SPhilipp Zabel /**/	SOC_SINGLE("ADC Capture Switch", UDA1380_PGA, 15, 1, 1),		/* MT_ADC */
264*b7482f52SPhilipp Zabel 	SOC_DOUBLE_TLV("Line Capture Volume", UDA1380_PGA, 0, 8, 8, 0, pga_tlv), /* PGA_GAINCTRLL, PGA_GAINCTRLR */
265*b7482f52SPhilipp Zabel 	SOC_SINGLE("ADC Polarity inverting Switch", UDA1380_ADC, 12, 1, 0),	/* ADCPOL_INV */
266*b7482f52SPhilipp Zabel 	SOC_SINGLE_TLV("Mic Capture Volume", UDA1380_ADC, 8, 15, 0, vga_tlv),	/* VGA_CTRL */
267*b7482f52SPhilipp Zabel 	SOC_SINGLE("DC Filter Bypass Switch", UDA1380_ADC, 1, 1, 0),		/* SKIP_DCFIL (before decimator) */
268*b7482f52SPhilipp Zabel 	SOC_SINGLE("DC Filter Enable Switch", UDA1380_ADC, 0, 1, 0),		/* EN_DCFIL (at output of decimator) */
269*b7482f52SPhilipp Zabel 	SOC_SINGLE("AGC Timing", UDA1380_AGC, 8, 7, 0),			/* TODO: enum, see table 62 */
270*b7482f52SPhilipp Zabel 	SOC_SINGLE("AGC Target level", UDA1380_AGC, 2, 3, 1),			/* AGC_LEVEL */
271*b7482f52SPhilipp Zabel 	/* -5.5, -8, -11.5, -14 dBFS */
272*b7482f52SPhilipp Zabel 	SOC_SINGLE("AGC Switch", UDA1380_AGC, 0, 1, 0),
273*b7482f52SPhilipp Zabel };
274*b7482f52SPhilipp Zabel 
275*b7482f52SPhilipp Zabel /* add non dapm controls */
276*b7482f52SPhilipp Zabel static int uda1380_add_controls(struct snd_soc_codec *codec)
277*b7482f52SPhilipp Zabel {
278*b7482f52SPhilipp Zabel 	int err, i;
279*b7482f52SPhilipp Zabel 
280*b7482f52SPhilipp Zabel 	for (i = 0; i < ARRAY_SIZE(uda1380_snd_controls); i++) {
281*b7482f52SPhilipp Zabel 		err = snd_ctl_add(codec->card,
282*b7482f52SPhilipp Zabel 			snd_soc_cnew(&uda1380_snd_controls[i], codec, NULL));
283*b7482f52SPhilipp Zabel 		if (err < 0)
284*b7482f52SPhilipp Zabel 			return err;
285*b7482f52SPhilipp Zabel 	}
286*b7482f52SPhilipp Zabel 
287*b7482f52SPhilipp Zabel 	return 0;
288*b7482f52SPhilipp Zabel }
289*b7482f52SPhilipp Zabel 
290*b7482f52SPhilipp Zabel /* Input mux */
291*b7482f52SPhilipp Zabel static const struct snd_kcontrol_new uda1380_input_mux_control =
292*b7482f52SPhilipp Zabel 	SOC_DAPM_ENUM("Route", uda1380_input_sel_enum);
293*b7482f52SPhilipp Zabel 
294*b7482f52SPhilipp Zabel /* Output mux */
295*b7482f52SPhilipp Zabel static const struct snd_kcontrol_new uda1380_output_mux_control =
296*b7482f52SPhilipp Zabel 	SOC_DAPM_ENUM("Route", uda1380_output_sel_enum);
297*b7482f52SPhilipp Zabel 
298*b7482f52SPhilipp Zabel /* Capture mux */
299*b7482f52SPhilipp Zabel static const struct snd_kcontrol_new uda1380_capture_mux_control =
300*b7482f52SPhilipp Zabel 	SOC_DAPM_ENUM("Route", uda1380_capture_sel_enum);
301*b7482f52SPhilipp Zabel 
302*b7482f52SPhilipp Zabel 
303*b7482f52SPhilipp Zabel static const struct snd_soc_dapm_widget uda1380_dapm_widgets[] = {
304*b7482f52SPhilipp Zabel 	SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0,
305*b7482f52SPhilipp Zabel 		&uda1380_input_mux_control),
306*b7482f52SPhilipp Zabel 	SND_SOC_DAPM_MUX("Output Mux", SND_SOC_NOPM, 0, 0,
307*b7482f52SPhilipp Zabel 		&uda1380_output_mux_control),
308*b7482f52SPhilipp Zabel 	SND_SOC_DAPM_MUX("Capture Mux", SND_SOC_NOPM, 0, 0,
309*b7482f52SPhilipp Zabel 		&uda1380_capture_mux_control),
310*b7482f52SPhilipp Zabel 	SND_SOC_DAPM_PGA("Left PGA", UDA1380_PM, 3, 0, NULL, 0),
311*b7482f52SPhilipp Zabel 	SND_SOC_DAPM_PGA("Right PGA", UDA1380_PM, 1, 0, NULL, 0),
312*b7482f52SPhilipp Zabel 	SND_SOC_DAPM_PGA("Mic LNA", UDA1380_PM, 4, 0, NULL, 0),
313*b7482f52SPhilipp Zabel 	SND_SOC_DAPM_ADC("Left ADC", "Left Capture", UDA1380_PM, 2, 0),
314*b7482f52SPhilipp Zabel 	SND_SOC_DAPM_ADC("Right ADC", "Right Capture", UDA1380_PM, 0, 0),
315*b7482f52SPhilipp Zabel 	SND_SOC_DAPM_INPUT("VINM"),
316*b7482f52SPhilipp Zabel 	SND_SOC_DAPM_INPUT("VINL"),
317*b7482f52SPhilipp Zabel 	SND_SOC_DAPM_INPUT("VINR"),
318*b7482f52SPhilipp Zabel 	SND_SOC_DAPM_MIXER("Analog Mixer", UDA1380_PM, 6, 0, NULL, 0),
319*b7482f52SPhilipp Zabel 	SND_SOC_DAPM_OUTPUT("VOUTLHP"),
320*b7482f52SPhilipp Zabel 	SND_SOC_DAPM_OUTPUT("VOUTRHP"),
321*b7482f52SPhilipp Zabel 	SND_SOC_DAPM_OUTPUT("VOUTL"),
322*b7482f52SPhilipp Zabel 	SND_SOC_DAPM_OUTPUT("VOUTR"),
323*b7482f52SPhilipp Zabel 	SND_SOC_DAPM_DAC("DAC", "Playback", UDA1380_PM, 10, 0),
324*b7482f52SPhilipp Zabel 	SND_SOC_DAPM_PGA("HeadPhone Driver", UDA1380_PM, 13, 0, NULL, 0),
325*b7482f52SPhilipp Zabel };
326*b7482f52SPhilipp Zabel 
327*b7482f52SPhilipp Zabel static const struct snd_soc_dapm_route audio_map[] = {
328*b7482f52SPhilipp Zabel 
329*b7482f52SPhilipp Zabel 	/* output mux */
330*b7482f52SPhilipp Zabel 	{"HeadPhone Driver", NULL, "Output Mux"},
331*b7482f52SPhilipp Zabel 	{"VOUTR", NULL, "Output Mux"},
332*b7482f52SPhilipp Zabel 	{"VOUTL", NULL, "Output Mux"},
333*b7482f52SPhilipp Zabel 
334*b7482f52SPhilipp Zabel 	{"Analog Mixer", NULL, "VINR"},
335*b7482f52SPhilipp Zabel 	{"Analog Mixer", NULL, "VINL"},
336*b7482f52SPhilipp Zabel 	{"Analog Mixer", NULL, "DAC"},
337*b7482f52SPhilipp Zabel 
338*b7482f52SPhilipp Zabel 	{"Output Mux", "DAC", "DAC"},
339*b7482f52SPhilipp Zabel 	{"Output Mux", "Analog Mixer", "Analog Mixer"},
340*b7482f52SPhilipp Zabel 
341*b7482f52SPhilipp Zabel 	/* {"DAC", "Digital Mixer", "I2S" } */
342*b7482f52SPhilipp Zabel 
343*b7482f52SPhilipp Zabel 	/* headphone driver */
344*b7482f52SPhilipp Zabel 	{"VOUTLHP", NULL, "HeadPhone Driver"},
345*b7482f52SPhilipp Zabel 	{"VOUTRHP", NULL, "HeadPhone Driver"},
346*b7482f52SPhilipp Zabel 
347*b7482f52SPhilipp Zabel 	/* input mux */
348*b7482f52SPhilipp Zabel 	{"Left ADC", NULL, "Input Mux"},
349*b7482f52SPhilipp Zabel 	{"Input Mux", "Mic", "Mic LNA"},
350*b7482f52SPhilipp Zabel 	{"Input Mux", "Mic + Line R", "Mic LNA"},
351*b7482f52SPhilipp Zabel 	{"Input Mux", "Line L", "Left PGA"},
352*b7482f52SPhilipp Zabel 	{"Input Mux", "Line", "Left PGA"},
353*b7482f52SPhilipp Zabel 
354*b7482f52SPhilipp Zabel 	/* right input */
355*b7482f52SPhilipp Zabel 	{"Right ADC", "Mic + Line R", "Right PGA"},
356*b7482f52SPhilipp Zabel 	{"Right ADC", "Line", "Right PGA"},
357*b7482f52SPhilipp Zabel 
358*b7482f52SPhilipp Zabel 	/* inputs */
359*b7482f52SPhilipp Zabel 	{"Mic LNA", NULL, "VINM"},
360*b7482f52SPhilipp Zabel 	{"Left PGA", NULL, "VINL"},
361*b7482f52SPhilipp Zabel 	{"Right PGA", NULL, "VINR"},
362*b7482f52SPhilipp Zabel };
363*b7482f52SPhilipp Zabel 
364*b7482f52SPhilipp Zabel static int uda1380_add_widgets(struct snd_soc_codec *codec)
365*b7482f52SPhilipp Zabel {
366*b7482f52SPhilipp Zabel 	snd_soc_dapm_new_controls(codec, uda1380_dapm_widgets,
367*b7482f52SPhilipp Zabel 				  ARRAY_SIZE(uda1380_dapm_widgets));
368*b7482f52SPhilipp Zabel 
369*b7482f52SPhilipp Zabel 	snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
370*b7482f52SPhilipp Zabel 
371*b7482f52SPhilipp Zabel 	snd_soc_dapm_new_widgets(codec);
372*b7482f52SPhilipp Zabel 	return 0;
373*b7482f52SPhilipp Zabel }
374*b7482f52SPhilipp Zabel 
375*b7482f52SPhilipp Zabel static int uda1380_set_dai_fmt(struct snd_soc_codec_dai *codec_dai,
376*b7482f52SPhilipp Zabel 		unsigned int fmt)
377*b7482f52SPhilipp Zabel {
378*b7482f52SPhilipp Zabel 	struct snd_soc_codec *codec = codec_dai->codec;
379*b7482f52SPhilipp Zabel 	int iface;
380*b7482f52SPhilipp Zabel 
381*b7482f52SPhilipp Zabel 	/* set up DAI based upon fmt */
382*b7482f52SPhilipp Zabel 	iface = uda1380_read_reg_cache(codec, UDA1380_IFACE);
383*b7482f52SPhilipp Zabel 	iface &= ~(R01_SFORI_MASK | R01_SIM | R01_SFORO_MASK);
384*b7482f52SPhilipp Zabel 
385*b7482f52SPhilipp Zabel 	/* FIXME: how to select I2S for DATAO and MSB for DATAI correctly? */
386*b7482f52SPhilipp Zabel 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
387*b7482f52SPhilipp Zabel 	case SND_SOC_DAIFMT_I2S:
388*b7482f52SPhilipp Zabel 		iface |= R01_SFORI_I2S | R01_SFORO_I2S;
389*b7482f52SPhilipp Zabel 		break;
390*b7482f52SPhilipp Zabel 	case SND_SOC_DAIFMT_LSB:
391*b7482f52SPhilipp Zabel 		iface |= R01_SFORI_LSB16 | R01_SFORO_I2S;
392*b7482f52SPhilipp Zabel 		break;
393*b7482f52SPhilipp Zabel 	case SND_SOC_DAIFMT_MSB:
394*b7482f52SPhilipp Zabel 		iface |= R01_SFORI_MSB | R01_SFORO_I2S;
395*b7482f52SPhilipp Zabel 	}
396*b7482f52SPhilipp Zabel 
397*b7482f52SPhilipp Zabel 	if ((fmt & SND_SOC_DAIFMT_MASTER_MASK) == SND_SOC_DAIFMT_CBM_CFM)
398*b7482f52SPhilipp Zabel 		iface |= R01_SIM;
399*b7482f52SPhilipp Zabel 
400*b7482f52SPhilipp Zabel 	uda1380_write(codec, UDA1380_IFACE, iface);
401*b7482f52SPhilipp Zabel 
402*b7482f52SPhilipp Zabel 	return 0;
403*b7482f52SPhilipp Zabel }
404*b7482f52SPhilipp Zabel 
405*b7482f52SPhilipp Zabel /*
406*b7482f52SPhilipp Zabel  * Flush reg cache
407*b7482f52SPhilipp Zabel  * We can only write the interpolator and decimator registers
408*b7482f52SPhilipp Zabel  * when the DAI is being clocked by the CPU DAI. It's up to the
409*b7482f52SPhilipp Zabel  * machine and cpu DAI driver to do this before we are called.
410*b7482f52SPhilipp Zabel  */
411*b7482f52SPhilipp Zabel static int uda1380_pcm_prepare(struct snd_pcm_substream *substream)
412*b7482f52SPhilipp Zabel {
413*b7482f52SPhilipp Zabel 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
414*b7482f52SPhilipp Zabel 	struct snd_soc_device *socdev = rtd->socdev;
415*b7482f52SPhilipp Zabel 	struct snd_soc_codec *codec = socdev->codec;
416*b7482f52SPhilipp Zabel 	int reg, reg_start, reg_end, clk;
417*b7482f52SPhilipp Zabel 
418*b7482f52SPhilipp Zabel 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
419*b7482f52SPhilipp Zabel 		reg_start = UDA1380_MVOL;
420*b7482f52SPhilipp Zabel 		reg_end = UDA1380_MIXER;
421*b7482f52SPhilipp Zabel 	} else {
422*b7482f52SPhilipp Zabel 		reg_start = UDA1380_DEC;
423*b7482f52SPhilipp Zabel 		reg_end = UDA1380_AGC;
424*b7482f52SPhilipp Zabel 	}
425*b7482f52SPhilipp Zabel 
426*b7482f52SPhilipp Zabel 	/* FIXME disable DAC_CLK */
427*b7482f52SPhilipp Zabel 	clk = uda1380_read_reg_cache(codec, UDA1380_CLK);
428*b7482f52SPhilipp Zabel 	uda1380_write(codec, UDA1380_CLK, clk & ~R00_DAC_CLK);
429*b7482f52SPhilipp Zabel 
430*b7482f52SPhilipp Zabel 	for (reg = reg_start; reg <= reg_end; reg++) {
431*b7482f52SPhilipp Zabel 		pr_debug("uda1380: flush reg %x val %x:", reg,
432*b7482f52SPhilipp Zabel 				uda1380_read_reg_cache(codec, reg));
433*b7482f52SPhilipp Zabel 		uda1380_write(codec, reg, uda1380_read_reg_cache(codec, reg));
434*b7482f52SPhilipp Zabel 	}
435*b7482f52SPhilipp Zabel 
436*b7482f52SPhilipp Zabel 	/* FIXME enable DAC_CLK */
437*b7482f52SPhilipp Zabel 	uda1380_write(codec, UDA1380_CLK, clk | R00_DAC_CLK);
438*b7482f52SPhilipp Zabel 
439*b7482f52SPhilipp Zabel 	return 0;
440*b7482f52SPhilipp Zabel }
441*b7482f52SPhilipp Zabel 
442*b7482f52SPhilipp Zabel static int uda1380_pcm_hw_params(struct snd_pcm_substream *substream,
443*b7482f52SPhilipp Zabel 	struct snd_pcm_hw_params *params)
444*b7482f52SPhilipp Zabel {
445*b7482f52SPhilipp Zabel 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
446*b7482f52SPhilipp Zabel 	struct snd_soc_device *socdev = rtd->socdev;
447*b7482f52SPhilipp Zabel 	struct snd_soc_codec *codec = socdev->codec;
448*b7482f52SPhilipp Zabel 	u16 clk = uda1380_read_reg_cache(codec, UDA1380_CLK);
449*b7482f52SPhilipp Zabel 
450*b7482f52SPhilipp Zabel 	/* set WSPLL power and divider if running from this clock */
451*b7482f52SPhilipp Zabel 	if (clk & R00_DAC_CLK) {
452*b7482f52SPhilipp Zabel 		int rate = params_rate(params);
453*b7482f52SPhilipp Zabel 		u16 pm = uda1380_read_reg_cache(codec, UDA1380_PM);
454*b7482f52SPhilipp Zabel 		clk &= ~0x3; /* clear SEL_LOOP_DIV */
455*b7482f52SPhilipp Zabel 		switch (rate) {
456*b7482f52SPhilipp Zabel 		case 6250 ... 12500:
457*b7482f52SPhilipp Zabel 			clk |= 0x0;
458*b7482f52SPhilipp Zabel 			break;
459*b7482f52SPhilipp Zabel 		case 12501 ... 25000:
460*b7482f52SPhilipp Zabel 			clk |= 0x1;
461*b7482f52SPhilipp Zabel 			break;
462*b7482f52SPhilipp Zabel 		case 25001 ... 50000:
463*b7482f52SPhilipp Zabel 			clk |= 0x2;
464*b7482f52SPhilipp Zabel 			break;
465*b7482f52SPhilipp Zabel 		case 50001 ... 100000:
466*b7482f52SPhilipp Zabel 			clk |= 0x3;
467*b7482f52SPhilipp Zabel 			break;
468*b7482f52SPhilipp Zabel 		}
469*b7482f52SPhilipp Zabel 		uda1380_write(codec, UDA1380_PM, R02_PON_PLL | pm);
470*b7482f52SPhilipp Zabel 	}
471*b7482f52SPhilipp Zabel 
472*b7482f52SPhilipp Zabel 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
473*b7482f52SPhilipp Zabel 		clk |= R00_EN_DAC | R00_EN_INT;
474*b7482f52SPhilipp Zabel 	else
475*b7482f52SPhilipp Zabel 		clk |= R00_EN_ADC | R00_EN_DEC;
476*b7482f52SPhilipp Zabel 
477*b7482f52SPhilipp Zabel 	uda1380_write(codec, UDA1380_CLK, clk);
478*b7482f52SPhilipp Zabel 	return 0;
479*b7482f52SPhilipp Zabel }
480*b7482f52SPhilipp Zabel 
481*b7482f52SPhilipp Zabel static void uda1380_pcm_shutdown(struct snd_pcm_substream *substream)
482*b7482f52SPhilipp Zabel {
483*b7482f52SPhilipp Zabel 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
484*b7482f52SPhilipp Zabel 	struct snd_soc_device *socdev = rtd->socdev;
485*b7482f52SPhilipp Zabel 	struct snd_soc_codec *codec = socdev->codec;
486*b7482f52SPhilipp Zabel 	u16 clk = uda1380_read_reg_cache(codec, UDA1380_CLK);
487*b7482f52SPhilipp Zabel 
488*b7482f52SPhilipp Zabel 	/* shut down WSPLL power if running from this clock */
489*b7482f52SPhilipp Zabel 	if (clk & R00_DAC_CLK) {
490*b7482f52SPhilipp Zabel 		u16 pm = uda1380_read_reg_cache(codec, UDA1380_PM);
491*b7482f52SPhilipp Zabel 		uda1380_write(codec, UDA1380_PM, ~R02_PON_PLL & pm);
492*b7482f52SPhilipp Zabel 	}
493*b7482f52SPhilipp Zabel 
494*b7482f52SPhilipp Zabel 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
495*b7482f52SPhilipp Zabel 		clk &= ~(R00_EN_DAC | R00_EN_INT);
496*b7482f52SPhilipp Zabel 	else
497*b7482f52SPhilipp Zabel 		clk &= ~(R00_EN_ADC | R00_EN_DEC);
498*b7482f52SPhilipp Zabel 
499*b7482f52SPhilipp Zabel 	uda1380_write(codec, UDA1380_CLK, clk);
500*b7482f52SPhilipp Zabel }
501*b7482f52SPhilipp Zabel 
502*b7482f52SPhilipp Zabel static int uda1380_mute(struct snd_soc_codec_dai *codec_dai, int mute)
503*b7482f52SPhilipp Zabel {
504*b7482f52SPhilipp Zabel 	struct snd_soc_codec *codec = codec_dai->codec;
505*b7482f52SPhilipp Zabel 	u16 mute_reg = uda1380_read_reg_cache(codec, UDA1380_DEEMP) & ~R13_MTM;
506*b7482f52SPhilipp Zabel 
507*b7482f52SPhilipp Zabel 	/* FIXME: mute(codec,0) is called when the magician clock is already
508*b7482f52SPhilipp Zabel 	 * set to WSPLL, but for some unknown reason writing to interpolator
509*b7482f52SPhilipp Zabel 	 * registers works only when clocked by SYSCLK */
510*b7482f52SPhilipp Zabel 	u16 clk = uda1380_read_reg_cache(codec, UDA1380_CLK);
511*b7482f52SPhilipp Zabel 	uda1380_write(codec, UDA1380_CLK, ~R00_DAC_CLK & clk);
512*b7482f52SPhilipp Zabel 	if (mute)
513*b7482f52SPhilipp Zabel 		uda1380_write(codec, UDA1380_DEEMP, mute_reg | R13_MTM);
514*b7482f52SPhilipp Zabel 	else
515*b7482f52SPhilipp Zabel 		uda1380_write(codec, UDA1380_DEEMP, mute_reg);
516*b7482f52SPhilipp Zabel 	uda1380_write(codec, UDA1380_CLK, clk);
517*b7482f52SPhilipp Zabel 	return 0;
518*b7482f52SPhilipp Zabel }
519*b7482f52SPhilipp Zabel 
520*b7482f52SPhilipp Zabel static int uda1380_set_bias_level(struct snd_soc_codec *codec,
521*b7482f52SPhilipp Zabel 	enum snd_soc_bias_level level)
522*b7482f52SPhilipp Zabel {
523*b7482f52SPhilipp Zabel 	int pm = uda1380_read_reg_cache(codec, UDA1380_PM);
524*b7482f52SPhilipp Zabel 
525*b7482f52SPhilipp Zabel 	switch (level) {
526*b7482f52SPhilipp Zabel 	case SND_SOC_BIAS_ON:
527*b7482f52SPhilipp Zabel 	case SND_SOC_BIAS_PREPARE:
528*b7482f52SPhilipp Zabel 		uda1380_write(codec, UDA1380_PM, R02_PON_BIAS | pm);
529*b7482f52SPhilipp Zabel 		break;
530*b7482f52SPhilipp Zabel 	case SND_SOC_BIAS_STANDBY:
531*b7482f52SPhilipp Zabel 		uda1380_write(codec, UDA1380_PM, R02_PON_BIAS);
532*b7482f52SPhilipp Zabel 		break;
533*b7482f52SPhilipp Zabel 	case SND_SOC_BIAS_OFF:
534*b7482f52SPhilipp Zabel 		uda1380_write(codec, UDA1380_PM, 0x0);
535*b7482f52SPhilipp Zabel 		break;
536*b7482f52SPhilipp Zabel 	}
537*b7482f52SPhilipp Zabel 	codec->bias_level = level;
538*b7482f52SPhilipp Zabel 	return 0;
539*b7482f52SPhilipp Zabel }
540*b7482f52SPhilipp Zabel 
541*b7482f52SPhilipp Zabel #define UDA1380_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
542*b7482f52SPhilipp Zabel 		       SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
543*b7482f52SPhilipp Zabel 		       SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
544*b7482f52SPhilipp Zabel 
545*b7482f52SPhilipp Zabel struct snd_soc_codec_dai uda1380_dai[] = {
546*b7482f52SPhilipp Zabel {
547*b7482f52SPhilipp Zabel 	.name = "UDA1380",
548*b7482f52SPhilipp Zabel 	.playback = {
549*b7482f52SPhilipp Zabel 		.stream_name = "Playback",
550*b7482f52SPhilipp Zabel 		.channels_min = 1,
551*b7482f52SPhilipp Zabel 		.channels_max = 2,
552*b7482f52SPhilipp Zabel 		.rates = UDA1380_RATES,
553*b7482f52SPhilipp Zabel 		.formats = SNDRV_PCM_FMTBIT_S16_LE,},
554*b7482f52SPhilipp Zabel 	.capture = {
555*b7482f52SPhilipp Zabel 		.stream_name = "Capture",
556*b7482f52SPhilipp Zabel 		.channels_min = 1,
557*b7482f52SPhilipp Zabel 		.channels_max = 2,
558*b7482f52SPhilipp Zabel 		.rates = UDA1380_RATES,
559*b7482f52SPhilipp Zabel 		.formats = SNDRV_PCM_FMTBIT_S16_LE,},
560*b7482f52SPhilipp Zabel 	.ops = {
561*b7482f52SPhilipp Zabel 		.hw_params = uda1380_pcm_hw_params,
562*b7482f52SPhilipp Zabel 		.shutdown = uda1380_pcm_shutdown,
563*b7482f52SPhilipp Zabel 		.prepare = uda1380_pcm_prepare,
564*b7482f52SPhilipp Zabel 	},
565*b7482f52SPhilipp Zabel 	.dai_ops = {
566*b7482f52SPhilipp Zabel 		.digital_mute = uda1380_mute,
567*b7482f52SPhilipp Zabel 		.set_fmt = uda1380_set_dai_fmt,
568*b7482f52SPhilipp Zabel 	},
569*b7482f52SPhilipp Zabel },
570*b7482f52SPhilipp Zabel { /* playback only - dual interface */
571*b7482f52SPhilipp Zabel 	.name = "UDA1380",
572*b7482f52SPhilipp Zabel 	.playback = {
573*b7482f52SPhilipp Zabel 		.stream_name = "Playback",
574*b7482f52SPhilipp Zabel 		.channels_min = 1,
575*b7482f52SPhilipp Zabel 		.channels_max = 2,
576*b7482f52SPhilipp Zabel 		.rates = UDA1380_RATES,
577*b7482f52SPhilipp Zabel 		.formats = SNDRV_PCM_FMTBIT_S16_LE,
578*b7482f52SPhilipp Zabel 	},
579*b7482f52SPhilipp Zabel 	.ops = {
580*b7482f52SPhilipp Zabel 		.hw_params = uda1380_pcm_hw_params,
581*b7482f52SPhilipp Zabel 		.shutdown = uda1380_pcm_shutdown,
582*b7482f52SPhilipp Zabel 		.prepare = uda1380_pcm_prepare,
583*b7482f52SPhilipp Zabel 	},
584*b7482f52SPhilipp Zabel 	.dai_ops = {
585*b7482f52SPhilipp Zabel 		.digital_mute = uda1380_mute,
586*b7482f52SPhilipp Zabel 		.set_fmt = uda1380_set_dai_fmt,
587*b7482f52SPhilipp Zabel 	},
588*b7482f52SPhilipp Zabel },
589*b7482f52SPhilipp Zabel { /* capture only - dual interface*/
590*b7482f52SPhilipp Zabel 	.name = "UDA1380",
591*b7482f52SPhilipp Zabel 	.capture = {
592*b7482f52SPhilipp Zabel 		.stream_name = "Capture",
593*b7482f52SPhilipp Zabel 		.channels_min = 1,
594*b7482f52SPhilipp Zabel 		.channels_max = 2,
595*b7482f52SPhilipp Zabel 		.rates = UDA1380_RATES,
596*b7482f52SPhilipp Zabel 		.formats = SNDRV_PCM_FMTBIT_S16_LE,
597*b7482f52SPhilipp Zabel 	},
598*b7482f52SPhilipp Zabel 	.ops = {
599*b7482f52SPhilipp Zabel 		.hw_params = uda1380_pcm_hw_params,
600*b7482f52SPhilipp Zabel 		.shutdown = uda1380_pcm_shutdown,
601*b7482f52SPhilipp Zabel 		.prepare = uda1380_pcm_prepare,
602*b7482f52SPhilipp Zabel 	},
603*b7482f52SPhilipp Zabel 	.dai_ops = {
604*b7482f52SPhilipp Zabel 		.set_fmt = uda1380_set_dai_fmt,
605*b7482f52SPhilipp Zabel 	},
606*b7482f52SPhilipp Zabel },
607*b7482f52SPhilipp Zabel };
608*b7482f52SPhilipp Zabel EXPORT_SYMBOL_GPL(uda1380_dai);
609*b7482f52SPhilipp Zabel 
610*b7482f52SPhilipp Zabel static int uda1380_suspend(struct platform_device *pdev, pm_message_t state)
611*b7482f52SPhilipp Zabel {
612*b7482f52SPhilipp Zabel 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
613*b7482f52SPhilipp Zabel 	struct snd_soc_codec *codec = socdev->codec;
614*b7482f52SPhilipp Zabel 
615*b7482f52SPhilipp Zabel 	uda1380_set_bias_level(codec, SND_SOC_BIAS_OFF);
616*b7482f52SPhilipp Zabel 	return 0;
617*b7482f52SPhilipp Zabel }
618*b7482f52SPhilipp Zabel 
619*b7482f52SPhilipp Zabel static int uda1380_resume(struct platform_device *pdev)
620*b7482f52SPhilipp Zabel {
621*b7482f52SPhilipp Zabel 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
622*b7482f52SPhilipp Zabel 	struct snd_soc_codec *codec = socdev->codec;
623*b7482f52SPhilipp Zabel 	int i;
624*b7482f52SPhilipp Zabel 	u8 data[2];
625*b7482f52SPhilipp Zabel 	u16 *cache = codec->reg_cache;
626*b7482f52SPhilipp Zabel 
627*b7482f52SPhilipp Zabel 	/* Sync reg_cache with the hardware */
628*b7482f52SPhilipp Zabel 	for (i = 0; i < ARRAY_SIZE(uda1380_reg); i++) {
629*b7482f52SPhilipp Zabel 		data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
630*b7482f52SPhilipp Zabel 		data[1] = cache[i] & 0x00ff;
631*b7482f52SPhilipp Zabel 		codec->hw_write(codec->control_data, data, 2);
632*b7482f52SPhilipp Zabel 	}
633*b7482f52SPhilipp Zabel 	uda1380_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
634*b7482f52SPhilipp Zabel 	uda1380_set_bias_level(codec, codec->suspend_bias_level);
635*b7482f52SPhilipp Zabel 	return 0;
636*b7482f52SPhilipp Zabel }
637*b7482f52SPhilipp Zabel 
638*b7482f52SPhilipp Zabel /*
639*b7482f52SPhilipp Zabel  * initialise the UDA1380 driver
640*b7482f52SPhilipp Zabel  * register mixer and dsp interfaces with the kernel
641*b7482f52SPhilipp Zabel  */
642*b7482f52SPhilipp Zabel static int uda1380_init(struct snd_soc_device *socdev, int dac_clk)
643*b7482f52SPhilipp Zabel {
644*b7482f52SPhilipp Zabel 	struct snd_soc_codec *codec = socdev->codec;
645*b7482f52SPhilipp Zabel 	int ret = 0;
646*b7482f52SPhilipp Zabel 
647*b7482f52SPhilipp Zabel 	codec->name = "UDA1380";
648*b7482f52SPhilipp Zabel 	codec->owner = THIS_MODULE;
649*b7482f52SPhilipp Zabel 	codec->read = uda1380_read_reg_cache;
650*b7482f52SPhilipp Zabel 	codec->write = uda1380_write;
651*b7482f52SPhilipp Zabel 	codec->set_bias_level = uda1380_set_bias_level;
652*b7482f52SPhilipp Zabel 	codec->dai = uda1380_dai;
653*b7482f52SPhilipp Zabel 	codec->num_dai = ARRAY_SIZE(uda1380_dai);
654*b7482f52SPhilipp Zabel 	codec->reg_cache = kmemdup(uda1380_reg, sizeof(uda1380_reg),
655*b7482f52SPhilipp Zabel 				   GFP_KERNEL);
656*b7482f52SPhilipp Zabel 	if (codec->reg_cache == NULL)
657*b7482f52SPhilipp Zabel 		return -ENOMEM;
658*b7482f52SPhilipp Zabel 	codec->reg_cache_size = sizeof(uda1380_reg);
659*b7482f52SPhilipp Zabel 	codec->reg_cache_step = 2;
660*b7482f52SPhilipp Zabel 	uda1380_reset(codec);
661*b7482f52SPhilipp Zabel 
662*b7482f52SPhilipp Zabel 	/* register pcms */
663*b7482f52SPhilipp Zabel 	ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
664*b7482f52SPhilipp Zabel 	if (ret < 0) {
665*b7482f52SPhilipp Zabel 		pr_err("uda1380: failed to create pcms\n");
666*b7482f52SPhilipp Zabel 		goto pcm_err;
667*b7482f52SPhilipp Zabel 	}
668*b7482f52SPhilipp Zabel 
669*b7482f52SPhilipp Zabel 	/* power on device */
670*b7482f52SPhilipp Zabel 	uda1380_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
671*b7482f52SPhilipp Zabel 	/* set clock input */
672*b7482f52SPhilipp Zabel 	switch (dac_clk) {
673*b7482f52SPhilipp Zabel 	case UDA1380_DAC_CLK_SYSCLK:
674*b7482f52SPhilipp Zabel 		uda1380_write(codec, UDA1380_CLK, 0);
675*b7482f52SPhilipp Zabel 		break;
676*b7482f52SPhilipp Zabel 	case UDA1380_DAC_CLK_WSPLL:
677*b7482f52SPhilipp Zabel 		uda1380_write(codec, UDA1380_CLK, R00_DAC_CLK);
678*b7482f52SPhilipp Zabel 		break;
679*b7482f52SPhilipp Zabel 	}
680*b7482f52SPhilipp Zabel 
681*b7482f52SPhilipp Zabel 	/* uda1380 init */
682*b7482f52SPhilipp Zabel 	uda1380_add_controls(codec);
683*b7482f52SPhilipp Zabel 	uda1380_add_widgets(codec);
684*b7482f52SPhilipp Zabel 	ret = snd_soc_register_card(socdev);
685*b7482f52SPhilipp Zabel 	if (ret < 0) {
686*b7482f52SPhilipp Zabel 		pr_err("uda1380: failed to register card\n");
687*b7482f52SPhilipp Zabel 		goto card_err;
688*b7482f52SPhilipp Zabel 	}
689*b7482f52SPhilipp Zabel 
690*b7482f52SPhilipp Zabel 	return ret;
691*b7482f52SPhilipp Zabel 
692*b7482f52SPhilipp Zabel card_err:
693*b7482f52SPhilipp Zabel 	snd_soc_free_pcms(socdev);
694*b7482f52SPhilipp Zabel 	snd_soc_dapm_free(socdev);
695*b7482f52SPhilipp Zabel pcm_err:
696*b7482f52SPhilipp Zabel 	kfree(codec->reg_cache);
697*b7482f52SPhilipp Zabel 	return ret;
698*b7482f52SPhilipp Zabel }
699*b7482f52SPhilipp Zabel 
700*b7482f52SPhilipp Zabel static struct snd_soc_device *uda1380_socdev;
701*b7482f52SPhilipp Zabel 
702*b7482f52SPhilipp Zabel #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
703*b7482f52SPhilipp Zabel 
704*b7482f52SPhilipp Zabel #define I2C_DRIVERID_UDA1380 0xfefe /* liam -  need a proper id */
705*b7482f52SPhilipp Zabel 
706*b7482f52SPhilipp Zabel static unsigned short normal_i2c[] = { 0, I2C_CLIENT_END };
707*b7482f52SPhilipp Zabel 
708*b7482f52SPhilipp Zabel /* Magic definition of all other variables and things */
709*b7482f52SPhilipp Zabel I2C_CLIENT_INSMOD;
710*b7482f52SPhilipp Zabel 
711*b7482f52SPhilipp Zabel static struct i2c_driver uda1380_i2c_driver;
712*b7482f52SPhilipp Zabel static struct i2c_client client_template;
713*b7482f52SPhilipp Zabel 
714*b7482f52SPhilipp Zabel /* If the i2c layer weren't so broken, we could pass this kind of data
715*b7482f52SPhilipp Zabel    around */
716*b7482f52SPhilipp Zabel 
717*b7482f52SPhilipp Zabel static int uda1380_codec_probe(struct i2c_adapter *adap, int addr, int kind)
718*b7482f52SPhilipp Zabel {
719*b7482f52SPhilipp Zabel 	struct snd_soc_device *socdev = uda1380_socdev;
720*b7482f52SPhilipp Zabel 	struct uda1380_setup_data *setup = socdev->codec_data;
721*b7482f52SPhilipp Zabel 	struct snd_soc_codec *codec = socdev->codec;
722*b7482f52SPhilipp Zabel 	struct i2c_client *i2c;
723*b7482f52SPhilipp Zabel 	int ret;
724*b7482f52SPhilipp Zabel 
725*b7482f52SPhilipp Zabel 	if (addr != setup->i2c_address)
726*b7482f52SPhilipp Zabel 		return -ENODEV;
727*b7482f52SPhilipp Zabel 
728*b7482f52SPhilipp Zabel 	client_template.adapter = adap;
729*b7482f52SPhilipp Zabel 	client_template.addr = addr;
730*b7482f52SPhilipp Zabel 
731*b7482f52SPhilipp Zabel 	i2c = kmemdup(&client_template, sizeof(client_template), GFP_KERNEL);
732*b7482f52SPhilipp Zabel 	if (i2c == NULL) {
733*b7482f52SPhilipp Zabel 		kfree(codec);
734*b7482f52SPhilipp Zabel 		return -ENOMEM;
735*b7482f52SPhilipp Zabel 	}
736*b7482f52SPhilipp Zabel 	i2c_set_clientdata(i2c, codec);
737*b7482f52SPhilipp Zabel 	codec->control_data = i2c;
738*b7482f52SPhilipp Zabel 
739*b7482f52SPhilipp Zabel 	ret = i2c_attach_client(i2c);
740*b7482f52SPhilipp Zabel 	if (ret < 0) {
741*b7482f52SPhilipp Zabel 		pr_err("uda1380: failed to attach codec at addr %x\n", addr);
742*b7482f52SPhilipp Zabel 		goto err;
743*b7482f52SPhilipp Zabel 	}
744*b7482f52SPhilipp Zabel 
745*b7482f52SPhilipp Zabel 	ret = uda1380_init(socdev, setup->dac_clk);
746*b7482f52SPhilipp Zabel 	if (ret < 0) {
747*b7482f52SPhilipp Zabel 		pr_err("uda1380: failed to initialise UDA1380\n");
748*b7482f52SPhilipp Zabel 		goto err;
749*b7482f52SPhilipp Zabel 	}
750*b7482f52SPhilipp Zabel 	return ret;
751*b7482f52SPhilipp Zabel 
752*b7482f52SPhilipp Zabel err:
753*b7482f52SPhilipp Zabel 	kfree(codec);
754*b7482f52SPhilipp Zabel 	kfree(i2c);
755*b7482f52SPhilipp Zabel 	return ret;
756*b7482f52SPhilipp Zabel }
757*b7482f52SPhilipp Zabel 
758*b7482f52SPhilipp Zabel static int uda1380_i2c_detach(struct i2c_client *client)
759*b7482f52SPhilipp Zabel {
760*b7482f52SPhilipp Zabel 	struct snd_soc_codec *codec = i2c_get_clientdata(client);
761*b7482f52SPhilipp Zabel 	i2c_detach_client(client);
762*b7482f52SPhilipp Zabel 	kfree(codec->reg_cache);
763*b7482f52SPhilipp Zabel 	kfree(client);
764*b7482f52SPhilipp Zabel 	return 0;
765*b7482f52SPhilipp Zabel }
766*b7482f52SPhilipp Zabel 
767*b7482f52SPhilipp Zabel static int uda1380_i2c_attach(struct i2c_adapter *adap)
768*b7482f52SPhilipp Zabel {
769*b7482f52SPhilipp Zabel 	return i2c_probe(adap, &addr_data, uda1380_codec_probe);
770*b7482f52SPhilipp Zabel }
771*b7482f52SPhilipp Zabel 
772*b7482f52SPhilipp Zabel static struct i2c_driver uda1380_i2c_driver = {
773*b7482f52SPhilipp Zabel 	.driver = {
774*b7482f52SPhilipp Zabel 		.name =  "UDA1380 I2C Codec",
775*b7482f52SPhilipp Zabel 		.owner = THIS_MODULE,
776*b7482f52SPhilipp Zabel 	},
777*b7482f52SPhilipp Zabel 	.id =             I2C_DRIVERID_UDA1380,
778*b7482f52SPhilipp Zabel 	.attach_adapter = uda1380_i2c_attach,
779*b7482f52SPhilipp Zabel 	.detach_client =  uda1380_i2c_detach,
780*b7482f52SPhilipp Zabel 	.command =        NULL,
781*b7482f52SPhilipp Zabel };
782*b7482f52SPhilipp Zabel 
783*b7482f52SPhilipp Zabel static struct i2c_client client_template = {
784*b7482f52SPhilipp Zabel 	.name =   "UDA1380",
785*b7482f52SPhilipp Zabel 	.driver = &uda1380_i2c_driver,
786*b7482f52SPhilipp Zabel };
787*b7482f52SPhilipp Zabel #endif
788*b7482f52SPhilipp Zabel 
789*b7482f52SPhilipp Zabel static int uda1380_probe(struct platform_device *pdev)
790*b7482f52SPhilipp Zabel {
791*b7482f52SPhilipp Zabel 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
792*b7482f52SPhilipp Zabel 	struct uda1380_setup_data *setup;
793*b7482f52SPhilipp Zabel 	struct snd_soc_codec *codec;
794*b7482f52SPhilipp Zabel 	int ret = 0;
795*b7482f52SPhilipp Zabel 
796*b7482f52SPhilipp Zabel 	pr_info("UDA1380 Audio Codec %s", UDA1380_VERSION);
797*b7482f52SPhilipp Zabel 
798*b7482f52SPhilipp Zabel 	setup = socdev->codec_data;
799*b7482f52SPhilipp Zabel 	codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
800*b7482f52SPhilipp Zabel 	if (codec == NULL)
801*b7482f52SPhilipp Zabel 		return -ENOMEM;
802*b7482f52SPhilipp Zabel 
803*b7482f52SPhilipp Zabel 	socdev->codec = codec;
804*b7482f52SPhilipp Zabel 	mutex_init(&codec->mutex);
805*b7482f52SPhilipp Zabel 	INIT_LIST_HEAD(&codec->dapm_widgets);
806*b7482f52SPhilipp Zabel 	INIT_LIST_HEAD(&codec->dapm_paths);
807*b7482f52SPhilipp Zabel 
808*b7482f52SPhilipp Zabel 	uda1380_socdev = socdev;
809*b7482f52SPhilipp Zabel #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
810*b7482f52SPhilipp Zabel 	if (setup->i2c_address) {
811*b7482f52SPhilipp Zabel 		normal_i2c[0] = setup->i2c_address;
812*b7482f52SPhilipp Zabel 		codec->hw_write = (hw_write_t)i2c_master_send;
813*b7482f52SPhilipp Zabel 		ret = i2c_add_driver(&uda1380_i2c_driver);
814*b7482f52SPhilipp Zabel 		if (ret != 0)
815*b7482f52SPhilipp Zabel 			printk(KERN_ERR "can't add i2c driver");
816*b7482f52SPhilipp Zabel 	}
817*b7482f52SPhilipp Zabel #else
818*b7482f52SPhilipp Zabel 	/* Add other interfaces here */
819*b7482f52SPhilipp Zabel #endif
820*b7482f52SPhilipp Zabel 	return ret;
821*b7482f52SPhilipp Zabel }
822*b7482f52SPhilipp Zabel 
823*b7482f52SPhilipp Zabel /* power down chip */
824*b7482f52SPhilipp Zabel static int uda1380_remove(struct platform_device *pdev)
825*b7482f52SPhilipp Zabel {
826*b7482f52SPhilipp Zabel 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
827*b7482f52SPhilipp Zabel 	struct snd_soc_codec *codec = socdev->codec;
828*b7482f52SPhilipp Zabel 
829*b7482f52SPhilipp Zabel 	if (codec->control_data)
830*b7482f52SPhilipp Zabel 		uda1380_set_bias_level(codec, SND_SOC_BIAS_OFF);
831*b7482f52SPhilipp Zabel 
832*b7482f52SPhilipp Zabel 	snd_soc_free_pcms(socdev);
833*b7482f52SPhilipp Zabel 	snd_soc_dapm_free(socdev);
834*b7482f52SPhilipp Zabel #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
835*b7482f52SPhilipp Zabel 	i2c_del_driver(&uda1380_i2c_driver);
836*b7482f52SPhilipp Zabel #endif
837*b7482f52SPhilipp Zabel 	kfree(codec);
838*b7482f52SPhilipp Zabel 
839*b7482f52SPhilipp Zabel 	return 0;
840*b7482f52SPhilipp Zabel }
841*b7482f52SPhilipp Zabel 
842*b7482f52SPhilipp Zabel struct snd_soc_codec_device soc_codec_dev_uda1380 = {
843*b7482f52SPhilipp Zabel 	.probe = 	uda1380_probe,
844*b7482f52SPhilipp Zabel 	.remove = 	uda1380_remove,
845*b7482f52SPhilipp Zabel 	.suspend = 	uda1380_suspend,
846*b7482f52SPhilipp Zabel 	.resume =	uda1380_resume,
847*b7482f52SPhilipp Zabel };
848*b7482f52SPhilipp Zabel EXPORT_SYMBOL_GPL(soc_codec_dev_uda1380);
849*b7482f52SPhilipp Zabel 
850*b7482f52SPhilipp Zabel MODULE_AUTHOR("Giorgio Padrin");
851*b7482f52SPhilipp Zabel MODULE_DESCRIPTION("Audio support for codec Philips UDA1380");
852*b7482f52SPhilipp Zabel MODULE_LICENSE("GPL");
853