• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/linux/linux-2.6.36/sound/soc/codecs/
1/*
2 * DA7210 ALSA Soc codec driver
3 *
4 * Copyright (c) 2009 Dialog Semiconductor
5 * Written by David Chen <Dajun.chen@diasemi.com>
6 *
7 * Copyright (C) 2009 Renesas Solutions Corp.
8 * Cleanups by Kuninori Morimoto <morimoto.kuninori@renesas.com>
9 *
10 * Tested on SuperH Ecovec24 board with S16/S24 LE in 48KHz using I2S
11 *
12 * This program is free software; you can redistribute  it and/or modify it
13 * under  the terms of  the GNU General  Public License as published by the
14 * Free Software Foundation;  either version 2 of the  License, or (at your
15 * option) any later version.
16 */
17
18#include <linux/delay.h>
19#include <linux/i2c.h>
20#include <linux/platform_device.h>
21#include <linux/slab.h>
22#include <sound/pcm.h>
23#include <sound/pcm_params.h>
24#include <sound/soc-dapm.h>
25#include <sound/initval.h>
26#include <sound/tlv.h>
27
28#include "da7210.h"
29
30/* DA7210 register space */
31#define DA7210_STATUS			0x02
32#define DA7210_STARTUP1			0x03
33#define DA7210_MIC_L			0x07
34#define DA7210_MIC_R			0x08
35#define DA7210_INMIX_L			0x0D
36#define DA7210_INMIX_R			0x0E
37#define DA7210_ADC_HPF			0x0F
38#define DA7210_ADC			0x10
39#define DA7210_DAC_HPF			0x14
40#define DA7210_DAC_L			0x15
41#define DA7210_DAC_R			0x16
42#define DA7210_DAC_SEL			0x17
43#define DA7210_OUTMIX_L			0x1C
44#define DA7210_OUTMIX_R			0x1D
45#define DA7210_HP_L_VOL			0x21
46#define DA7210_HP_R_VOL			0x22
47#define DA7210_HP_CFG			0x23
48#define DA7210_DAI_SRC_SEL		0x25
49#define DA7210_DAI_CFG1			0x26
50#define DA7210_DAI_CFG3			0x28
51#define DA7210_PLL_DIV1			0x29
52#define DA7210_PLL_DIV2			0x2A
53#define DA7210_PLL_DIV3			0x2B
54#define DA7210_PLL			0x2C
55#define DA7210_A_HID_UNLOCK		0x8A
56#define DA7210_A_TEST_UNLOCK		0x8B
57#define DA7210_A_PLL1			0x90
58#define DA7210_A_CP_MODE		0xA7
59
60/* STARTUP1 bit fields */
61#define DA7210_SC_MST_EN		(1 << 0)
62
63/* MIC_L bit fields */
64#define DA7210_MICBIAS_EN		(1 << 6)
65#define DA7210_MIC_L_EN			(1 << 7)
66
67/* MIC_R bit fields */
68#define DA7210_MIC_R_EN			(1 << 7)
69
70/* INMIX_L bit fields */
71#define DA7210_IN_L_EN			(1 << 7)
72
73/* INMIX_R bit fields */
74#define DA7210_IN_R_EN			(1 << 7)
75
76/* ADC bit fields */
77#define DA7210_ADC_L_EN			(1 << 3)
78#define DA7210_ADC_R_EN			(1 << 7)
79
80/* DAC/ADC HPF fields */
81#define DA7210_VOICE_F0_MASK		(0x7 << 4)
82#define DA7210_VOICE_F0_25		(1 << 4)
83#define DA7210_VOICE_EN			(1 << 7)
84
85/* DAC_SEL bit fields */
86#define DA7210_DAC_L_SRC_DAI_L		(4 << 0)
87#define DA7210_DAC_L_EN			(1 << 3)
88#define DA7210_DAC_R_SRC_DAI_R		(5 << 4)
89#define DA7210_DAC_R_EN			(1 << 7)
90
91/* OUTMIX_L bit fields */
92#define DA7210_OUT_L_EN			(1 << 7)
93
94/* OUTMIX_R bit fields */
95#define DA7210_OUT_R_EN			(1 << 7)
96
97/* HP_CFG bit fields */
98#define DA7210_HP_2CAP_MODE		(1 << 1)
99#define DA7210_HP_SENSE_EN		(1 << 2)
100#define DA7210_HP_L_EN			(1 << 3)
101#define DA7210_HP_MODE			(1 << 6)
102#define DA7210_HP_R_EN			(1 << 7)
103
104/* DAI_SRC_SEL bit fields */
105#define DA7210_DAI_OUT_L_SRC		(6 << 0)
106#define DA7210_DAI_OUT_R_SRC		(7 << 4)
107
108/* DAI_CFG1 bit fields */
109#define DA7210_DAI_WORD_S16_LE		(0 << 0)
110#define DA7210_DAI_WORD_S24_LE		(2 << 0)
111#define DA7210_DAI_FLEN_64BIT		(1 << 2)
112#define DA7210_DAI_MODE_MASTER		(1 << 7)
113
114/* DAI_CFG3 bit fields */
115#define DA7210_DAI_FORMAT_I2SMODE	(0 << 0)
116#define DA7210_DAI_OE			(1 << 3)
117#define DA7210_DAI_EN			(1 << 7)
118
119/*PLL_DIV3 bit fields */
120#define DA7210_MCLK_RANGE_10_20_MHZ	(1 << 4)
121#define DA7210_PLL_BYP			(1 << 6)
122
123/* PLL bit fields */
124#define DA7210_PLL_FS_MASK		(0xF << 0)
125#define DA7210_PLL_FS_8000		(0x1 << 0)
126#define DA7210_PLL_FS_11025		(0x2 << 0)
127#define DA7210_PLL_FS_12000		(0x3 << 0)
128#define DA7210_PLL_FS_16000		(0x5 << 0)
129#define DA7210_PLL_FS_22050		(0x6 << 0)
130#define DA7210_PLL_FS_24000		(0x7 << 0)
131#define DA7210_PLL_FS_32000		(0x9 << 0)
132#define DA7210_PLL_FS_44100		(0xA << 0)
133#define DA7210_PLL_FS_48000		(0xB << 0)
134#define DA7210_PLL_FS_88200		(0xE << 0)
135#define DA7210_PLL_FS_96000		(0xF << 0)
136#define DA7210_PLL_EN			(0x1 << 7)
137
138#define DA7210_VERSION "0.0.1"
139
140static const DECLARE_TLV_DB_SCALE(hp_out_tlv, -7950, 150, 1);
141
142static const struct snd_kcontrol_new da7210_snd_controls[] = {
143
144	SOC_DOUBLE_R_TLV("HeadPhone Playback Volume",
145			 DA7210_HP_L_VOL, DA7210_HP_R_VOL,
146			 0, 0x3F, 0, hp_out_tlv),
147};
148
149/* Codec private data */
150struct da7210_priv {
151	struct snd_soc_codec codec;
152};
153
154static struct snd_soc_codec *da7210_codec;
155
156/*
157 * Register cache
158 */
159static const u8 da7210_reg[] = {
160	0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	/* R0  - R7  */
161	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,	/* R8  - RF  */
162	0x00, 0x00, 0x00, 0x00, 0x08, 0x10, 0x10, 0x54,	/* R10 - R17 */
163	0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	/* R18 - R1F */
164	0x00, 0x00, 0x00, 0x02, 0x00, 0x76, 0x00, 0x00,	/* R20 - R27 */
165	0x04, 0x00, 0x00, 0x30, 0x2A, 0x00, 0x40, 0x00,	/* R28 - R2F */
166	0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00,	/* R30 - R37 */
167	0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x00, 0x00,	/* R38 - R3F */
168	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	/* R40 - R4F */
169	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	/* R48 - R4F */
170	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	/* R50 - R57 */
171	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	/* R58 - R5F */
172	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	/* R60 - R67 */
173	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	/* R68 - R6F */
174	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	/* R70 - R77 */
175	0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x54, 0x00,	/* R78 - R7F */
176	0x00, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x00, 0x00,	/* R80 - R87 */
177	0x00,						/* R88       */
178};
179
180/*
181 * Read da7210 register cache
182 */
183static inline u32 da7210_read_reg_cache(struct snd_soc_codec *codec, u32 reg)
184{
185	u8 *cache = codec->reg_cache;
186	BUG_ON(reg >= ARRAY_SIZE(da7210_reg));
187	return cache[reg];
188}
189
190/*
191 * Write to the da7210 register space
192 */
193static int da7210_write(struct snd_soc_codec *codec, u32 reg, u32 value)
194{
195	u8 *cache = codec->reg_cache;
196	u8 data[2];
197
198	BUG_ON(codec->volatile_register);
199
200	data[0] = reg & 0xff;
201	data[1] = value & 0xff;
202
203	if (reg >= codec->reg_cache_size)
204		return -EIO;
205
206	if (2 != codec->hw_write(codec->control_data, data, 2))
207		return -EIO;
208
209	cache[reg] = value;
210	return 0;
211}
212
213/*
214 * Read from the da7210 register space.
215 */
216static inline u32 da7210_read(struct snd_soc_codec *codec, u32 reg)
217{
218	if (DA7210_STATUS == reg)
219		return i2c_smbus_read_byte_data(codec->control_data, reg);
220
221	return da7210_read_reg_cache(codec, reg);
222}
223
224static int da7210_startup(struct snd_pcm_substream *substream,
225			  struct snd_soc_dai *dai)
226{
227	int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
228	struct snd_soc_codec *codec = dai->codec;
229
230	if (is_play) {
231		/* Enable Out */
232		snd_soc_update_bits(codec, DA7210_OUTMIX_L, 0x1F, 0x10);
233		snd_soc_update_bits(codec, DA7210_OUTMIX_R, 0x1F, 0x10);
234
235	} else {
236		/* Volume 7 */
237		snd_soc_update_bits(codec, DA7210_MIC_L, 0x7, 0x7);
238		snd_soc_update_bits(codec, DA7210_MIC_R, 0x7, 0x7);
239
240		/* Enable Mic */
241		snd_soc_update_bits(codec, DA7210_INMIX_L, 0x1F, 0x1);
242		snd_soc_update_bits(codec, DA7210_INMIX_R, 0x1F, 0x1);
243	}
244
245	return 0;
246}
247
248/*
249 * Set PCM DAI word length.
250 */
251static int da7210_hw_params(struct snd_pcm_substream *substream,
252			    struct snd_pcm_hw_params *params,
253			    struct snd_soc_dai *dai)
254{
255	struct snd_soc_pcm_runtime *rtd = substream->private_data;
256	struct snd_soc_device *socdev = rtd->socdev;
257	struct snd_soc_codec *codec = socdev->card->codec;
258	u32 dai_cfg1;
259	u32 hpf_reg, hpf_mask, hpf_value;
260	u32 fs, bypass;
261
262	/* set DAI source to Left and Right ADC */
263	da7210_write(codec, DA7210_DAI_SRC_SEL,
264		     DA7210_DAI_OUT_R_SRC | DA7210_DAI_OUT_L_SRC);
265
266	/* Enable DAI */
267	da7210_write(codec, DA7210_DAI_CFG3, DA7210_DAI_OE | DA7210_DAI_EN);
268
269	dai_cfg1 = 0xFC & da7210_read(codec, DA7210_DAI_CFG1);
270
271	switch (params_format(params)) {
272	case SNDRV_PCM_FORMAT_S16_LE:
273		dai_cfg1 |= DA7210_DAI_WORD_S16_LE;
274		break;
275	case SNDRV_PCM_FORMAT_S24_LE:
276		dai_cfg1 |= DA7210_DAI_WORD_S24_LE;
277		break;
278	default:
279		return -EINVAL;
280	}
281
282	da7210_write(codec, DA7210_DAI_CFG1, dai_cfg1);
283
284	hpf_reg = (SNDRV_PCM_STREAM_PLAYBACK == substream->stream) ?
285		DA7210_DAC_HPF : DA7210_ADC_HPF;
286
287	switch (params_rate(params)) {
288	case 8000:
289		fs		= DA7210_PLL_FS_8000;
290		hpf_mask	= DA7210_VOICE_F0_MASK	| DA7210_VOICE_EN;
291		hpf_value	= DA7210_VOICE_F0_25	| DA7210_VOICE_EN;
292		bypass		= DA7210_PLL_BYP;
293		break;
294	case 11025:
295		fs		= DA7210_PLL_FS_11025;
296		hpf_mask	= DA7210_VOICE_F0_MASK	| DA7210_VOICE_EN;
297		hpf_value	= DA7210_VOICE_F0_25	| DA7210_VOICE_EN;
298		bypass		= 0;
299		break;
300	case 12000:
301		fs		= DA7210_PLL_FS_12000;
302		hpf_mask	= DA7210_VOICE_F0_MASK	| DA7210_VOICE_EN;
303		hpf_value	= DA7210_VOICE_F0_25	| DA7210_VOICE_EN;
304		bypass		= DA7210_PLL_BYP;
305		break;
306	case 16000:
307		fs		= DA7210_PLL_FS_16000;
308		hpf_mask	= DA7210_VOICE_F0_MASK	| DA7210_VOICE_EN;
309		hpf_value	= DA7210_VOICE_F0_25	| DA7210_VOICE_EN;
310		bypass		= DA7210_PLL_BYP;
311		break;
312	case 22050:
313		fs		= DA7210_PLL_FS_22050;
314		hpf_mask	= DA7210_VOICE_EN;
315		hpf_value	= 0;
316		bypass		= 0;
317		break;
318	case 32000:
319		fs		= DA7210_PLL_FS_32000;
320		hpf_mask	= DA7210_VOICE_EN;
321		hpf_value	= 0;
322		bypass		= DA7210_PLL_BYP;
323		break;
324	case 44100:
325		fs		= DA7210_PLL_FS_44100;
326		hpf_mask	= DA7210_VOICE_EN;
327		hpf_value	= 0;
328		bypass		= 0;
329		break;
330	case 48000:
331		fs		= DA7210_PLL_FS_48000;
332		hpf_mask	= DA7210_VOICE_EN;
333		hpf_value	= 0;
334		bypass		= DA7210_PLL_BYP;
335		break;
336	case 88200:
337		fs		= DA7210_PLL_FS_88200;
338		hpf_mask	= DA7210_VOICE_EN;
339		hpf_value	= 0;
340		bypass		= 0;
341		break;
342	case 96000:
343		fs		= DA7210_PLL_FS_96000;
344		hpf_mask	= DA7210_VOICE_EN;
345		hpf_value	= 0;
346		bypass		= DA7210_PLL_BYP;
347		break;
348	default:
349		return -EINVAL;
350	}
351
352	/* Disable active mode */
353	snd_soc_update_bits(codec, DA7210_STARTUP1, DA7210_SC_MST_EN, 0);
354
355	snd_soc_update_bits(codec, hpf_reg, hpf_mask, hpf_value);
356	snd_soc_update_bits(codec, DA7210_PLL, DA7210_PLL_FS_MASK, fs);
357	snd_soc_update_bits(codec, DA7210_PLL_DIV3, DA7210_PLL_BYP, bypass);
358
359	/* Enable active mode */
360	snd_soc_update_bits(codec, DA7210_STARTUP1,
361			    DA7210_SC_MST_EN, DA7210_SC_MST_EN);
362
363	return 0;
364}
365
366/*
367 * Set DAI mode and Format
368 */
369static int da7210_set_dai_fmt(struct snd_soc_dai *codec_dai, u32 fmt)
370{
371	struct snd_soc_codec *codec = codec_dai->codec;
372	u32 dai_cfg1;
373	u32 dai_cfg3;
374
375	dai_cfg1 = 0x7f & da7210_read(codec, DA7210_DAI_CFG1);
376	dai_cfg3 = 0xfc & da7210_read(codec, DA7210_DAI_CFG3);
377
378	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
379	case SND_SOC_DAIFMT_CBM_CFM:
380		dai_cfg1 |= DA7210_DAI_MODE_MASTER;
381		break;
382	default:
383		return -EINVAL;
384	}
385
386	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
387	case SND_SOC_DAIFMT_I2S:
388		dai_cfg3 |= DA7210_DAI_FORMAT_I2SMODE;
389		break;
390	default:
391		return -EINVAL;
392	}
393
394	dai_cfg1 |= DA7210_DAI_FLEN_64BIT;
395
396	da7210_write(codec, DA7210_DAI_CFG1, dai_cfg1);
397	da7210_write(codec, DA7210_DAI_CFG3, dai_cfg3);
398
399	return 0;
400}
401
402#define DA7210_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE)
403
404/* DAI operations */
405static struct snd_soc_dai_ops da7210_dai_ops = {
406	.startup	= da7210_startup,
407	.hw_params	= da7210_hw_params,
408	.set_fmt	= da7210_set_dai_fmt,
409};
410
411struct snd_soc_dai da7210_dai = {
412	.name = "DA7210 IIS",
413	.id = 0,
414	/* playback capabilities */
415	.playback = {
416		.stream_name = "Playback",
417		.channels_min = 1,
418		.channels_max = 2,
419		.rates = SNDRV_PCM_RATE_8000_96000,
420		.formats = DA7210_FORMATS,
421	},
422	/* capture capabilities */
423	.capture = {
424		.stream_name = "Capture",
425		.channels_min = 1,
426		.channels_max = 2,
427		.rates = SNDRV_PCM_RATE_8000_96000,
428		.formats = DA7210_FORMATS,
429	},
430	.ops = &da7210_dai_ops,
431	.symmetric_rates = 1,
432};
433EXPORT_SYMBOL_GPL(da7210_dai);
434
435/*
436 * Initialize the DA7210 driver
437 * register the mixer and dsp interfaces with the kernel
438 */
439static int da7210_init(struct da7210_priv *da7210)
440{
441	struct snd_soc_codec *codec = &da7210->codec;
442	int ret = 0;
443
444	if (da7210_codec) {
445		dev_err(codec->dev, "Another da7210 is registered\n");
446		return -EINVAL;
447	}
448
449	mutex_init(&codec->mutex);
450	INIT_LIST_HEAD(&codec->dapm_widgets);
451	INIT_LIST_HEAD(&codec->dapm_paths);
452
453	snd_soc_codec_set_drvdata(codec, da7210);
454	codec->name		= "DA7210";
455	codec->owner		= THIS_MODULE;
456	codec->read		= da7210_read;
457	codec->write		= da7210_write;
458	codec->dai		= &da7210_dai;
459	codec->num_dai		= 1;
460	codec->hw_write		= (hw_write_t)i2c_master_send;
461	codec->reg_cache_size	= ARRAY_SIZE(da7210_reg);
462	codec->reg_cache	= kmemdup(da7210_reg,
463					  sizeof(da7210_reg), GFP_KERNEL);
464
465	if (!codec->reg_cache)
466		return -ENOMEM;
467
468	da7210_dai.dev = codec->dev;
469	da7210_codec = codec;
470
471	ret = snd_soc_register_codec(codec);
472	if (ret) {
473		dev_err(codec->dev, "Failed to register CODEC: %d\n", ret);
474		goto init_err;
475	}
476
477	ret = snd_soc_register_dai(&da7210_dai);
478	if (ret) {
479		dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
480		goto codec_err;
481	}
482
483
484	/*
485	 * make sure that DA7210 use bypass mode before start up
486	 */
487	da7210_write(codec, DA7210_STARTUP1, 0);
488	da7210_write(codec, DA7210_PLL_DIV3,
489		     DA7210_MCLK_RANGE_10_20_MHZ | DA7210_PLL_BYP);
490
491	/*
492	 * ADC settings
493	 */
494
495	/* Enable Left & Right MIC PGA and Mic Bias */
496	da7210_write(codec, DA7210_MIC_L, DA7210_MIC_L_EN | DA7210_MICBIAS_EN);
497	da7210_write(codec, DA7210_MIC_R, DA7210_MIC_R_EN);
498
499	/* Enable Left and Right input PGA */
500	da7210_write(codec, DA7210_INMIX_L, DA7210_IN_L_EN);
501	da7210_write(codec, DA7210_INMIX_R, DA7210_IN_R_EN);
502
503	/* Enable Left and Right ADC */
504	da7210_write(codec, DA7210_ADC, DA7210_ADC_L_EN | DA7210_ADC_R_EN);
505
506	/*
507	 * DAC settings
508	 */
509
510	/* Enable Left and Right DAC */
511	da7210_write(codec, DA7210_DAC_SEL,
512		     DA7210_DAC_L_SRC_DAI_L | DA7210_DAC_L_EN |
513		     DA7210_DAC_R_SRC_DAI_R | DA7210_DAC_R_EN);
514
515	/* Enable Left and Right out PGA */
516	da7210_write(codec, DA7210_OUTMIX_L, DA7210_OUT_L_EN);
517	da7210_write(codec, DA7210_OUTMIX_R, DA7210_OUT_R_EN);
518
519	/* Enable Left and Right HeadPhone PGA */
520	da7210_write(codec, DA7210_HP_CFG,
521		     DA7210_HP_2CAP_MODE | DA7210_HP_SENSE_EN |
522		     DA7210_HP_L_EN | DA7210_HP_MODE | DA7210_HP_R_EN);
523
524	/* Diable PLL and bypass it */
525	da7210_write(codec, DA7210_PLL, DA7210_PLL_FS_48000);
526
527	/*
528	 * If 48kHz sound came, it use bypass mode,
529	 * and when it is 44.1kHz, it use PLL.
530	 *
531	 * This time, this driver sets PLL always ON
532	 * and controls bypass/PLL mode by switching
533	 * DA7210_PLL_DIV3 :: DA7210_PLL_BYP bit.
534	 *   see da7210_hw_params
535	 */
536	da7210_write(codec, DA7210_PLL_DIV1, 0xE5); /* MCLK = 12.288MHz */
537	da7210_write(codec, DA7210_PLL_DIV2, 0x99);
538	da7210_write(codec, DA7210_PLL_DIV3, 0x0A |
539		     DA7210_MCLK_RANGE_10_20_MHZ | DA7210_PLL_BYP);
540	snd_soc_update_bits(codec, DA7210_PLL, DA7210_PLL_EN, DA7210_PLL_EN);
541
542	/* As suggested by Dialog */
543	da7210_write(codec, DA7210_A_HID_UNLOCK,	0x8B); /* unlock */
544	da7210_write(codec, DA7210_A_TEST_UNLOCK,	0xB4);
545	da7210_write(codec, DA7210_A_PLL1,		0x01);
546	da7210_write(codec, DA7210_A_CP_MODE,		0x7C);
547	da7210_write(codec, DA7210_A_HID_UNLOCK,	0x00); /* re-lock */
548	da7210_write(codec, DA7210_A_TEST_UNLOCK,	0x00);
549
550	/* Activate all enabled subsystem */
551	da7210_write(codec, DA7210_STARTUP1, DA7210_SC_MST_EN);
552
553	return ret;
554
555codec_err:
556	snd_soc_unregister_codec(codec);
557init_err:
558	kfree(codec->reg_cache);
559	codec->reg_cache = NULL;
560
561	return ret;
562
563}
564
565#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
566static int __devinit da7210_i2c_probe(struct i2c_client *i2c,
567			   	      const struct i2c_device_id *id)
568{
569	struct da7210_priv *da7210;
570	struct snd_soc_codec *codec;
571	int ret;
572
573	da7210 = kzalloc(sizeof(struct da7210_priv), GFP_KERNEL);
574	if (!da7210)
575		return -ENOMEM;
576
577	codec = &da7210->codec;
578	codec->dev = &i2c->dev;
579
580	i2c_set_clientdata(i2c, da7210);
581	codec->control_data = i2c;
582
583	ret = da7210_init(da7210);
584	if (ret < 0) {
585		pr_err("Failed to initialise da7210 audio codec\n");
586		kfree(da7210);
587	}
588
589	return ret;
590}
591
592static int __devexit da7210_i2c_remove(struct i2c_client *client)
593{
594	struct da7210_priv *da7210 = i2c_get_clientdata(client);
595
596	snd_soc_unregister_dai(&da7210_dai);
597	kfree(da7210->codec.reg_cache);
598	kfree(da7210);
599	da7210_codec = NULL;
600
601	return 0;
602}
603
604static const struct i2c_device_id da7210_i2c_id[] = {
605	{ "da7210", 0 },
606	{ }
607};
608MODULE_DEVICE_TABLE(i2c, da7210_i2c_id);
609
610/* I2C codec control layer */
611static struct i2c_driver da7210_i2c_driver = {
612	.driver = {
613		.name = "DA7210 I2C Codec",
614		.owner = THIS_MODULE,
615	},
616	.probe = da7210_i2c_probe,
617	.remove =  __devexit_p(da7210_i2c_remove),
618	.id_table = da7210_i2c_id,
619};
620#endif
621
622static int da7210_probe(struct platform_device *pdev)
623{
624	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
625	struct snd_soc_codec *codec;
626	int ret;
627
628	if (!da7210_codec) {
629		dev_err(&pdev->dev, "Codec device not registered\n");
630		return -ENODEV;
631	}
632
633	socdev->card->codec = da7210_codec;
634	codec = da7210_codec;
635
636	/* Register pcms */
637	ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
638	if (ret < 0)
639		goto pcm_err;
640
641	snd_soc_add_controls(da7210_codec, da7210_snd_controls,
642			     ARRAY_SIZE(da7210_snd_controls));
643
644	dev_info(&pdev->dev, "DA7210 Audio Codec %s\n", DA7210_VERSION);
645
646pcm_err:
647	return ret;
648}
649
650static int da7210_remove(struct platform_device *pdev)
651{
652	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
653
654	snd_soc_free_pcms(socdev);
655	snd_soc_dapm_free(socdev);
656
657	return 0;
658}
659
660struct snd_soc_codec_device soc_codec_dev_da7210 = {
661	.probe =	da7210_probe,
662	.remove =	da7210_remove,
663};
664EXPORT_SYMBOL_GPL(soc_codec_dev_da7210);
665
666static int __init da7210_modinit(void)
667{
668	int ret = 0;
669#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
670	ret = i2c_add_driver(&da7210_i2c_driver);
671#endif
672	return ret;
673}
674module_init(da7210_modinit);
675
676static void __exit da7210_exit(void)
677{
678#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
679	i2c_del_driver(&da7210_i2c_driver);
680#endif
681}
682module_exit(da7210_exit);
683
684MODULE_DESCRIPTION("ASoC DA7210 driver");
685MODULE_AUTHOR("David Chen, Kuninori Morimoto");
686MODULE_LICENSE("GPL");
687