blob: 336de8f69a026e9a6da20af04f3b821d5ba69a33 [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
Arun KSc1f27192008-10-02 14:45:49 +053050static const char *rec_src_text[] = { "Line", "Mic" };
51static const char *deemph_text[] = {"None", "32Khz", "44.1Khz", "48Khz"};
Arun KSc1f27192008-10-02 14:45:49 +053052
53static const struct soc_enum rec_src_enum =
54 SOC_ENUM_SINGLE(TLV320AIC23_ANLG, 2, 2, rec_src_text);
55
56static const struct snd_kcontrol_new tlv320aic23_rec_src_mux_controls =
57SOC_DAPM_ENUM("Input Select", rec_src_enum);
58
59static const struct soc_enum tlv320aic23_rec_src =
60 SOC_ENUM_SINGLE(TLV320AIC23_ANLG, 2, 2, rec_src_text);
61static const struct soc_enum tlv320aic23_deemph =
62 SOC_ENUM_SINGLE(TLV320AIC23_DIGT, 1, 4, deemph_text);
Arun KSc1f27192008-10-02 14:45:49 +053063
64static const DECLARE_TLV_DB_SCALE(out_gain_tlv, -12100, 100, 0);
65static const DECLARE_TLV_DB_SCALE(input_gain_tlv, -1725, 75, 0);
Arun KSdf91ddf2008-10-03 17:07:30 +053066static const DECLARE_TLV_DB_SCALE(sidetone_vol_tlv, -1800, 300, 0);
67
68static int snd_soc_tlv320aic23_put_volsw(struct snd_kcontrol *kcontrol,
69 struct snd_ctl_elem_value *ucontrol)
70{
71 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
72 u16 val, reg;
73
74 val = (ucontrol->value.integer.value[0] & 0x07);
75
76 /* linear conversion to userspace
77 * 000 = -6db
78 * 001 = -9db
79 * 010 = -12db
80 * 011 = -18db (Min)
81 * 100 = 0db (Max)
82 */
83 val = (val >= 4) ? 4 : (3 - val);
84
Axel Linf9dfbf92011-10-13 15:06:43 +080085 reg = snd_soc_read(codec, TLV320AIC23_ANLG) & (~0x1C0);
86 snd_soc_write(codec, TLV320AIC23_ANLG, reg | (val << 6));
Arun KSdf91ddf2008-10-03 17:07:30 +053087
88 return 0;
89}
90
91static int snd_soc_tlv320aic23_get_volsw(struct snd_kcontrol *kcontrol,
92 struct snd_ctl_elem_value *ucontrol)
93{
94 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
95 u16 val;
96
Axel Linf9dfbf92011-10-13 15:06:43 +080097 val = snd_soc_read(codec, TLV320AIC23_ANLG) & (0x1C0);
Arun KSdf91ddf2008-10-03 17:07:30 +053098 val = val >> 6;
99 val = (val >= 4) ? 4 : (3 - val);
100 ucontrol->value.integer.value[0] = val;
101 return 0;
102
103}
104
Arun KSc1f27192008-10-02 14:45:49 +0530105static const struct snd_kcontrol_new tlv320aic23_snd_controls[] = {
106 SOC_DOUBLE_R_TLV("Digital Playback Volume", TLV320AIC23_LCHNVOL,
107 TLV320AIC23_RCHNVOL, 0, 127, 0, out_gain_tlv),
108 SOC_SINGLE("Digital Playback Switch", TLV320AIC23_DIGT, 3, 1, 1),
109 SOC_DOUBLE_R("Line Input Switch", TLV320AIC23_LINVOL,
110 TLV320AIC23_RINVOL, 7, 1, 0),
111 SOC_DOUBLE_R_TLV("Line Input Volume", TLV320AIC23_LINVOL,
112 TLV320AIC23_RINVOL, 0, 31, 0, input_gain_tlv),
113 SOC_SINGLE("Mic Input Switch", TLV320AIC23_ANLG, 1, 1, 1),
114 SOC_SINGLE("Mic Booster Switch", TLV320AIC23_ANLG, 0, 1, 0),
Peter Ujfalusi0f9887d2011-10-05 10:29:19 +0300115 SOC_SINGLE_EXT_TLV("Sidetone Volume", TLV320AIC23_ANLG, 6, 4, 0,
116 snd_soc_tlv320aic23_get_volsw,
117 snd_soc_tlv320aic23_put_volsw, sidetone_vol_tlv),
Arun KSc1f27192008-10-02 14:45:49 +0530118 SOC_ENUM("Playback De-emphasis", tlv320aic23_deemph),
119};
120
Arun KSc1f27192008-10-02 14:45:49 +0530121/* PGA Mixer controls for Line and Mic switch */
122static const struct snd_kcontrol_new tlv320aic23_output_mixer_controls[] = {
123 SOC_DAPM_SINGLE("Line Bypass Switch", TLV320AIC23_ANLG, 3, 1, 0),
124 SOC_DAPM_SINGLE("Mic Sidetone Switch", TLV320AIC23_ANLG, 5, 1, 0),
125 SOC_DAPM_SINGLE("Playback Switch", TLV320AIC23_ANLG, 4, 1, 0),
126};
127
128static const struct snd_soc_dapm_widget tlv320aic23_dapm_widgets[] = {
129 SND_SOC_DAPM_DAC("DAC", "Playback", TLV320AIC23_PWR, 3, 1),
130 SND_SOC_DAPM_ADC("ADC", "Capture", TLV320AIC23_PWR, 2, 1),
131 SND_SOC_DAPM_MUX("Capture Source", SND_SOC_NOPM, 0, 0,
132 &tlv320aic23_rec_src_mux_controls),
133 SND_SOC_DAPM_MIXER("Output Mixer", TLV320AIC23_PWR, 4, 1,
134 &tlv320aic23_output_mixer_controls[0],
135 ARRAY_SIZE(tlv320aic23_output_mixer_controls)),
136 SND_SOC_DAPM_PGA("Line Input", TLV320AIC23_PWR, 0, 1, NULL, 0),
137 SND_SOC_DAPM_PGA("Mic Input", TLV320AIC23_PWR, 1, 1, NULL, 0),
138
139 SND_SOC_DAPM_OUTPUT("LHPOUT"),
140 SND_SOC_DAPM_OUTPUT("RHPOUT"),
141 SND_SOC_DAPM_OUTPUT("LOUT"),
142 SND_SOC_DAPM_OUTPUT("ROUT"),
143
144 SND_SOC_DAPM_INPUT("LLINEIN"),
145 SND_SOC_DAPM_INPUT("RLINEIN"),
146
147 SND_SOC_DAPM_INPUT("MICIN"),
148};
149
Lu Guanquna7dca702011-03-30 21:53:16 +0800150static const struct snd_soc_dapm_route tlv320aic23_intercon[] = {
Arun KSc1f27192008-10-02 14:45:49 +0530151 /* Output Mixer */
152 {"Output Mixer", "Line Bypass Switch", "Line Input"},
153 {"Output Mixer", "Playback Switch", "DAC"},
154 {"Output Mixer", "Mic Sidetone Switch", "Mic Input"},
155
156 /* Outputs */
157 {"RHPOUT", NULL, "Output Mixer"},
158 {"LHPOUT", NULL, "Output Mixer"},
159 {"LOUT", NULL, "Output Mixer"},
160 {"ROUT", NULL, "Output Mixer"},
161
162 /* Inputs */
163 {"Line Input", "NULL", "LLINEIN"},
164 {"Line Input", "NULL", "RLINEIN"},
165
166 {"Mic Input", "NULL", "MICIN"},
167
168 /* input mux */
169 {"Capture Source", "Line", "Line Input"},
170 {"Capture Source", "Mic", "Mic Input"},
171 {"ADC", NULL, "Capture Source"},
172
173};
174
Troy Kisky26df91c2008-11-05 18:53:28 +0000175/* AIC23 driver data */
176struct aic23 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000177 enum snd_soc_control_type control_type;
Troy Kisky26df91c2008-11-05 18:53:28 +0000178 int mclk;
179 int requested_adc;
180 int requested_dac;
Arun KSc1f27192008-10-02 14:45:49 +0530181};
182
Troy Kisky26df91c2008-11-05 18:53:28 +0000183/*
184 * Common Crystals used
185 * 11.2896 Mhz /128 = *88.2k /192 = 58.8k
186 * 12.0000 Mhz /125 = *96k /136 = 88.235K
187 * 12.2880 Mhz /128 = *96k /192 = 64k
188 * 16.9344 Mhz /128 = 132.3k /192 = *88.2k
189 * 18.4320 Mhz /128 = 144k /192 = *96k
190 */
191
192/*
193 * Normal BOSR 0-256/2 = 128, 1-384/2 = 192
194 * USB BOSR 0-250/2 = 125, 1-272/2 = 136
195 */
196static const int bosr_usb_divisor_table[] = {
197 128, 125, 192, 136
198};
199#define LOWER_GROUP ((1<<0) | (1<<1) | (1<<2) | (1<<3) | (1<<6) | (1<<7))
200#define UPPER_GROUP ((1<<8) | (1<<9) | (1<<10) | (1<<11) | (1<<15))
201static const unsigned short sr_valid_mask[] = {
202 LOWER_GROUP|UPPER_GROUP, /* Normal, bosr - 0*/
Troy Kisky26df91c2008-11-05 18:53:28 +0000203 LOWER_GROUP, /* Usb, bosr - 0*/
Troy Kiskybab02122009-11-17 13:51:01 -0700204 LOWER_GROUP|UPPER_GROUP, /* Normal, bosr - 1*/
Troy Kisky26df91c2008-11-05 18:53:28 +0000205 UPPER_GROUP, /* Usb, bosr - 1*/
206};
207/*
208 * Every divisor is a factor of 11*12
209 */
210#define SR_MULT (11*12)
Troy Kiskyccff4b12009-06-05 19:15:58 -0700211#define A(x) (SR_MULT/x)
Troy Kisky26df91c2008-11-05 18:53:28 +0000212static const unsigned char sr_adc_mult_table[] = {
Troy Kiskyccff4b12009-06-05 19:15:58 -0700213 A(2), A(2), A(12), A(12), 0, 0, A(3), A(1),
214 A(2), A(2), A(11), A(11), 0, 0, 0, A(1)
Troy Kisky26df91c2008-11-05 18:53:28 +0000215};
216static const unsigned char sr_dac_mult_table[] = {
Troy Kiskyccff4b12009-06-05 19:15:58 -0700217 A(2), A(12), A(2), A(12), 0, 0, A(3), A(1),
218 A(2), A(11), A(2), A(11), 0, 0, 0, A(1)
Troy Kisky26df91c2008-11-05 18:53:28 +0000219};
220
221static unsigned get_score(int adc, int adc_l, int adc_h, int need_adc,
222 int dac, int dac_l, int dac_h, int need_dac)
223{
224 if ((adc >= adc_l) && (adc <= adc_h) &&
225 (dac >= dac_l) && (dac <= dac_h)) {
226 int diff_adc = need_adc - adc;
227 int diff_dac = need_dac - dac;
228 return abs(diff_adc) + abs(diff_dac);
229 }
230 return UINT_MAX;
231}
232
233static int find_rate(int mclk, u32 need_adc, u32 need_dac)
234{
235 int i, j;
236 int best_i = -1;
237 int best_j = -1;
238 int best_div = 0;
239 unsigned best_score = UINT_MAX;
240 int adc_l, adc_h, dac_l, dac_h;
241
242 need_adc *= SR_MULT;
243 need_dac *= SR_MULT;
244 /*
245 * rates given are +/- 1/32
246 */
247 adc_l = need_adc - (need_adc >> 5);
248 adc_h = need_adc + (need_adc >> 5);
249 dac_l = need_dac - (need_dac >> 5);
250 dac_h = need_dac + (need_dac >> 5);
Mark Brown8d702f22008-11-17 21:42:01 +0000251 for (i = 0; i < ARRAY_SIZE(bosr_usb_divisor_table); i++) {
Troy Kisky26df91c2008-11-05 18:53:28 +0000252 int base = mclk / bosr_usb_divisor_table[i];
253 int mask = sr_valid_mask[i];
Mark Brown8d702f22008-11-17 21:42:01 +0000254 for (j = 0; j < ARRAY_SIZE(sr_adc_mult_table);
255 j++, mask >>= 1) {
Troy Kisky26df91c2008-11-05 18:53:28 +0000256 int adc;
257 int dac;
258 int score;
259 if ((mask & 1) == 0)
260 continue;
261 adc = base * sr_adc_mult_table[j];
262 dac = base * sr_dac_mult_table[j];
263 score = get_score(adc, adc_l, adc_h, need_adc,
264 dac, dac_l, dac_h, need_dac);
265 if (best_score > score) {
266 best_score = score;
267 best_i = i;
268 best_j = j;
269 best_div = 0;
270 }
271 score = get_score((adc >> 1), adc_l, adc_h, need_adc,
272 (dac >> 1), dac_l, dac_h, need_dac);
273 /* prefer to have a /2 */
274 if ((score != UINT_MAX) && (best_score >= score)) {
275 best_score = score;
276 best_i = i;
277 best_j = j;
278 best_div = 1;
279 }
280 }
281 }
282 return (best_j << 2) | best_i | (best_div << TLV320AIC23_CLKIN_SHIFT);
283}
284
Mark Brown8d702f22008-11-17 21:42:01 +0000285#ifdef DEBUG
Troy Kisky26df91c2008-11-05 18:53:28 +0000286static void get_current_sample_rates(struct snd_soc_codec *codec, int mclk,
287 u32 *sample_rate_adc, u32 *sample_rate_dac)
288{
Axel Linf9dfbf92011-10-13 15:06:43 +0800289 int src = snd_soc_read(codec, TLV320AIC23_SRATE);
Troy Kisky26df91c2008-11-05 18:53:28 +0000290 int sr = (src >> 2) & 0x0f;
291 int val = (mclk / bosr_usb_divisor_table[src & 3]);
292 int adc = (val * sr_adc_mult_table[sr]) / SR_MULT;
293 int dac = (val * sr_dac_mult_table[sr]) / SR_MULT;
294 if (src & TLV320AIC23_CLKIN_HALF) {
295 adc >>= 1;
296 dac >>= 1;
297 }
298 *sample_rate_adc = adc;
299 *sample_rate_dac = dac;
300}
Mark Brown8d702f22008-11-17 21:42:01 +0000301#endif
Troy Kisky26df91c2008-11-05 18:53:28 +0000302
303static int set_sample_rate_control(struct snd_soc_codec *codec, int mclk,
304 u32 sample_rate_adc, u32 sample_rate_dac)
305{
306 /* Search for the right sample rate */
307 int data = find_rate(mclk, sample_rate_adc, sample_rate_dac);
308 if (data < 0) {
309 printk(KERN_ERR "%s:Invalid rate %u,%u requested\n",
310 __func__, sample_rate_adc, sample_rate_dac);
311 return -EINVAL;
312 }
Axel Linf9dfbf92011-10-13 15:06:43 +0800313 snd_soc_write(codec, TLV320AIC23_SRATE, data);
Mark Brown8d702f22008-11-17 21:42:01 +0000314#ifdef DEBUG
315 {
316 u32 adc, dac;
Troy Kisky26df91c2008-11-05 18:53:28 +0000317 get_current_sample_rates(codec, mclk, &adc, &dac);
318 printk(KERN_DEBUG "actual samplerate = %u,%u reg=%x\n",
319 adc, dac, data);
320 }
Mark Brown8d702f22008-11-17 21:42:01 +0000321#endif
Troy Kisky26df91c2008-11-05 18:53:28 +0000322 return 0;
323}
324
Arun KSc1f27192008-10-02 14:45:49 +0530325static int tlv320aic23_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000326 struct snd_pcm_hw_params *params,
327 struct snd_soc_dai *dai)
Arun KSc1f27192008-10-02 14:45:49 +0530328{
329 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000330 struct snd_soc_codec *codec = rtd->codec;
Troy Kisky26df91c2008-11-05 18:53:28 +0000331 u16 iface_reg;
332 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000333 struct aic23 *aic23 = snd_soc_codec_get_drvdata(codec);
Troy Kisky26df91c2008-11-05 18:53:28 +0000334 u32 sample_rate_adc = aic23->requested_adc;
335 u32 sample_rate_dac = aic23->requested_dac;
336 u32 sample_rate = params_rate(params);
337
338 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
339 aic23->requested_dac = sample_rate_dac = sample_rate;
340 if (!sample_rate_adc)
341 sample_rate_adc = sample_rate;
342 } else {
343 aic23->requested_adc = sample_rate_adc = sample_rate;
344 if (!sample_rate_dac)
345 sample_rate_dac = sample_rate;
346 }
347 ret = set_sample_rate_control(codec, aic23->mclk, sample_rate_adc,
348 sample_rate_dac);
349 if (ret < 0)
350 return ret;
Arun KSc1f27192008-10-02 14:45:49 +0530351
Axel Linf9dfbf92011-10-13 15:06:43 +0800352 iface_reg = snd_soc_read(codec, TLV320AIC23_DIGT_FMT) & ~(0x03 << 2);
353
Arun KSc1f27192008-10-02 14:45:49 +0530354 switch (params_format(params)) {
355 case SNDRV_PCM_FORMAT_S16_LE:
356 break;
357 case SNDRV_PCM_FORMAT_S20_3LE:
358 iface_reg |= (0x01 << 2);
359 break;
360 case SNDRV_PCM_FORMAT_S24_LE:
361 iface_reg |= (0x02 << 2);
362 break;
363 case SNDRV_PCM_FORMAT_S32_LE:
364 iface_reg |= (0x03 << 2);
365 break;
366 }
Axel Linf9dfbf92011-10-13 15:06:43 +0800367 snd_soc_write(codec, TLV320AIC23_DIGT_FMT, iface_reg);
Arun KSc1f27192008-10-02 14:45:49 +0530368
369 return 0;
370}
371
Mark Browndee89c42008-11-18 22:11:38 +0000372static int tlv320aic23_pcm_prepare(struct snd_pcm_substream *substream,
373 struct snd_soc_dai *dai)
Arun KSc1f27192008-10-02 14:45:49 +0530374{
375 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000376 struct snd_soc_codec *codec = rtd->codec;
Arun KSc1f27192008-10-02 14:45:49 +0530377
378 /* set active */
Axel Linf9dfbf92011-10-13 15:06:43 +0800379 snd_soc_write(codec, TLV320AIC23_ACTIVE, 0x0001);
Arun KSc1f27192008-10-02 14:45:49 +0530380
381 return 0;
382}
383
Mark Browndee89c42008-11-18 22:11:38 +0000384static void tlv320aic23_shutdown(struct snd_pcm_substream *substream,
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;
389 struct aic23 *aic23 = snd_soc_codec_get_drvdata(codec);
Arun KSc1f27192008-10-02 14:45:49 +0530390
391 /* deactivate */
392 if (!codec->active) {
393 udelay(50);
Axel Linf9dfbf92011-10-13 15:06:43 +0800394 snd_soc_write(codec, TLV320AIC23_ACTIVE, 0x0);
Arun KSc1f27192008-10-02 14:45:49 +0530395 }
Troy Kisky26df91c2008-11-05 18:53:28 +0000396 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
397 aic23->requested_dac = 0;
398 else
399 aic23->requested_adc = 0;
Arun KSc1f27192008-10-02 14:45:49 +0530400}
401
402static int tlv320aic23_mute(struct snd_soc_dai *dai, int mute)
403{
404 struct snd_soc_codec *codec = dai->codec;
405 u16 reg;
406
Axel Linf9dfbf92011-10-13 15:06:43 +0800407 reg = snd_soc_read(codec, TLV320AIC23_DIGT);
Arun KSc1f27192008-10-02 14:45:49 +0530408 if (mute)
409 reg |= TLV320AIC23_DACM_MUTE;
410
411 else
412 reg &= ~TLV320AIC23_DACM_MUTE;
413
Axel Linf9dfbf92011-10-13 15:06:43 +0800414 snd_soc_write(codec, TLV320AIC23_DIGT, reg);
Arun KSc1f27192008-10-02 14:45:49 +0530415
416 return 0;
417}
418
419static int tlv320aic23_set_dai_fmt(struct snd_soc_dai *codec_dai,
420 unsigned int fmt)
421{
422 struct snd_soc_codec *codec = codec_dai->codec;
423 u16 iface_reg;
424
Axel Linf9dfbf92011-10-13 15:06:43 +0800425 iface_reg = snd_soc_read(codec, TLV320AIC23_DIGT_FMT) & (~0x03);
Arun KSc1f27192008-10-02 14:45:49 +0530426
427 /* set master/slave audio interface */
428 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
429 case SND_SOC_DAIFMT_CBM_CFM:
430 iface_reg |= TLV320AIC23_MS_MASTER;
431 break;
432 case SND_SOC_DAIFMT_CBS_CFS:
Axel Linb01a3d62011-10-27 16:35:49 +0800433 iface_reg &= ~TLV320AIC23_MS_MASTER;
Arun KSc1f27192008-10-02 14:45:49 +0530434 break;
435 default:
436 return -EINVAL;
437
438 }
439
440 /* interface format */
441 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
442 case SND_SOC_DAIFMT_I2S:
443 iface_reg |= TLV320AIC23_FOR_I2S;
444 break;
Peter Ujfalusi894bf922009-04-09 12:34:40 +0300445 case SND_SOC_DAIFMT_DSP_A:
446 iface_reg |= TLV320AIC23_LRP_ON;
Jarkko Nikulabd258672008-12-22 10:21:36 +0200447 case SND_SOC_DAIFMT_DSP_B:
Arun KSc1f27192008-10-02 14:45:49 +0530448 iface_reg |= TLV320AIC23_FOR_DSP;
449 break;
450 case SND_SOC_DAIFMT_RIGHT_J:
451 break;
452 case SND_SOC_DAIFMT_LEFT_J:
453 iface_reg |= TLV320AIC23_FOR_LJUST;
454 break;
455 default:
456 return -EINVAL;
457
458 }
459
Axel Linf9dfbf92011-10-13 15:06:43 +0800460 snd_soc_write(codec, TLV320AIC23_DIGT_FMT, iface_reg);
Arun KSc1f27192008-10-02 14:45:49 +0530461
462 return 0;
463}
464
465static int tlv320aic23_set_dai_sysclk(struct snd_soc_dai *codec_dai,
466 int clk_id, unsigned int freq, int dir)
467{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000468 struct aic23 *aic23 = snd_soc_dai_get_drvdata(codec_dai);
Troy Kisky26df91c2008-11-05 18:53:28 +0000469 aic23->mclk = freq;
470 return 0;
Arun KSc1f27192008-10-02 14:45:49 +0530471}
472
473static int tlv320aic23_set_bias_level(struct snd_soc_codec *codec,
474 enum snd_soc_bias_level level)
475{
Axel Linf9dfbf92011-10-13 15:06:43 +0800476 u16 reg = snd_soc_read(codec, TLV320AIC23_PWR) & 0xff7f;
Arun KSc1f27192008-10-02 14:45:49 +0530477
478 switch (level) {
479 case SND_SOC_BIAS_ON:
480 /* vref/mid, osc on, dac unmute */
Eric Bénard3d5a4512010-06-19 19:33:39 +0200481 reg &= ~(TLV320AIC23_DEVICE_PWR_OFF | TLV320AIC23_OSC_OFF | \
482 TLV320AIC23_DAC_OFF);
Axel Linf9dfbf92011-10-13 15:06:43 +0800483 snd_soc_write(codec, TLV320AIC23_PWR, reg);
Arun KSc1f27192008-10-02 14:45:49 +0530484 break;
485 case SND_SOC_BIAS_PREPARE:
486 break;
487 case SND_SOC_BIAS_STANDBY:
488 /* everything off except vref/vmid, */
Axel Linf9dfbf92011-10-13 15:06:43 +0800489 snd_soc_write(codec, TLV320AIC23_PWR,
490 reg | TLV320AIC23_CLK_OFF);
Arun KSc1f27192008-10-02 14:45:49 +0530491 break;
492 case SND_SOC_BIAS_OFF:
493 /* everything off, dac mute, inactive */
Axel Linf9dfbf92011-10-13 15:06:43 +0800494 snd_soc_write(codec, TLV320AIC23_ACTIVE, 0x0);
495 snd_soc_write(codec, TLV320AIC23_PWR, 0xffff);
Arun KSc1f27192008-10-02 14:45:49 +0530496 break;
497 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200498 codec->dapm.bias_level = level;
Arun KSc1f27192008-10-02 14:45:49 +0530499 return 0;
500}
501
502#define AIC23_RATES SNDRV_PCM_RATE_8000_96000
503#define AIC23_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
504 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)
505
Eric Miao6335d052009-03-03 09:41:00 +0800506static struct snd_soc_dai_ops tlv320aic23_dai_ops = {
507 .prepare = tlv320aic23_pcm_prepare,
508 .hw_params = tlv320aic23_hw_params,
509 .shutdown = tlv320aic23_shutdown,
510 .digital_mute = tlv320aic23_mute,
511 .set_fmt = tlv320aic23_set_dai_fmt,
512 .set_sysclk = tlv320aic23_set_dai_sysclk,
513};
514
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000515static struct snd_soc_dai_driver tlv320aic23_dai = {
516 .name = "tlv320aic23-hifi",
Arun KSc1f27192008-10-02 14:45:49 +0530517 .playback = {
518 .stream_name = "Playback",
519 .channels_min = 2,
520 .channels_max = 2,
521 .rates = AIC23_RATES,
522 .formats = AIC23_FORMATS,},
523 .capture = {
524 .stream_name = "Capture",
525 .channels_min = 2,
526 .channels_max = 2,
527 .rates = AIC23_RATES,
528 .formats = AIC23_FORMATS,},
Eric Miao6335d052009-03-03 09:41:00 +0800529 .ops = &tlv320aic23_dai_ops,
Arun KSc1f27192008-10-02 14:45:49 +0530530};
Arun KSc1f27192008-10-02 14:45:49 +0530531
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000532static int tlv320aic23_suspend(struct snd_soc_codec *codec,
Arun KSc1f27192008-10-02 14:45:49 +0530533 pm_message_t state)
534{
Arun KSc1f27192008-10-02 14:45:49 +0530535 tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_OFF);
536
537 return 0;
538}
539
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000540static int tlv320aic23_resume(struct snd_soc_codec *codec)
Arun KSc1f27192008-10-02 14:45:49 +0530541{
Axel Linf9dfbf92011-10-13 15:06:43 +0800542 snd_soc_cache_sync(codec);
Arun KSc1f27192008-10-02 14:45:49 +0530543 tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
Arun KSc1f27192008-10-02 14:45:49 +0530544
545 return 0;
546}
547
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000548static int tlv320aic23_probe(struct snd_soc_codec *codec)
Arun KSc1f27192008-10-02 14:45:49 +0530549{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000550 struct aic23 *aic23 = snd_soc_codec_get_drvdata(codec);
Axel Linf9dfbf92011-10-13 15:06:43 +0800551 int ret;
Arun KSc1f27192008-10-02 14:45:49 +0530552
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000553 printk(KERN_INFO "AIC23 Audio Codec %s\n", AIC23_VERSION);
Axel Linf9dfbf92011-10-13 15:06:43 +0800554
555 ret = snd_soc_codec_set_cache_io(codec, 7, 9, aic23->control_type);
556 if (ret < 0) {
557 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
558 return ret;
559 }
Arun KSc1f27192008-10-02 14:45:49 +0530560
561 /* Reset codec */
Axel Linf9dfbf92011-10-13 15:06:43 +0800562 snd_soc_write(codec, TLV320AIC23_RESET, 0);
563
564 /* Write the register default value to cache for reserved registers,
565 * so the write to the these registers are suppressed by the cache
566 * restore code when it skips writes of default registers.
567 */
568 snd_soc_cache_write(codec, 0x0A, 0);
569 snd_soc_cache_write(codec, 0x0B, 0);
570 snd_soc_cache_write(codec, 0x0C, 0);
571 snd_soc_cache_write(codec, 0x0D, 0);
572 snd_soc_cache_write(codec, 0x0E, 0);
Arun KSc1f27192008-10-02 14:45:49 +0530573
Arun KSc1f27192008-10-02 14:45:49 +0530574 /* power on device */
575 tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
576
Axel Linf9dfbf92011-10-13 15:06:43 +0800577 snd_soc_write(codec, TLV320AIC23_DIGT, TLV320AIC23_DEEMP_44K);
Arun KSc1f27192008-10-02 14:45:49 +0530578
579 /* Unmute input */
Axel Linf9dfbf92011-10-13 15:06:43 +0800580 snd_soc_update_bits(codec, TLV320AIC23_LINVOL,
581 TLV320AIC23_LIM_MUTED, TLV320AIC23_LRS_ENABLED);
Arun KSc1f27192008-10-02 14:45:49 +0530582
Axel Linf9dfbf92011-10-13 15:06:43 +0800583 snd_soc_update_bits(codec, TLV320AIC23_RINVOL,
584 TLV320AIC23_LIM_MUTED, TLV320AIC23_LRS_ENABLED);
Arun KSc1f27192008-10-02 14:45:49 +0530585
Axel Linf9dfbf92011-10-13 15:06:43 +0800586 snd_soc_update_bits(codec, TLV320AIC23_ANLG,
587 TLV320AIC23_BYPASS_ON | TLV320AIC23_MICM_MUTED,
588 0);
Arun KSc1f27192008-10-02 14:45:49 +0530589
590 /* Default output volume */
Axel Linf9dfbf92011-10-13 15:06:43 +0800591 snd_soc_write(codec, TLV320AIC23_LCHNVOL,
592 TLV320AIC23_DEFAULT_OUT_VOL & TLV320AIC23_OUT_VOL_MASK);
593 snd_soc_write(codec, TLV320AIC23_RCHNVOL,
594 TLV320AIC23_DEFAULT_OUT_VOL & TLV320AIC23_OUT_VOL_MASK);
Arun KSc1f27192008-10-02 14:45:49 +0530595
Axel Linf9dfbf92011-10-13 15:06:43 +0800596 snd_soc_write(codec, TLV320AIC23_ACTIVE, 0x1);
Arun KSc1f27192008-10-02 14:45:49 +0530597
Ian Molton3e8e1952009-01-09 00:23:21 +0000598 snd_soc_add_controls(codec, tlv320aic23_snd_controls,
599 ARRAY_SIZE(tlv320aic23_snd_controls));
Arun KSc1f27192008-10-02 14:45:49 +0530600
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000601 return 0;
Arun KSc1f27192008-10-02 14:45:49 +0530602}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000603
604static int tlv320aic23_remove(struct snd_soc_codec *codec)
605{
606 tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_OFF);
607 return 0;
608}
609
610static struct snd_soc_codec_driver soc_codec_dev_tlv320aic23 = {
611 .reg_cache_size = ARRAY_SIZE(tlv320aic23_reg),
612 .reg_word_size = sizeof(u16),
613 .reg_cache_default = tlv320aic23_reg,
614 .probe = tlv320aic23_probe,
615 .remove = tlv320aic23_remove,
616 .suspend = tlv320aic23_suspend,
617 .resume = tlv320aic23_resume,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000618 .set_bias_level = tlv320aic23_set_bias_level,
Lu Guanquna7dca702011-03-30 21:53:16 +0800619 .dapm_widgets = tlv320aic23_dapm_widgets,
620 .num_dapm_widgets = ARRAY_SIZE(tlv320aic23_dapm_widgets),
621 .dapm_routes = tlv320aic23_intercon,
622 .num_dapm_routes = ARRAY_SIZE(tlv320aic23_intercon),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000623};
Arun KSc1f27192008-10-02 14:45:49 +0530624
625#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
626/*
627 * If the i2c layer weren't so broken, we could pass this kind of data
628 * around
629 */
630static int tlv320aic23_codec_probe(struct i2c_client *i2c,
631 const struct i2c_device_id *i2c_id)
632{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000633 struct aic23 *aic23;
Arun KSc1f27192008-10-02 14:45:49 +0530634 int ret;
635
636 if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
637 return -EINVAL;
638
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000639 aic23 = kzalloc(sizeof(struct aic23), GFP_KERNEL);
640 if (aic23 == NULL)
641 return -ENOMEM;
Arun KSc1f27192008-10-02 14:45:49 +0530642
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000643 i2c_set_clientdata(i2c, aic23);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000644 aic23->control_type = SND_SOC_I2C;
Arun KSc1f27192008-10-02 14:45:49 +0530645
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000646 ret = snd_soc_register_codec(&i2c->dev,
647 &soc_codec_dev_tlv320aic23, &tlv320aic23_dai, 1);
648 if (ret < 0)
649 kfree(aic23);
Arun KSc1f27192008-10-02 14:45:49 +0530650 return ret;
651}
652static int __exit tlv320aic23_i2c_remove(struct i2c_client *i2c)
653{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000654 snd_soc_unregister_codec(&i2c->dev);
655 kfree(i2c_get_clientdata(i2c));
Arun KSc1f27192008-10-02 14:45:49 +0530656 return 0;
657}
658
659static const struct i2c_device_id tlv320aic23_id[] = {
660 {"tlv320aic23", 0},
661 {}
662};
663
664MODULE_DEVICE_TABLE(i2c, tlv320aic23_id);
665
666static struct i2c_driver tlv320aic23_i2c_driver = {
667 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000668 .name = "tlv320aic23-codec",
Arun KSc1f27192008-10-02 14:45:49 +0530669 },
670 .probe = tlv320aic23_codec_probe,
671 .remove = __exit_p(tlv320aic23_i2c_remove),
672 .id_table = tlv320aic23_id,
673};
674
675#endif
676
Takashi Iwaic9b3a402008-12-10 07:47:22 +0100677static int __init tlv320aic23_modinit(void)
Mark Brown64089b82008-12-08 19:17:58 +0000678{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000679 int ret;
680#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
681 ret = i2c_add_driver(&tlv320aic23_i2c_driver);
682 if (ret != 0) {
683 printk(KERN_ERR "Failed to register TLV320AIC23 I2C driver: %d\n",
684 ret);
685 }
686#endif
687 return ret;
Mark Brown64089b82008-12-08 19:17:58 +0000688}
689module_init(tlv320aic23_modinit);
690
691static void __exit tlv320aic23_exit(void)
692{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000693#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
694 i2c_del_driver(&tlv320aic23_i2c_driver);
695#endif
Mark Brown64089b82008-12-08 19:17:58 +0000696}
697module_exit(tlv320aic23_exit);
698
Arun KSc1f27192008-10-02 14:45:49 +0530699MODULE_DESCRIPTION("ASoC TLV320AIC23 codec driver");
700MODULE_AUTHOR("Arun KS <arunks@mistralsolutions.com>");
701MODULE_LICENSE("GPL");