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