blob: 33bb52f3f68306686025890cd287a52292373fec [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
162#define SOC_TLV320AIC23_SINGLE_TLV(xname, reg, shift, max, invert, tlv_array) \
163{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
164 .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
165 SNDRV_CTL_ELEM_ACCESS_READWRITE,\
166 .tlv.p = (tlv_array), \
167 .info = snd_soc_info_volsw, .get = snd_soc_tlv320aic23_get_volsw,\
168 .put = snd_soc_tlv320aic23_put_volsw, \
169 .private_value = SOC_SINGLE_VALUE(reg, shift, max, invert) }
Arun KSc1f27192008-10-02 14:45:49 +0530170
171static const struct snd_kcontrol_new tlv320aic23_snd_controls[] = {
172 SOC_DOUBLE_R_TLV("Digital Playback Volume", TLV320AIC23_LCHNVOL,
173 TLV320AIC23_RCHNVOL, 0, 127, 0, out_gain_tlv),
174 SOC_SINGLE("Digital Playback Switch", TLV320AIC23_DIGT, 3, 1, 1),
175 SOC_DOUBLE_R("Line Input Switch", TLV320AIC23_LINVOL,
176 TLV320AIC23_RINVOL, 7, 1, 0),
177 SOC_DOUBLE_R_TLV("Line Input Volume", TLV320AIC23_LINVOL,
178 TLV320AIC23_RINVOL, 0, 31, 0, input_gain_tlv),
179 SOC_SINGLE("Mic Input Switch", TLV320AIC23_ANLG, 1, 1, 1),
180 SOC_SINGLE("Mic Booster Switch", TLV320AIC23_ANLG, 0, 1, 0),
Arun KSdf91ddf2008-10-03 17:07:30 +0530181 SOC_TLV320AIC23_SINGLE_TLV("Sidetone Volume", TLV320AIC23_ANLG,
182 6, 4, 0, sidetone_vol_tlv),
Arun KSc1f27192008-10-02 14:45:49 +0530183 SOC_ENUM("Playback De-emphasis", tlv320aic23_deemph),
184};
185
Arun KSc1f27192008-10-02 14:45:49 +0530186/* PGA Mixer controls for Line and Mic switch */
187static const struct snd_kcontrol_new tlv320aic23_output_mixer_controls[] = {
188 SOC_DAPM_SINGLE("Line Bypass Switch", TLV320AIC23_ANLG, 3, 1, 0),
189 SOC_DAPM_SINGLE("Mic Sidetone Switch", TLV320AIC23_ANLG, 5, 1, 0),
190 SOC_DAPM_SINGLE("Playback Switch", TLV320AIC23_ANLG, 4, 1, 0),
191};
192
193static const struct snd_soc_dapm_widget tlv320aic23_dapm_widgets[] = {
194 SND_SOC_DAPM_DAC("DAC", "Playback", TLV320AIC23_PWR, 3, 1),
195 SND_SOC_DAPM_ADC("ADC", "Capture", TLV320AIC23_PWR, 2, 1),
196 SND_SOC_DAPM_MUX("Capture Source", SND_SOC_NOPM, 0, 0,
197 &tlv320aic23_rec_src_mux_controls),
198 SND_SOC_DAPM_MIXER("Output Mixer", TLV320AIC23_PWR, 4, 1,
199 &tlv320aic23_output_mixer_controls[0],
200 ARRAY_SIZE(tlv320aic23_output_mixer_controls)),
201 SND_SOC_DAPM_PGA("Line Input", TLV320AIC23_PWR, 0, 1, NULL, 0),
202 SND_SOC_DAPM_PGA("Mic Input", TLV320AIC23_PWR, 1, 1, NULL, 0),
203
204 SND_SOC_DAPM_OUTPUT("LHPOUT"),
205 SND_SOC_DAPM_OUTPUT("RHPOUT"),
206 SND_SOC_DAPM_OUTPUT("LOUT"),
207 SND_SOC_DAPM_OUTPUT("ROUT"),
208
209 SND_SOC_DAPM_INPUT("LLINEIN"),
210 SND_SOC_DAPM_INPUT("RLINEIN"),
211
212 SND_SOC_DAPM_INPUT("MICIN"),
213};
214
Lu Guanquna7dca702011-03-30 21:53:16 +0800215static const struct snd_soc_dapm_route tlv320aic23_intercon[] = {
Arun KSc1f27192008-10-02 14:45:49 +0530216 /* Output Mixer */
217 {"Output Mixer", "Line Bypass Switch", "Line Input"},
218 {"Output Mixer", "Playback Switch", "DAC"},
219 {"Output Mixer", "Mic Sidetone Switch", "Mic Input"},
220
221 /* Outputs */
222 {"RHPOUT", NULL, "Output Mixer"},
223 {"LHPOUT", NULL, "Output Mixer"},
224 {"LOUT", NULL, "Output Mixer"},
225 {"ROUT", NULL, "Output Mixer"},
226
227 /* Inputs */
228 {"Line Input", "NULL", "LLINEIN"},
229 {"Line Input", "NULL", "RLINEIN"},
230
231 {"Mic Input", "NULL", "MICIN"},
232
233 /* input mux */
234 {"Capture Source", "Line", "Line Input"},
235 {"Capture Source", "Mic", "Mic Input"},
236 {"ADC", NULL, "Capture Source"},
237
238};
239
Troy Kisky26df91c2008-11-05 18:53:28 +0000240/* AIC23 driver data */
241struct aic23 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000242 enum snd_soc_control_type control_type;
243 void *control_data;
Troy Kisky26df91c2008-11-05 18:53:28 +0000244 int mclk;
245 int requested_adc;
246 int requested_dac;
Arun KSc1f27192008-10-02 14:45:49 +0530247};
248
Troy Kisky26df91c2008-11-05 18:53:28 +0000249/*
250 * Common Crystals used
251 * 11.2896 Mhz /128 = *88.2k /192 = 58.8k
252 * 12.0000 Mhz /125 = *96k /136 = 88.235K
253 * 12.2880 Mhz /128 = *96k /192 = 64k
254 * 16.9344 Mhz /128 = 132.3k /192 = *88.2k
255 * 18.4320 Mhz /128 = 144k /192 = *96k
256 */
257
258/*
259 * Normal BOSR 0-256/2 = 128, 1-384/2 = 192
260 * USB BOSR 0-250/2 = 125, 1-272/2 = 136
261 */
262static const int bosr_usb_divisor_table[] = {
263 128, 125, 192, 136
264};
265#define LOWER_GROUP ((1<<0) | (1<<1) | (1<<2) | (1<<3) | (1<<6) | (1<<7))
266#define UPPER_GROUP ((1<<8) | (1<<9) | (1<<10) | (1<<11) | (1<<15))
267static const unsigned short sr_valid_mask[] = {
268 LOWER_GROUP|UPPER_GROUP, /* Normal, bosr - 0*/
Troy Kisky26df91c2008-11-05 18:53:28 +0000269 LOWER_GROUP, /* Usb, bosr - 0*/
Troy Kiskybab02122009-11-17 13:51:01 -0700270 LOWER_GROUP|UPPER_GROUP, /* Normal, bosr - 1*/
Troy Kisky26df91c2008-11-05 18:53:28 +0000271 UPPER_GROUP, /* Usb, bosr - 1*/
272};
273/*
274 * Every divisor is a factor of 11*12
275 */
276#define SR_MULT (11*12)
Troy Kiskyccff4b12009-06-05 19:15:58 -0700277#define A(x) (SR_MULT/x)
Troy Kisky26df91c2008-11-05 18:53:28 +0000278static const unsigned char sr_adc_mult_table[] = {
Troy Kiskyccff4b12009-06-05 19:15:58 -0700279 A(2), A(2), A(12), A(12), 0, 0, A(3), A(1),
280 A(2), A(2), A(11), A(11), 0, 0, 0, A(1)
Troy Kisky26df91c2008-11-05 18:53:28 +0000281};
282static const unsigned char sr_dac_mult_table[] = {
Troy Kiskyccff4b12009-06-05 19:15:58 -0700283 A(2), A(12), A(2), A(12), 0, 0, A(3), A(1),
284 A(2), A(11), A(2), A(11), 0, 0, 0, A(1)
Troy Kisky26df91c2008-11-05 18:53:28 +0000285};
286
287static unsigned get_score(int adc, int adc_l, int adc_h, int need_adc,
288 int dac, int dac_l, int dac_h, int need_dac)
289{
290 if ((adc >= adc_l) && (adc <= adc_h) &&
291 (dac >= dac_l) && (dac <= dac_h)) {
292 int diff_adc = need_adc - adc;
293 int diff_dac = need_dac - dac;
294 return abs(diff_adc) + abs(diff_dac);
295 }
296 return UINT_MAX;
297}
298
299static int find_rate(int mclk, u32 need_adc, u32 need_dac)
300{
301 int i, j;
302 int best_i = -1;
303 int best_j = -1;
304 int best_div = 0;
305 unsigned best_score = UINT_MAX;
306 int adc_l, adc_h, dac_l, dac_h;
307
308 need_adc *= SR_MULT;
309 need_dac *= SR_MULT;
310 /*
311 * rates given are +/- 1/32
312 */
313 adc_l = need_adc - (need_adc >> 5);
314 adc_h = need_adc + (need_adc >> 5);
315 dac_l = need_dac - (need_dac >> 5);
316 dac_h = need_dac + (need_dac >> 5);
Mark Brown8d702f22008-11-17 21:42:01 +0000317 for (i = 0; i < ARRAY_SIZE(bosr_usb_divisor_table); i++) {
Troy Kisky26df91c2008-11-05 18:53:28 +0000318 int base = mclk / bosr_usb_divisor_table[i];
319 int mask = sr_valid_mask[i];
Mark Brown8d702f22008-11-17 21:42:01 +0000320 for (j = 0; j < ARRAY_SIZE(sr_adc_mult_table);
321 j++, mask >>= 1) {
Troy Kisky26df91c2008-11-05 18:53:28 +0000322 int adc;
323 int dac;
324 int score;
325 if ((mask & 1) == 0)
326 continue;
327 adc = base * sr_adc_mult_table[j];
328 dac = base * sr_dac_mult_table[j];
329 score = get_score(adc, adc_l, adc_h, need_adc,
330 dac, dac_l, dac_h, need_dac);
331 if (best_score > score) {
332 best_score = score;
333 best_i = i;
334 best_j = j;
335 best_div = 0;
336 }
337 score = get_score((adc >> 1), adc_l, adc_h, need_adc,
338 (dac >> 1), dac_l, dac_h, need_dac);
339 /* prefer to have a /2 */
340 if ((score != UINT_MAX) && (best_score >= score)) {
341 best_score = score;
342 best_i = i;
343 best_j = j;
344 best_div = 1;
345 }
346 }
347 }
348 return (best_j << 2) | best_i | (best_div << TLV320AIC23_CLKIN_SHIFT);
349}
350
Mark Brown8d702f22008-11-17 21:42:01 +0000351#ifdef DEBUG
Troy Kisky26df91c2008-11-05 18:53:28 +0000352static void get_current_sample_rates(struct snd_soc_codec *codec, int mclk,
353 u32 *sample_rate_adc, u32 *sample_rate_dac)
354{
355 int src = tlv320aic23_read_reg_cache(codec, TLV320AIC23_SRATE);
356 int sr = (src >> 2) & 0x0f;
357 int val = (mclk / bosr_usb_divisor_table[src & 3]);
358 int adc = (val * sr_adc_mult_table[sr]) / SR_MULT;
359 int dac = (val * sr_dac_mult_table[sr]) / SR_MULT;
360 if (src & TLV320AIC23_CLKIN_HALF) {
361 adc >>= 1;
362 dac >>= 1;
363 }
364 *sample_rate_adc = adc;
365 *sample_rate_dac = dac;
366}
Mark Brown8d702f22008-11-17 21:42:01 +0000367#endif
Troy Kisky26df91c2008-11-05 18:53:28 +0000368
369static int set_sample_rate_control(struct snd_soc_codec *codec, int mclk,
370 u32 sample_rate_adc, u32 sample_rate_dac)
371{
372 /* Search for the right sample rate */
373 int data = find_rate(mclk, sample_rate_adc, sample_rate_dac);
374 if (data < 0) {
375 printk(KERN_ERR "%s:Invalid rate %u,%u requested\n",
376 __func__, sample_rate_adc, sample_rate_dac);
377 return -EINVAL;
378 }
379 tlv320aic23_write(codec, TLV320AIC23_SRATE, data);
Mark Brown8d702f22008-11-17 21:42:01 +0000380#ifdef DEBUG
381 {
382 u32 adc, dac;
Troy Kisky26df91c2008-11-05 18:53:28 +0000383 get_current_sample_rates(codec, mclk, &adc, &dac);
384 printk(KERN_DEBUG "actual samplerate = %u,%u reg=%x\n",
385 adc, dac, data);
386 }
Mark Brown8d702f22008-11-17 21:42:01 +0000387#endif
Troy Kisky26df91c2008-11-05 18:53:28 +0000388 return 0;
389}
390
Arun KSc1f27192008-10-02 14:45:49 +0530391static int tlv320aic23_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000392 struct snd_pcm_hw_params *params,
393 struct snd_soc_dai *dai)
Arun KSc1f27192008-10-02 14:45:49 +0530394{
395 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000396 struct snd_soc_codec *codec = rtd->codec;
Troy Kisky26df91c2008-11-05 18:53:28 +0000397 u16 iface_reg;
398 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000399 struct aic23 *aic23 = snd_soc_codec_get_drvdata(codec);
Troy Kisky26df91c2008-11-05 18:53:28 +0000400 u32 sample_rate_adc = aic23->requested_adc;
401 u32 sample_rate_dac = aic23->requested_dac;
402 u32 sample_rate = params_rate(params);
403
404 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
405 aic23->requested_dac = sample_rate_dac = sample_rate;
406 if (!sample_rate_adc)
407 sample_rate_adc = sample_rate;
408 } else {
409 aic23->requested_adc = sample_rate_adc = sample_rate;
410 if (!sample_rate_dac)
411 sample_rate_dac = sample_rate;
412 }
413 ret = set_sample_rate_control(codec, aic23->mclk, sample_rate_adc,
414 sample_rate_dac);
415 if (ret < 0)
416 return ret;
Arun KSc1f27192008-10-02 14:45:49 +0530417
418 iface_reg =
419 tlv320aic23_read_reg_cache(codec,
420 TLV320AIC23_DIGT_FMT) & ~(0x03 << 2);
Arun KSc1f27192008-10-02 14:45:49 +0530421 switch (params_format(params)) {
422 case SNDRV_PCM_FORMAT_S16_LE:
423 break;
424 case SNDRV_PCM_FORMAT_S20_3LE:
425 iface_reg |= (0x01 << 2);
426 break;
427 case SNDRV_PCM_FORMAT_S24_LE:
428 iface_reg |= (0x02 << 2);
429 break;
430 case SNDRV_PCM_FORMAT_S32_LE:
431 iface_reg |= (0x03 << 2);
432 break;
433 }
434 tlv320aic23_write(codec, TLV320AIC23_DIGT_FMT, iface_reg);
435
436 return 0;
437}
438
Mark Browndee89c42008-11-18 22:11:38 +0000439static int tlv320aic23_pcm_prepare(struct snd_pcm_substream *substream,
440 struct snd_soc_dai *dai)
Arun KSc1f27192008-10-02 14:45:49 +0530441{
442 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000443 struct snd_soc_codec *codec = rtd->codec;
Arun KSc1f27192008-10-02 14:45:49 +0530444
445 /* set active */
446 tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x0001);
447
448 return 0;
449}
450
Mark Browndee89c42008-11-18 22:11:38 +0000451static void tlv320aic23_shutdown(struct snd_pcm_substream *substream,
452 struct snd_soc_dai *dai)
Arun KSc1f27192008-10-02 14:45:49 +0530453{
454 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000455 struct snd_soc_codec *codec = rtd->codec;
456 struct aic23 *aic23 = snd_soc_codec_get_drvdata(codec);
Arun KSc1f27192008-10-02 14:45:49 +0530457
458 /* deactivate */
459 if (!codec->active) {
460 udelay(50);
461 tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x0);
462 }
Troy Kisky26df91c2008-11-05 18:53:28 +0000463 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
464 aic23->requested_dac = 0;
465 else
466 aic23->requested_adc = 0;
Arun KSc1f27192008-10-02 14:45:49 +0530467}
468
469static int tlv320aic23_mute(struct snd_soc_dai *dai, int mute)
470{
471 struct snd_soc_codec *codec = dai->codec;
472 u16 reg;
473
474 reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_DIGT);
475 if (mute)
476 reg |= TLV320AIC23_DACM_MUTE;
477
478 else
479 reg &= ~TLV320AIC23_DACM_MUTE;
480
481 tlv320aic23_write(codec, TLV320AIC23_DIGT, reg);
482
483 return 0;
484}
485
486static int tlv320aic23_set_dai_fmt(struct snd_soc_dai *codec_dai,
487 unsigned int fmt)
488{
489 struct snd_soc_codec *codec = codec_dai->codec;
490 u16 iface_reg;
491
492 iface_reg =
493 tlv320aic23_read_reg_cache(codec, TLV320AIC23_DIGT_FMT) & (~0x03);
494
495 /* set master/slave audio interface */
496 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
497 case SND_SOC_DAIFMT_CBM_CFM:
498 iface_reg |= TLV320AIC23_MS_MASTER;
499 break;
500 case SND_SOC_DAIFMT_CBS_CFS:
501 break;
502 default:
503 return -EINVAL;
504
505 }
506
507 /* interface format */
508 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
509 case SND_SOC_DAIFMT_I2S:
510 iface_reg |= TLV320AIC23_FOR_I2S;
511 break;
Peter Ujfalusi894bf922009-04-09 12:34:40 +0300512 case SND_SOC_DAIFMT_DSP_A:
513 iface_reg |= TLV320AIC23_LRP_ON;
Jarkko Nikulabd258672008-12-22 10:21:36 +0200514 case SND_SOC_DAIFMT_DSP_B:
Arun KSc1f27192008-10-02 14:45:49 +0530515 iface_reg |= TLV320AIC23_FOR_DSP;
516 break;
517 case SND_SOC_DAIFMT_RIGHT_J:
518 break;
519 case SND_SOC_DAIFMT_LEFT_J:
520 iface_reg |= TLV320AIC23_FOR_LJUST;
521 break;
522 default:
523 return -EINVAL;
524
525 }
526
527 tlv320aic23_write(codec, TLV320AIC23_DIGT_FMT, iface_reg);
528
529 return 0;
530}
531
532static int tlv320aic23_set_dai_sysclk(struct snd_soc_dai *codec_dai,
533 int clk_id, unsigned int freq, int dir)
534{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000535 struct aic23 *aic23 = snd_soc_dai_get_drvdata(codec_dai);
Troy Kisky26df91c2008-11-05 18:53:28 +0000536 aic23->mclk = freq;
537 return 0;
Arun KSc1f27192008-10-02 14:45:49 +0530538}
539
540static int tlv320aic23_set_bias_level(struct snd_soc_codec *codec,
541 enum snd_soc_bias_level level)
542{
543 u16 reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_PWR) & 0xff7f;
544
545 switch (level) {
546 case SND_SOC_BIAS_ON:
547 /* vref/mid, osc on, dac unmute */
Eric Bénard3d5a4512010-06-19 19:33:39 +0200548 reg &= ~(TLV320AIC23_DEVICE_PWR_OFF | TLV320AIC23_OSC_OFF | \
549 TLV320AIC23_DAC_OFF);
Arun KSc1f27192008-10-02 14:45:49 +0530550 tlv320aic23_write(codec, TLV320AIC23_PWR, reg);
551 break;
552 case SND_SOC_BIAS_PREPARE:
553 break;
554 case SND_SOC_BIAS_STANDBY:
555 /* everything off except vref/vmid, */
Eric Bénard3d5a4512010-06-19 19:33:39 +0200556 tlv320aic23_write(codec, TLV320AIC23_PWR, reg | \
557 TLV320AIC23_CLK_OFF);
Arun KSc1f27192008-10-02 14:45:49 +0530558 break;
559 case SND_SOC_BIAS_OFF:
560 /* everything off, dac mute, inactive */
561 tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x0);
562 tlv320aic23_write(codec, TLV320AIC23_PWR, 0xffff);
563 break;
564 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200565 codec->dapm.bias_level = level;
Arun KSc1f27192008-10-02 14:45:49 +0530566 return 0;
567}
568
569#define AIC23_RATES SNDRV_PCM_RATE_8000_96000
570#define AIC23_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
571 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)
572
Eric Miao6335d052009-03-03 09:41:00 +0800573static struct snd_soc_dai_ops tlv320aic23_dai_ops = {
574 .prepare = tlv320aic23_pcm_prepare,
575 .hw_params = tlv320aic23_hw_params,
576 .shutdown = tlv320aic23_shutdown,
577 .digital_mute = tlv320aic23_mute,
578 .set_fmt = tlv320aic23_set_dai_fmt,
579 .set_sysclk = tlv320aic23_set_dai_sysclk,
580};
581
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000582static struct snd_soc_dai_driver tlv320aic23_dai = {
583 .name = "tlv320aic23-hifi",
Arun KSc1f27192008-10-02 14:45:49 +0530584 .playback = {
585 .stream_name = "Playback",
586 .channels_min = 2,
587 .channels_max = 2,
588 .rates = AIC23_RATES,
589 .formats = AIC23_FORMATS,},
590 .capture = {
591 .stream_name = "Capture",
592 .channels_min = 2,
593 .channels_max = 2,
594 .rates = AIC23_RATES,
595 .formats = AIC23_FORMATS,},
Eric Miao6335d052009-03-03 09:41:00 +0800596 .ops = &tlv320aic23_dai_ops,
Arun KSc1f27192008-10-02 14:45:49 +0530597};
Arun KSc1f27192008-10-02 14:45:49 +0530598
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000599static int tlv320aic23_suspend(struct snd_soc_codec *codec,
Arun KSc1f27192008-10-02 14:45:49 +0530600 pm_message_t state)
601{
Arun KSc1f27192008-10-02 14:45:49 +0530602 tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_OFF);
603
604 return 0;
605}
606
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000607static int tlv320aic23_resume(struct snd_soc_codec *codec)
Arun KSc1f27192008-10-02 14:45:49 +0530608{
Arun KSc1f27192008-10-02 14:45:49 +0530609 u16 reg;
610
611 /* Sync reg_cache with the hardware */
Anuj Aggarwal3e59aaa2010-01-29 13:58:55 +0530612 for (reg = 0; reg <= TLV320AIC23_ACTIVE; reg++) {
Arun KSc1f27192008-10-02 14:45:49 +0530613 u16 val = tlv320aic23_read_reg_cache(codec, reg);
614 tlv320aic23_write(codec, reg, val);
615 }
Arun KSc1f27192008-10-02 14:45:49 +0530616 tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
Arun KSc1f27192008-10-02 14:45:49 +0530617
618 return 0;
619}
620
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000621static int tlv320aic23_probe(struct snd_soc_codec *codec)
Arun KSc1f27192008-10-02 14:45:49 +0530622{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000623 struct aic23 *aic23 = snd_soc_codec_get_drvdata(codec);
624 int reg;
Arun KSc1f27192008-10-02 14:45:49 +0530625
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000626 printk(KERN_INFO "AIC23 Audio Codec %s\n", AIC23_VERSION);
627 codec->control_data = aic23->control_data;
628 codec->hw_write = (hw_write_t)i2c_master_send;
629 codec->hw_read = NULL;
Arun KSc1f27192008-10-02 14:45:49 +0530630
631 /* Reset codec */
632 tlv320aic23_write(codec, TLV320AIC23_RESET, 0);
633
Arun KSc1f27192008-10-02 14:45:49 +0530634 /* power on device */
635 tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
636
637 tlv320aic23_write(codec, TLV320AIC23_DIGT, TLV320AIC23_DEEMP_44K);
638
639 /* Unmute input */
640 reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_LINVOL);
641 tlv320aic23_write(codec, TLV320AIC23_LINVOL,
642 (reg & (~TLV320AIC23_LIM_MUTED)) |
643 (TLV320AIC23_LRS_ENABLED));
644
645 reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_RINVOL);
646 tlv320aic23_write(codec, TLV320AIC23_RINVOL,
647 (reg & (~TLV320AIC23_LIM_MUTED)) |
648 TLV320AIC23_LRS_ENABLED);
649
650 reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_ANLG);
651 tlv320aic23_write(codec, TLV320AIC23_ANLG,
652 (reg) & (~TLV320AIC23_BYPASS_ON) &
653 (~TLV320AIC23_MICM_MUTED));
654
655 /* Default output volume */
656 tlv320aic23_write(codec, TLV320AIC23_LCHNVOL,
657 TLV320AIC23_DEFAULT_OUT_VOL &
658 TLV320AIC23_OUT_VOL_MASK);
659 tlv320aic23_write(codec, TLV320AIC23_RCHNVOL,
660 TLV320AIC23_DEFAULT_OUT_VOL &
661 TLV320AIC23_OUT_VOL_MASK);
662
663 tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x1);
664
Ian Molton3e8e1952009-01-09 00:23:21 +0000665 snd_soc_add_controls(codec, tlv320aic23_snd_controls,
666 ARRAY_SIZE(tlv320aic23_snd_controls));
Arun KSc1f27192008-10-02 14:45:49 +0530667
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000668 return 0;
Arun KSc1f27192008-10-02 14:45:49 +0530669}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000670
671static int tlv320aic23_remove(struct snd_soc_codec *codec)
672{
673 tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_OFF);
674 return 0;
675}
676
677static struct snd_soc_codec_driver soc_codec_dev_tlv320aic23 = {
678 .reg_cache_size = ARRAY_SIZE(tlv320aic23_reg),
679 .reg_word_size = sizeof(u16),
680 .reg_cache_default = tlv320aic23_reg,
681 .probe = tlv320aic23_probe,
682 .remove = tlv320aic23_remove,
683 .suspend = tlv320aic23_suspend,
684 .resume = tlv320aic23_resume,
685 .read = tlv320aic23_read_reg_cache,
686 .write = tlv320aic23_write,
687 .set_bias_level = tlv320aic23_set_bias_level,
Lu Guanquna7dca702011-03-30 21:53:16 +0800688 .dapm_widgets = tlv320aic23_dapm_widgets,
689 .num_dapm_widgets = ARRAY_SIZE(tlv320aic23_dapm_widgets),
690 .dapm_routes = tlv320aic23_intercon,
691 .num_dapm_routes = ARRAY_SIZE(tlv320aic23_intercon),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000692};
Arun KSc1f27192008-10-02 14:45:49 +0530693
694#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
695/*
696 * If the i2c layer weren't so broken, we could pass this kind of data
697 * around
698 */
699static int tlv320aic23_codec_probe(struct i2c_client *i2c,
700 const struct i2c_device_id *i2c_id)
701{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000702 struct aic23 *aic23;
Arun KSc1f27192008-10-02 14:45:49 +0530703 int ret;
704
705 if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
706 return -EINVAL;
707
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000708 aic23 = kzalloc(sizeof(struct aic23), GFP_KERNEL);
709 if (aic23 == NULL)
710 return -ENOMEM;
Arun KSc1f27192008-10-02 14:45:49 +0530711
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000712 i2c_set_clientdata(i2c, aic23);
713 aic23->control_data = i2c;
714 aic23->control_type = SND_SOC_I2C;
Arun KSc1f27192008-10-02 14:45:49 +0530715
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000716 ret = snd_soc_register_codec(&i2c->dev,
717 &soc_codec_dev_tlv320aic23, &tlv320aic23_dai, 1);
718 if (ret < 0)
719 kfree(aic23);
Arun KSc1f27192008-10-02 14:45:49 +0530720 return ret;
721}
722static int __exit tlv320aic23_i2c_remove(struct i2c_client *i2c)
723{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000724 snd_soc_unregister_codec(&i2c->dev);
725 kfree(i2c_get_clientdata(i2c));
Arun KSc1f27192008-10-02 14:45:49 +0530726 return 0;
727}
728
729static const struct i2c_device_id tlv320aic23_id[] = {
730 {"tlv320aic23", 0},
731 {}
732};
733
734MODULE_DEVICE_TABLE(i2c, tlv320aic23_id);
735
736static struct i2c_driver tlv320aic23_i2c_driver = {
737 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000738 .name = "tlv320aic23-codec",
Arun KSc1f27192008-10-02 14:45:49 +0530739 },
740 .probe = tlv320aic23_codec_probe,
741 .remove = __exit_p(tlv320aic23_i2c_remove),
742 .id_table = tlv320aic23_id,
743};
744
745#endif
746
Takashi Iwaic9b3a402008-12-10 07:47:22 +0100747static int __init tlv320aic23_modinit(void)
Mark Brown64089b82008-12-08 19:17:58 +0000748{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000749 int ret;
750#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
751 ret = i2c_add_driver(&tlv320aic23_i2c_driver);
752 if (ret != 0) {
753 printk(KERN_ERR "Failed to register TLV320AIC23 I2C driver: %d\n",
754 ret);
755 }
756#endif
757 return ret;
Mark Brown64089b82008-12-08 19:17:58 +0000758}
759module_init(tlv320aic23_modinit);
760
761static void __exit tlv320aic23_exit(void)
762{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000763#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
764 i2c_del_driver(&tlv320aic23_i2c_driver);
765#endif
Mark Brown64089b82008-12-08 19:17:58 +0000766}
767module_exit(tlv320aic23_exit);
768
Arun KSc1f27192008-10-02 14:45:49 +0530769MODULE_DESCRIPTION("ASoC TLV320AIC23 codec driver");
770MODULE_AUTHOR("Arun KS <arunks@mistralsolutions.com>");
771MODULE_LICENSE("GPL");