blob: c3a4bb207d7bc8fa1a790a8483d37e9b0bcbbf3c [file] [log] [blame]
Arun KSc1f27192008-10-02 14:45:49 +05301/*
2 * ALSA SoC TLV320AIC23 codec driver
3 *
4 * Author: Arun KS, <arunks@mistralsolutions.com>
5 * Copyright: (C) 2008 Mistral Solutions Pvt Ltd.,
6 *
7 * Based on sound/soc/codecs/wm8731.c by Richard Purdie
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * Notes:
14 * The AIC23 is a driver for a low power stereo audio
15 * codec tlv320aic23
16 *
17 * The machine layer should disable unsupported inputs/outputs by
18 * snd_soc_dapm_disable_pin(codec, "LHPOUT"), etc.
19 */
20
21#include <linux/module.h>
22#include <linux/moduleparam.h>
23#include <linux/init.h>
24#include <linux/delay.h>
25#include <linux/pm.h>
26#include <linux/i2c.h>
27#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Arun KSc1f27192008-10-02 14:45:49 +053029#include <sound/core.h>
30#include <sound/pcm.h>
31#include <sound/pcm_params.h>
32#include <sound/soc.h>
Arun KSc1f27192008-10-02 14:45:49 +053033#include <sound/tlv.h>
34#include <sound/initval.h>
35
36#include "tlv320aic23.h"
37
Arun KSc1f27192008-10-02 14:45:49 +053038#define AIC23_VERSION "0.1"
39
Arun KSc1f27192008-10-02 14:45:49 +053040/*
41 * AIC23 register cache
42 */
43static const u16 tlv320aic23_reg[] = {
44 0x0097, 0x0097, 0x00F9, 0x00F9, /* 0 */
45 0x001A, 0x0004, 0x0007, 0x0001, /* 4 */
46 0x0020, 0x0000, 0x0000, 0x0000, /* 8 */
47 0x0000, 0x0000, 0x0000, 0x0000, /* 12 */
48};
49
50/*
51 * read tlv320aic23 register cache
52 */
53static inline unsigned int tlv320aic23_read_reg_cache(struct snd_soc_codec
54 *codec, unsigned int reg)
55{
56 u16 *cache = codec->reg_cache;
57 if (reg >= ARRAY_SIZE(tlv320aic23_reg))
58 return -1;
59 return cache[reg];
60}
61
62/*
63 * write tlv320aic23 register cache
64 */
65static inline void tlv320aic23_write_reg_cache(struct snd_soc_codec *codec,
66 u8 reg, u16 value)
67{
68 u16 *cache = codec->reg_cache;
69 if (reg >= ARRAY_SIZE(tlv320aic23_reg))
70 return;
71 cache[reg] = value;
72}
73
74/*
75 * write to the tlv320aic23 register space
76 */
77static int tlv320aic23_write(struct snd_soc_codec *codec, unsigned int reg,
78 unsigned int value)
79{
80
Arun KS4aa02392008-10-13 15:47:25 +053081 u8 data[2];
Arun KSc1f27192008-10-02 14:45:49 +053082
83 /* TLV320AIC23 has 7 bit address and 9 bits of data
84 * so we need to switch one data bit into reg and rest
85 * of data into val
86 */
87
Roel Kluin84ed1a12009-10-23 16:03:08 +020088 if (reg > 9 && reg != 15) {
Roel Kluin449bd542009-05-27 17:08:39 -070089 printk(KERN_WARNING "%s Invalid register R%u\n", __func__, reg);
Arun KSc1f27192008-10-02 14:45:49 +053090 return -1;
91 }
92
Arun KS4aa02392008-10-13 15:47:25 +053093 data[0] = (reg << 1) | (value >> 8 & 0x01);
94 data[1] = value & 0xff;
Arun KSc1f27192008-10-02 14:45:49 +053095
96 tlv320aic23_write_reg_cache(codec, reg, value);
97
Arun KS4aa02392008-10-13 15:47:25 +053098 if (codec->hw_write(codec->control_data, data, 2) == 2)
Arun KSc1f27192008-10-02 14:45:49 +053099 return 0;
100
Roel Kluin449bd542009-05-27 17:08:39 -0700101 printk(KERN_ERR "%s cannot write %03x to register R%u\n", __func__,
Arun KSc1f27192008-10-02 14:45:49 +0530102 value, reg);
103
104 return -EIO;
105}
106
107static const char *rec_src_text[] = { "Line", "Mic" };
108static const char *deemph_text[] = {"None", "32Khz", "44.1Khz", "48Khz"};
Arun KSc1f27192008-10-02 14:45:49 +0530109
110static const struct soc_enum rec_src_enum =
111 SOC_ENUM_SINGLE(TLV320AIC23_ANLG, 2, 2, rec_src_text);
112
113static const struct snd_kcontrol_new tlv320aic23_rec_src_mux_controls =
114SOC_DAPM_ENUM("Input Select", rec_src_enum);
115
116static const struct soc_enum tlv320aic23_rec_src =
117 SOC_ENUM_SINGLE(TLV320AIC23_ANLG, 2, 2, rec_src_text);
118static const struct soc_enum tlv320aic23_deemph =
119 SOC_ENUM_SINGLE(TLV320AIC23_DIGT, 1, 4, deemph_text);
Arun KSc1f27192008-10-02 14:45:49 +0530120
121static const DECLARE_TLV_DB_SCALE(out_gain_tlv, -12100, 100, 0);
122static const DECLARE_TLV_DB_SCALE(input_gain_tlv, -1725, 75, 0);
Arun KSdf91ddf2008-10-03 17:07:30 +0530123static const DECLARE_TLV_DB_SCALE(sidetone_vol_tlv, -1800, 300, 0);
124
125static int snd_soc_tlv320aic23_put_volsw(struct snd_kcontrol *kcontrol,
126 struct snd_ctl_elem_value *ucontrol)
127{
128 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
129 u16 val, reg;
130
131 val = (ucontrol->value.integer.value[0] & 0x07);
132
133 /* linear conversion to userspace
134 * 000 = -6db
135 * 001 = -9db
136 * 010 = -12db
137 * 011 = -18db (Min)
138 * 100 = 0db (Max)
139 */
140 val = (val >= 4) ? 4 : (3 - val);
141
142 reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_ANLG) & (~0x1C0);
143 tlv320aic23_write(codec, TLV320AIC23_ANLG, reg | (val << 6));
144
145 return 0;
146}
147
148static int snd_soc_tlv320aic23_get_volsw(struct snd_kcontrol *kcontrol,
149 struct snd_ctl_elem_value *ucontrol)
150{
151 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
152 u16 val;
153
154 val = tlv320aic23_read_reg_cache(codec, TLV320AIC23_ANLG) & (0x1C0);
155 val = val >> 6;
156 val = (val >= 4) ? 4 : (3 - val);
157 ucontrol->value.integer.value[0] = val;
158 return 0;
159
160}
161
Arun KSc1f27192008-10-02 14:45:49 +0530162static const struct snd_kcontrol_new tlv320aic23_snd_controls[] = {
163 SOC_DOUBLE_R_TLV("Digital Playback Volume", TLV320AIC23_LCHNVOL,
164 TLV320AIC23_RCHNVOL, 0, 127, 0, out_gain_tlv),
165 SOC_SINGLE("Digital Playback Switch", TLV320AIC23_DIGT, 3, 1, 1),
166 SOC_DOUBLE_R("Line Input Switch", TLV320AIC23_LINVOL,
167 TLV320AIC23_RINVOL, 7, 1, 0),
168 SOC_DOUBLE_R_TLV("Line Input Volume", TLV320AIC23_LINVOL,
169 TLV320AIC23_RINVOL, 0, 31, 0, input_gain_tlv),
170 SOC_SINGLE("Mic Input Switch", TLV320AIC23_ANLG, 1, 1, 1),
171 SOC_SINGLE("Mic Booster Switch", TLV320AIC23_ANLG, 0, 1, 0),
Peter Ujfalusi0f9887d2011-10-05 10:29:19 +0300172 SOC_SINGLE_EXT_TLV("Sidetone Volume", TLV320AIC23_ANLG, 6, 4, 0,
173 snd_soc_tlv320aic23_get_volsw,
174 snd_soc_tlv320aic23_put_volsw, sidetone_vol_tlv),
Arun KSc1f27192008-10-02 14:45:49 +0530175 SOC_ENUM("Playback De-emphasis", tlv320aic23_deemph),
176};
177
Arun KSc1f27192008-10-02 14:45:49 +0530178/* PGA Mixer controls for Line and Mic switch */
179static const struct snd_kcontrol_new tlv320aic23_output_mixer_controls[] = {
180 SOC_DAPM_SINGLE("Line Bypass Switch", TLV320AIC23_ANLG, 3, 1, 0),
181 SOC_DAPM_SINGLE("Mic Sidetone Switch", TLV320AIC23_ANLG, 5, 1, 0),
182 SOC_DAPM_SINGLE("Playback Switch", TLV320AIC23_ANLG, 4, 1, 0),
183};
184
185static const struct snd_soc_dapm_widget tlv320aic23_dapm_widgets[] = {
186 SND_SOC_DAPM_DAC("DAC", "Playback", TLV320AIC23_PWR, 3, 1),
187 SND_SOC_DAPM_ADC("ADC", "Capture", TLV320AIC23_PWR, 2, 1),
188 SND_SOC_DAPM_MUX("Capture Source", SND_SOC_NOPM, 0, 0,
189 &tlv320aic23_rec_src_mux_controls),
190 SND_SOC_DAPM_MIXER("Output Mixer", TLV320AIC23_PWR, 4, 1,
191 &tlv320aic23_output_mixer_controls[0],
192 ARRAY_SIZE(tlv320aic23_output_mixer_controls)),
193 SND_SOC_DAPM_PGA("Line Input", TLV320AIC23_PWR, 0, 1, NULL, 0),
194 SND_SOC_DAPM_PGA("Mic Input", TLV320AIC23_PWR, 1, 1, NULL, 0),
195
196 SND_SOC_DAPM_OUTPUT("LHPOUT"),
197 SND_SOC_DAPM_OUTPUT("RHPOUT"),
198 SND_SOC_DAPM_OUTPUT("LOUT"),
199 SND_SOC_DAPM_OUTPUT("ROUT"),
200
201 SND_SOC_DAPM_INPUT("LLINEIN"),
202 SND_SOC_DAPM_INPUT("RLINEIN"),
203
204 SND_SOC_DAPM_INPUT("MICIN"),
205};
206
Lu Guanquna7dca702011-03-30 21:53:16 +0800207static const struct snd_soc_dapm_route tlv320aic23_intercon[] = {
Arun KSc1f27192008-10-02 14:45:49 +0530208 /* Output Mixer */
209 {"Output Mixer", "Line Bypass Switch", "Line Input"},
210 {"Output Mixer", "Playback Switch", "DAC"},
211 {"Output Mixer", "Mic Sidetone Switch", "Mic Input"},
212
213 /* Outputs */
214 {"RHPOUT", NULL, "Output Mixer"},
215 {"LHPOUT", NULL, "Output Mixer"},
216 {"LOUT", NULL, "Output Mixer"},
217 {"ROUT", NULL, "Output Mixer"},
218
219 /* Inputs */
220 {"Line Input", "NULL", "LLINEIN"},
221 {"Line Input", "NULL", "RLINEIN"},
222
223 {"Mic Input", "NULL", "MICIN"},
224
225 /* input mux */
226 {"Capture Source", "Line", "Line Input"},
227 {"Capture Source", "Mic", "Mic Input"},
228 {"ADC", NULL, "Capture Source"},
229
230};
231
Troy Kisky26df91c2008-11-05 18:53:28 +0000232/* AIC23 driver data */
233struct aic23 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000234 enum snd_soc_control_type control_type;
235 void *control_data;
Troy Kisky26df91c2008-11-05 18:53:28 +0000236 int mclk;
237 int requested_adc;
238 int requested_dac;
Arun KSc1f27192008-10-02 14:45:49 +0530239};
240
Troy Kisky26df91c2008-11-05 18:53:28 +0000241/*
242 * Common Crystals used
243 * 11.2896 Mhz /128 = *88.2k /192 = 58.8k
244 * 12.0000 Mhz /125 = *96k /136 = 88.235K
245 * 12.2880 Mhz /128 = *96k /192 = 64k
246 * 16.9344 Mhz /128 = 132.3k /192 = *88.2k
247 * 18.4320 Mhz /128 = 144k /192 = *96k
248 */
249
250/*
251 * Normal BOSR 0-256/2 = 128, 1-384/2 = 192
252 * USB BOSR 0-250/2 = 125, 1-272/2 = 136
253 */
254static const int bosr_usb_divisor_table[] = {
255 128, 125, 192, 136
256};
257#define LOWER_GROUP ((1<<0) | (1<<1) | (1<<2) | (1<<3) | (1<<6) | (1<<7))
258#define UPPER_GROUP ((1<<8) | (1<<9) | (1<<10) | (1<<11) | (1<<15))
259static const unsigned short sr_valid_mask[] = {
260 LOWER_GROUP|UPPER_GROUP, /* Normal, bosr - 0*/
Troy Kisky26df91c2008-11-05 18:53:28 +0000261 LOWER_GROUP, /* Usb, bosr - 0*/
Troy Kiskybab02122009-11-17 13:51:01 -0700262 LOWER_GROUP|UPPER_GROUP, /* Normal, bosr - 1*/
Troy Kisky26df91c2008-11-05 18:53:28 +0000263 UPPER_GROUP, /* Usb, bosr - 1*/
264};
265/*
266 * Every divisor is a factor of 11*12
267 */
268#define SR_MULT (11*12)
Troy Kiskyccff4b12009-06-05 19:15:58 -0700269#define A(x) (SR_MULT/x)
Troy Kisky26df91c2008-11-05 18:53:28 +0000270static const unsigned char sr_adc_mult_table[] = {
Troy Kiskyccff4b12009-06-05 19:15:58 -0700271 A(2), A(2), A(12), A(12), 0, 0, A(3), A(1),
272 A(2), A(2), A(11), A(11), 0, 0, 0, A(1)
Troy Kisky26df91c2008-11-05 18:53:28 +0000273};
274static const unsigned char sr_dac_mult_table[] = {
Troy Kiskyccff4b12009-06-05 19:15:58 -0700275 A(2), A(12), A(2), A(12), 0, 0, A(3), A(1),
276 A(2), A(11), A(2), A(11), 0, 0, 0, A(1)
Troy Kisky26df91c2008-11-05 18:53:28 +0000277};
278
279static unsigned get_score(int adc, int adc_l, int adc_h, int need_adc,
280 int dac, int dac_l, int dac_h, int need_dac)
281{
282 if ((adc >= adc_l) && (adc <= adc_h) &&
283 (dac >= dac_l) && (dac <= dac_h)) {
284 int diff_adc = need_adc - adc;
285 int diff_dac = need_dac - dac;
286 return abs(diff_adc) + abs(diff_dac);
287 }
288 return UINT_MAX;
289}
290
291static int find_rate(int mclk, u32 need_adc, u32 need_dac)
292{
293 int i, j;
294 int best_i = -1;
295 int best_j = -1;
296 int best_div = 0;
297 unsigned best_score = UINT_MAX;
298 int adc_l, adc_h, dac_l, dac_h;
299
300 need_adc *= SR_MULT;
301 need_dac *= SR_MULT;
302 /*
303 * rates given are +/- 1/32
304 */
305 adc_l = need_adc - (need_adc >> 5);
306 adc_h = need_adc + (need_adc >> 5);
307 dac_l = need_dac - (need_dac >> 5);
308 dac_h = need_dac + (need_dac >> 5);
Mark Brown8d702f22008-11-17 21:42:01 +0000309 for (i = 0; i < ARRAY_SIZE(bosr_usb_divisor_table); i++) {
Troy Kisky26df91c2008-11-05 18:53:28 +0000310 int base = mclk / bosr_usb_divisor_table[i];
311 int mask = sr_valid_mask[i];
Mark Brown8d702f22008-11-17 21:42:01 +0000312 for (j = 0; j < ARRAY_SIZE(sr_adc_mult_table);
313 j++, mask >>= 1) {
Troy Kisky26df91c2008-11-05 18:53:28 +0000314 int adc;
315 int dac;
316 int score;
317 if ((mask & 1) == 0)
318 continue;
319 adc = base * sr_adc_mult_table[j];
320 dac = base * sr_dac_mult_table[j];
321 score = get_score(adc, adc_l, adc_h, need_adc,
322 dac, dac_l, dac_h, need_dac);
323 if (best_score > score) {
324 best_score = score;
325 best_i = i;
326 best_j = j;
327 best_div = 0;
328 }
329 score = get_score((adc >> 1), adc_l, adc_h, need_adc,
330 (dac >> 1), dac_l, dac_h, need_dac);
331 /* prefer to have a /2 */
332 if ((score != UINT_MAX) && (best_score >= score)) {
333 best_score = score;
334 best_i = i;
335 best_j = j;
336 best_div = 1;
337 }
338 }
339 }
340 return (best_j << 2) | best_i | (best_div << TLV320AIC23_CLKIN_SHIFT);
341}
342
Mark Brown8d702f22008-11-17 21:42:01 +0000343#ifdef DEBUG
Troy Kisky26df91c2008-11-05 18:53:28 +0000344static void get_current_sample_rates(struct snd_soc_codec *codec, int mclk,
345 u32 *sample_rate_adc, u32 *sample_rate_dac)
346{
347 int src = tlv320aic23_read_reg_cache(codec, TLV320AIC23_SRATE);
348 int sr = (src >> 2) & 0x0f;
349 int val = (mclk / bosr_usb_divisor_table[src & 3]);
350 int adc = (val * sr_adc_mult_table[sr]) / SR_MULT;
351 int dac = (val * sr_dac_mult_table[sr]) / SR_MULT;
352 if (src & TLV320AIC23_CLKIN_HALF) {
353 adc >>= 1;
354 dac >>= 1;
355 }
356 *sample_rate_adc = adc;
357 *sample_rate_dac = dac;
358}
Mark Brown8d702f22008-11-17 21:42:01 +0000359#endif
Troy Kisky26df91c2008-11-05 18:53:28 +0000360
361static int set_sample_rate_control(struct snd_soc_codec *codec, int mclk,
362 u32 sample_rate_adc, u32 sample_rate_dac)
363{
364 /* Search for the right sample rate */
365 int data = find_rate(mclk, sample_rate_adc, sample_rate_dac);
366 if (data < 0) {
367 printk(KERN_ERR "%s:Invalid rate %u,%u requested\n",
368 __func__, sample_rate_adc, sample_rate_dac);
369 return -EINVAL;
370 }
371 tlv320aic23_write(codec, TLV320AIC23_SRATE, data);
Mark Brown8d702f22008-11-17 21:42:01 +0000372#ifdef DEBUG
373 {
374 u32 adc, dac;
Troy Kisky26df91c2008-11-05 18:53:28 +0000375 get_current_sample_rates(codec, mclk, &adc, &dac);
376 printk(KERN_DEBUG "actual samplerate = %u,%u reg=%x\n",
377 adc, dac, data);
378 }
Mark Brown8d702f22008-11-17 21:42:01 +0000379#endif
Troy Kisky26df91c2008-11-05 18:53:28 +0000380 return 0;
381}
382
Arun KSc1f27192008-10-02 14:45:49 +0530383static int tlv320aic23_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000384 struct snd_pcm_hw_params *params,
385 struct snd_soc_dai *dai)
Arun KSc1f27192008-10-02 14:45:49 +0530386{
387 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000388 struct snd_soc_codec *codec = rtd->codec;
Troy Kisky26df91c2008-11-05 18:53:28 +0000389 u16 iface_reg;
390 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000391 struct aic23 *aic23 = snd_soc_codec_get_drvdata(codec);
Troy Kisky26df91c2008-11-05 18:53:28 +0000392 u32 sample_rate_adc = aic23->requested_adc;
393 u32 sample_rate_dac = aic23->requested_dac;
394 u32 sample_rate = params_rate(params);
395
396 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
397 aic23->requested_dac = sample_rate_dac = sample_rate;
398 if (!sample_rate_adc)
399 sample_rate_adc = sample_rate;
400 } else {
401 aic23->requested_adc = sample_rate_adc = sample_rate;
402 if (!sample_rate_dac)
403 sample_rate_dac = sample_rate;
404 }
405 ret = set_sample_rate_control(codec, aic23->mclk, sample_rate_adc,
406 sample_rate_dac);
407 if (ret < 0)
408 return ret;
Arun KSc1f27192008-10-02 14:45:49 +0530409
410 iface_reg =
411 tlv320aic23_read_reg_cache(codec,
412 TLV320AIC23_DIGT_FMT) & ~(0x03 << 2);
Arun KSc1f27192008-10-02 14:45:49 +0530413 switch (params_format(params)) {
414 case SNDRV_PCM_FORMAT_S16_LE:
415 break;
416 case SNDRV_PCM_FORMAT_S20_3LE:
417 iface_reg |= (0x01 << 2);
418 break;
419 case SNDRV_PCM_FORMAT_S24_LE:
420 iface_reg |= (0x02 << 2);
421 break;
422 case SNDRV_PCM_FORMAT_S32_LE:
423 iface_reg |= (0x03 << 2);
424 break;
425 }
426 tlv320aic23_write(codec, TLV320AIC23_DIGT_FMT, iface_reg);
427
428 return 0;
429}
430
Mark Browndee89c42008-11-18 22:11:38 +0000431static int tlv320aic23_pcm_prepare(struct snd_pcm_substream *substream,
432 struct snd_soc_dai *dai)
Arun KSc1f27192008-10-02 14:45:49 +0530433{
434 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000435 struct snd_soc_codec *codec = rtd->codec;
Arun KSc1f27192008-10-02 14:45:49 +0530436
437 /* set active */
438 tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x0001);
439
440 return 0;
441}
442
Mark Browndee89c42008-11-18 22:11:38 +0000443static void tlv320aic23_shutdown(struct snd_pcm_substream *substream,
444 struct snd_soc_dai *dai)
Arun KSc1f27192008-10-02 14:45:49 +0530445{
446 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000447 struct snd_soc_codec *codec = rtd->codec;
448 struct aic23 *aic23 = snd_soc_codec_get_drvdata(codec);
Arun KSc1f27192008-10-02 14:45:49 +0530449
450 /* deactivate */
451 if (!codec->active) {
452 udelay(50);
453 tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x0);
454 }
Troy Kisky26df91c2008-11-05 18:53:28 +0000455 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
456 aic23->requested_dac = 0;
457 else
458 aic23->requested_adc = 0;
Arun KSc1f27192008-10-02 14:45:49 +0530459}
460
461static int tlv320aic23_mute(struct snd_soc_dai *dai, int mute)
462{
463 struct snd_soc_codec *codec = dai->codec;
464 u16 reg;
465
466 reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_DIGT);
467 if (mute)
468 reg |= TLV320AIC23_DACM_MUTE;
469
470 else
471 reg &= ~TLV320AIC23_DACM_MUTE;
472
473 tlv320aic23_write(codec, TLV320AIC23_DIGT, reg);
474
475 return 0;
476}
477
478static int tlv320aic23_set_dai_fmt(struct snd_soc_dai *codec_dai,
479 unsigned int fmt)
480{
481 struct snd_soc_codec *codec = codec_dai->codec;
482 u16 iface_reg;
483
484 iface_reg =
485 tlv320aic23_read_reg_cache(codec, TLV320AIC23_DIGT_FMT) & (~0x03);
486
487 /* set master/slave audio interface */
488 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
489 case SND_SOC_DAIFMT_CBM_CFM:
490 iface_reg |= TLV320AIC23_MS_MASTER;
491 break;
492 case SND_SOC_DAIFMT_CBS_CFS:
493 break;
494 default:
495 return -EINVAL;
496
497 }
498
499 /* interface format */
500 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
501 case SND_SOC_DAIFMT_I2S:
502 iface_reg |= TLV320AIC23_FOR_I2S;
503 break;
Peter Ujfalusi894bf922009-04-09 12:34:40 +0300504 case SND_SOC_DAIFMT_DSP_A:
505 iface_reg |= TLV320AIC23_LRP_ON;
Jarkko Nikulabd258672008-12-22 10:21:36 +0200506 case SND_SOC_DAIFMT_DSP_B:
Arun KSc1f27192008-10-02 14:45:49 +0530507 iface_reg |= TLV320AIC23_FOR_DSP;
508 break;
509 case SND_SOC_DAIFMT_RIGHT_J:
510 break;
511 case SND_SOC_DAIFMT_LEFT_J:
512 iface_reg |= TLV320AIC23_FOR_LJUST;
513 break;
514 default:
515 return -EINVAL;
516
517 }
518
519 tlv320aic23_write(codec, TLV320AIC23_DIGT_FMT, iface_reg);
520
521 return 0;
522}
523
524static int tlv320aic23_set_dai_sysclk(struct snd_soc_dai *codec_dai,
525 int clk_id, unsigned int freq, int dir)
526{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000527 struct aic23 *aic23 = snd_soc_dai_get_drvdata(codec_dai);
Troy Kisky26df91c2008-11-05 18:53:28 +0000528 aic23->mclk = freq;
529 return 0;
Arun KSc1f27192008-10-02 14:45:49 +0530530}
531
532static int tlv320aic23_set_bias_level(struct snd_soc_codec *codec,
533 enum snd_soc_bias_level level)
534{
535 u16 reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_PWR) & 0xff7f;
536
537 switch (level) {
538 case SND_SOC_BIAS_ON:
539 /* vref/mid, osc on, dac unmute */
Eric Bénard3d5a4512010-06-19 19:33:39 +0200540 reg &= ~(TLV320AIC23_DEVICE_PWR_OFF | TLV320AIC23_OSC_OFF | \
541 TLV320AIC23_DAC_OFF);
Arun KSc1f27192008-10-02 14:45:49 +0530542 tlv320aic23_write(codec, TLV320AIC23_PWR, reg);
543 break;
544 case SND_SOC_BIAS_PREPARE:
545 break;
546 case SND_SOC_BIAS_STANDBY:
547 /* everything off except vref/vmid, */
Eric Bénard3d5a4512010-06-19 19:33:39 +0200548 tlv320aic23_write(codec, TLV320AIC23_PWR, reg | \
549 TLV320AIC23_CLK_OFF);
Arun KSc1f27192008-10-02 14:45:49 +0530550 break;
551 case SND_SOC_BIAS_OFF:
552 /* everything off, dac mute, inactive */
553 tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x0);
554 tlv320aic23_write(codec, TLV320AIC23_PWR, 0xffff);
555 break;
556 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200557 codec->dapm.bias_level = level;
Arun KSc1f27192008-10-02 14:45:49 +0530558 return 0;
559}
560
561#define AIC23_RATES SNDRV_PCM_RATE_8000_96000
562#define AIC23_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
563 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)
564
Eric Miao6335d052009-03-03 09:41:00 +0800565static struct snd_soc_dai_ops tlv320aic23_dai_ops = {
566 .prepare = tlv320aic23_pcm_prepare,
567 .hw_params = tlv320aic23_hw_params,
568 .shutdown = tlv320aic23_shutdown,
569 .digital_mute = tlv320aic23_mute,
570 .set_fmt = tlv320aic23_set_dai_fmt,
571 .set_sysclk = tlv320aic23_set_dai_sysclk,
572};
573
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000574static struct snd_soc_dai_driver tlv320aic23_dai = {
575 .name = "tlv320aic23-hifi",
Arun KSc1f27192008-10-02 14:45:49 +0530576 .playback = {
577 .stream_name = "Playback",
578 .channels_min = 2,
579 .channels_max = 2,
580 .rates = AIC23_RATES,
581 .formats = AIC23_FORMATS,},
582 .capture = {
583 .stream_name = "Capture",
584 .channels_min = 2,
585 .channels_max = 2,
586 .rates = AIC23_RATES,
587 .formats = AIC23_FORMATS,},
Eric Miao6335d052009-03-03 09:41:00 +0800588 .ops = &tlv320aic23_dai_ops,
Arun KSc1f27192008-10-02 14:45:49 +0530589};
Arun KSc1f27192008-10-02 14:45:49 +0530590
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000591static int tlv320aic23_suspend(struct snd_soc_codec *codec,
Arun KSc1f27192008-10-02 14:45:49 +0530592 pm_message_t state)
593{
Arun KSc1f27192008-10-02 14:45:49 +0530594 tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_OFF);
595
596 return 0;
597}
598
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000599static int tlv320aic23_resume(struct snd_soc_codec *codec)
Arun KSc1f27192008-10-02 14:45:49 +0530600{
Arun KSc1f27192008-10-02 14:45:49 +0530601 u16 reg;
602
603 /* Sync reg_cache with the hardware */
Anuj Aggarwal3e59aaa2010-01-29 13:58:55 +0530604 for (reg = 0; reg <= TLV320AIC23_ACTIVE; reg++) {
Arun KSc1f27192008-10-02 14:45:49 +0530605 u16 val = tlv320aic23_read_reg_cache(codec, reg);
606 tlv320aic23_write(codec, reg, val);
607 }
Arun KSc1f27192008-10-02 14:45:49 +0530608 tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
Arun KSc1f27192008-10-02 14:45:49 +0530609
610 return 0;
611}
612
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000613static int tlv320aic23_probe(struct snd_soc_codec *codec)
Arun KSc1f27192008-10-02 14:45:49 +0530614{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000615 struct aic23 *aic23 = snd_soc_codec_get_drvdata(codec);
616 int reg;
Arun KSc1f27192008-10-02 14:45:49 +0530617
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000618 printk(KERN_INFO "AIC23 Audio Codec %s\n", AIC23_VERSION);
619 codec->control_data = aic23->control_data;
620 codec->hw_write = (hw_write_t)i2c_master_send;
621 codec->hw_read = NULL;
Arun KSc1f27192008-10-02 14:45:49 +0530622
623 /* Reset codec */
624 tlv320aic23_write(codec, TLV320AIC23_RESET, 0);
625
Arun KSc1f27192008-10-02 14:45:49 +0530626 /* power on device */
627 tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
628
629 tlv320aic23_write(codec, TLV320AIC23_DIGT, TLV320AIC23_DEEMP_44K);
630
631 /* Unmute input */
632 reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_LINVOL);
633 tlv320aic23_write(codec, TLV320AIC23_LINVOL,
634 (reg & (~TLV320AIC23_LIM_MUTED)) |
635 (TLV320AIC23_LRS_ENABLED));
636
637 reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_RINVOL);
638 tlv320aic23_write(codec, TLV320AIC23_RINVOL,
639 (reg & (~TLV320AIC23_LIM_MUTED)) |
640 TLV320AIC23_LRS_ENABLED);
641
642 reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_ANLG);
643 tlv320aic23_write(codec, TLV320AIC23_ANLG,
644 (reg) & (~TLV320AIC23_BYPASS_ON) &
645 (~TLV320AIC23_MICM_MUTED));
646
647 /* Default output volume */
648 tlv320aic23_write(codec, TLV320AIC23_LCHNVOL,
649 TLV320AIC23_DEFAULT_OUT_VOL &
650 TLV320AIC23_OUT_VOL_MASK);
651 tlv320aic23_write(codec, TLV320AIC23_RCHNVOL,
652 TLV320AIC23_DEFAULT_OUT_VOL &
653 TLV320AIC23_OUT_VOL_MASK);
654
655 tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x1);
656
Ian Molton3e8e1952009-01-09 00:23:21 +0000657 snd_soc_add_controls(codec, tlv320aic23_snd_controls,
658 ARRAY_SIZE(tlv320aic23_snd_controls));
Arun KSc1f27192008-10-02 14:45:49 +0530659
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000660 return 0;
Arun KSc1f27192008-10-02 14:45:49 +0530661}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000662
663static int tlv320aic23_remove(struct snd_soc_codec *codec)
664{
665 tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_OFF);
666 return 0;
667}
668
669static struct snd_soc_codec_driver soc_codec_dev_tlv320aic23 = {
670 .reg_cache_size = ARRAY_SIZE(tlv320aic23_reg),
671 .reg_word_size = sizeof(u16),
672 .reg_cache_default = tlv320aic23_reg,
673 .probe = tlv320aic23_probe,
674 .remove = tlv320aic23_remove,
675 .suspend = tlv320aic23_suspend,
676 .resume = tlv320aic23_resume,
677 .read = tlv320aic23_read_reg_cache,
678 .write = tlv320aic23_write,
679 .set_bias_level = tlv320aic23_set_bias_level,
Lu Guanquna7dca702011-03-30 21:53:16 +0800680 .dapm_widgets = tlv320aic23_dapm_widgets,
681 .num_dapm_widgets = ARRAY_SIZE(tlv320aic23_dapm_widgets),
682 .dapm_routes = tlv320aic23_intercon,
683 .num_dapm_routes = ARRAY_SIZE(tlv320aic23_intercon),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000684};
Arun KSc1f27192008-10-02 14:45:49 +0530685
686#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
687/*
688 * If the i2c layer weren't so broken, we could pass this kind of data
689 * around
690 */
691static int tlv320aic23_codec_probe(struct i2c_client *i2c,
692 const struct i2c_device_id *i2c_id)
693{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000694 struct aic23 *aic23;
Arun KSc1f27192008-10-02 14:45:49 +0530695 int ret;
696
697 if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
698 return -EINVAL;
699
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000700 aic23 = kzalloc(sizeof(struct aic23), GFP_KERNEL);
701 if (aic23 == NULL)
702 return -ENOMEM;
Arun KSc1f27192008-10-02 14:45:49 +0530703
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000704 i2c_set_clientdata(i2c, aic23);
705 aic23->control_data = i2c;
706 aic23->control_type = SND_SOC_I2C;
Arun KSc1f27192008-10-02 14:45:49 +0530707
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000708 ret = snd_soc_register_codec(&i2c->dev,
709 &soc_codec_dev_tlv320aic23, &tlv320aic23_dai, 1);
710 if (ret < 0)
711 kfree(aic23);
Arun KSc1f27192008-10-02 14:45:49 +0530712 return ret;
713}
714static int __exit tlv320aic23_i2c_remove(struct i2c_client *i2c)
715{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000716 snd_soc_unregister_codec(&i2c->dev);
717 kfree(i2c_get_clientdata(i2c));
Arun KSc1f27192008-10-02 14:45:49 +0530718 return 0;
719}
720
721static const struct i2c_device_id tlv320aic23_id[] = {
722 {"tlv320aic23", 0},
723 {}
724};
725
726MODULE_DEVICE_TABLE(i2c, tlv320aic23_id);
727
728static struct i2c_driver tlv320aic23_i2c_driver = {
729 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000730 .name = "tlv320aic23-codec",
Arun KSc1f27192008-10-02 14:45:49 +0530731 },
732 .probe = tlv320aic23_codec_probe,
733 .remove = __exit_p(tlv320aic23_i2c_remove),
734 .id_table = tlv320aic23_id,
735};
736
737#endif
738
Takashi Iwaic9b3a402008-12-10 07:47:22 +0100739static int __init tlv320aic23_modinit(void)
Mark Brown64089b82008-12-08 19:17:58 +0000740{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000741 int ret;
742#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
743 ret = i2c_add_driver(&tlv320aic23_i2c_driver);
744 if (ret != 0) {
745 printk(KERN_ERR "Failed to register TLV320AIC23 I2C driver: %d\n",
746 ret);
747 }
748#endif
749 return ret;
Mark Brown64089b82008-12-08 19:17:58 +0000750}
751module_init(tlv320aic23_modinit);
752
753static void __exit tlv320aic23_exit(void)
754{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000755#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
756 i2c_del_driver(&tlv320aic23_i2c_driver);
757#endif
Mark Brown64089b82008-12-08 19:17:58 +0000758}
759module_exit(tlv320aic23_exit);
760
Arun KSc1f27192008-10-02 14:45:49 +0530761MODULE_DESCRIPTION("ASoC TLV320AIC23 codec driver");
762MODULE_AUTHOR("Arun KS <arunks@mistralsolutions.com>");
763MODULE_LICENSE("GPL");