blob: d9d8e844d63f6fd93423ef5f6ac0704fc59868e7 [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>
33#include <sound/soc-dapm.h>
34#include <sound/tlv.h>
35#include <sound/initval.h>
36
37#include "tlv320aic23.h"
38
Arun KSc1f27192008-10-02 14:45:49 +053039#define AIC23_VERSION "0.1"
40
Arun KSc1f27192008-10-02 14:45:49 +053041/*
42 * AIC23 register cache
43 */
44static const u16 tlv320aic23_reg[] = {
45 0x0097, 0x0097, 0x00F9, 0x00F9, /* 0 */
46 0x001A, 0x0004, 0x0007, 0x0001, /* 4 */
47 0x0020, 0x0000, 0x0000, 0x0000, /* 8 */
48 0x0000, 0x0000, 0x0000, 0x0000, /* 12 */
49};
50
51/*
52 * read tlv320aic23 register cache
53 */
54static inline unsigned int tlv320aic23_read_reg_cache(struct snd_soc_codec
55 *codec, unsigned int reg)
56{
57 u16 *cache = codec->reg_cache;
58 if (reg >= ARRAY_SIZE(tlv320aic23_reg))
59 return -1;
60 return cache[reg];
61}
62
63/*
64 * write tlv320aic23 register cache
65 */
66static inline void tlv320aic23_write_reg_cache(struct snd_soc_codec *codec,
67 u8 reg, u16 value)
68{
69 u16 *cache = codec->reg_cache;
70 if (reg >= ARRAY_SIZE(tlv320aic23_reg))
71 return;
72 cache[reg] = value;
73}
74
75/*
76 * write to the tlv320aic23 register space
77 */
78static int tlv320aic23_write(struct snd_soc_codec *codec, unsigned int reg,
79 unsigned int value)
80{
81
Arun KS4aa02392008-10-13 15:47:25 +053082 u8 data[2];
Arun KSc1f27192008-10-02 14:45:49 +053083
84 /* TLV320AIC23 has 7 bit address and 9 bits of data
85 * so we need to switch one data bit into reg and rest
86 * of data into val
87 */
88
Roel Kluin84ed1a12009-10-23 16:03:08 +020089 if (reg > 9 && reg != 15) {
Roel Kluin449bd542009-05-27 17:08:39 -070090 printk(KERN_WARNING "%s Invalid register R%u\n", __func__, reg);
Arun KSc1f27192008-10-02 14:45:49 +053091 return -1;
92 }
93
Arun KS4aa02392008-10-13 15:47:25 +053094 data[0] = (reg << 1) | (value >> 8 & 0x01);
95 data[1] = value & 0xff;
Arun KSc1f27192008-10-02 14:45:49 +053096
97 tlv320aic23_write_reg_cache(codec, reg, value);
98
Arun KS4aa02392008-10-13 15:47:25 +053099 if (codec->hw_write(codec->control_data, data, 2) == 2)
Arun KSc1f27192008-10-02 14:45:49 +0530100 return 0;
101
Roel Kluin449bd542009-05-27 17:08:39 -0700102 printk(KERN_ERR "%s cannot write %03x to register R%u\n", __func__,
Arun KSc1f27192008-10-02 14:45:49 +0530103 value, reg);
104
105 return -EIO;
106}
107
108static const char *rec_src_text[] = { "Line", "Mic" };
109static const char *deemph_text[] = {"None", "32Khz", "44.1Khz", "48Khz"};
Arun KSc1f27192008-10-02 14:45:49 +0530110
111static const struct soc_enum rec_src_enum =
112 SOC_ENUM_SINGLE(TLV320AIC23_ANLG, 2, 2, rec_src_text);
113
114static const struct snd_kcontrol_new tlv320aic23_rec_src_mux_controls =
115SOC_DAPM_ENUM("Input Select", rec_src_enum);
116
117static const struct soc_enum tlv320aic23_rec_src =
118 SOC_ENUM_SINGLE(TLV320AIC23_ANLG, 2, 2, rec_src_text);
119static const struct soc_enum tlv320aic23_deemph =
120 SOC_ENUM_SINGLE(TLV320AIC23_DIGT, 1, 4, deemph_text);
Arun KSc1f27192008-10-02 14:45:49 +0530121
122static const DECLARE_TLV_DB_SCALE(out_gain_tlv, -12100, 100, 0);
123static const DECLARE_TLV_DB_SCALE(input_gain_tlv, -1725, 75, 0);
Arun KSdf91ddf2008-10-03 17:07:30 +0530124static const DECLARE_TLV_DB_SCALE(sidetone_vol_tlv, -1800, 300, 0);
125
126static int snd_soc_tlv320aic23_put_volsw(struct snd_kcontrol *kcontrol,
127 struct snd_ctl_elem_value *ucontrol)
128{
129 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
130 u16 val, reg;
131
132 val = (ucontrol->value.integer.value[0] & 0x07);
133
134 /* linear conversion to userspace
135 * 000 = -6db
136 * 001 = -9db
137 * 010 = -12db
138 * 011 = -18db (Min)
139 * 100 = 0db (Max)
140 */
141 val = (val >= 4) ? 4 : (3 - val);
142
143 reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_ANLG) & (~0x1C0);
144 tlv320aic23_write(codec, TLV320AIC23_ANLG, reg | (val << 6));
145
146 return 0;
147}
148
149static int snd_soc_tlv320aic23_get_volsw(struct snd_kcontrol *kcontrol,
150 struct snd_ctl_elem_value *ucontrol)
151{
152 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
153 u16 val;
154
155 val = tlv320aic23_read_reg_cache(codec, TLV320AIC23_ANLG) & (0x1C0);
156 val = val >> 6;
157 val = (val >= 4) ? 4 : (3 - val);
158 ucontrol->value.integer.value[0] = val;
159 return 0;
160
161}
162
163#define SOC_TLV320AIC23_SINGLE_TLV(xname, reg, shift, max, invert, tlv_array) \
164{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
165 .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
166 SNDRV_CTL_ELEM_ACCESS_READWRITE,\
167 .tlv.p = (tlv_array), \
168 .info = snd_soc_info_volsw, .get = snd_soc_tlv320aic23_get_volsw,\
169 .put = snd_soc_tlv320aic23_put_volsw, \
170 .private_value = SOC_SINGLE_VALUE(reg, shift, max, invert) }
Arun KSc1f27192008-10-02 14:45:49 +0530171
172static const struct snd_kcontrol_new tlv320aic23_snd_controls[] = {
173 SOC_DOUBLE_R_TLV("Digital Playback Volume", TLV320AIC23_LCHNVOL,
174 TLV320AIC23_RCHNVOL, 0, 127, 0, out_gain_tlv),
175 SOC_SINGLE("Digital Playback Switch", TLV320AIC23_DIGT, 3, 1, 1),
176 SOC_DOUBLE_R("Line Input Switch", TLV320AIC23_LINVOL,
177 TLV320AIC23_RINVOL, 7, 1, 0),
178 SOC_DOUBLE_R_TLV("Line Input Volume", TLV320AIC23_LINVOL,
179 TLV320AIC23_RINVOL, 0, 31, 0, input_gain_tlv),
180 SOC_SINGLE("Mic Input Switch", TLV320AIC23_ANLG, 1, 1, 1),
181 SOC_SINGLE("Mic Booster Switch", TLV320AIC23_ANLG, 0, 1, 0),
Arun KSdf91ddf2008-10-03 17:07:30 +0530182 SOC_TLV320AIC23_SINGLE_TLV("Sidetone Volume", TLV320AIC23_ANLG,
183 6, 4, 0, sidetone_vol_tlv),
Arun KSc1f27192008-10-02 14:45:49 +0530184 SOC_ENUM("Playback De-emphasis", tlv320aic23_deemph),
185};
186
Arun KSc1f27192008-10-02 14:45:49 +0530187/* PGA Mixer controls for Line and Mic switch */
188static const struct snd_kcontrol_new tlv320aic23_output_mixer_controls[] = {
189 SOC_DAPM_SINGLE("Line Bypass Switch", TLV320AIC23_ANLG, 3, 1, 0),
190 SOC_DAPM_SINGLE("Mic Sidetone Switch", TLV320AIC23_ANLG, 5, 1, 0),
191 SOC_DAPM_SINGLE("Playback Switch", TLV320AIC23_ANLG, 4, 1, 0),
192};
193
194static const struct snd_soc_dapm_widget tlv320aic23_dapm_widgets[] = {
195 SND_SOC_DAPM_DAC("DAC", "Playback", TLV320AIC23_PWR, 3, 1),
196 SND_SOC_DAPM_ADC("ADC", "Capture", TLV320AIC23_PWR, 2, 1),
197 SND_SOC_DAPM_MUX("Capture Source", SND_SOC_NOPM, 0, 0,
198 &tlv320aic23_rec_src_mux_controls),
199 SND_SOC_DAPM_MIXER("Output Mixer", TLV320AIC23_PWR, 4, 1,
200 &tlv320aic23_output_mixer_controls[0],
201 ARRAY_SIZE(tlv320aic23_output_mixer_controls)),
202 SND_SOC_DAPM_PGA("Line Input", TLV320AIC23_PWR, 0, 1, NULL, 0),
203 SND_SOC_DAPM_PGA("Mic Input", TLV320AIC23_PWR, 1, 1, NULL, 0),
204
205 SND_SOC_DAPM_OUTPUT("LHPOUT"),
206 SND_SOC_DAPM_OUTPUT("RHPOUT"),
207 SND_SOC_DAPM_OUTPUT("LOUT"),
208 SND_SOC_DAPM_OUTPUT("ROUT"),
209
210 SND_SOC_DAPM_INPUT("LLINEIN"),
211 SND_SOC_DAPM_INPUT("RLINEIN"),
212
213 SND_SOC_DAPM_INPUT("MICIN"),
214};
215
216static const struct snd_soc_dapm_route intercon[] = {
217 /* Output Mixer */
218 {"Output Mixer", "Line Bypass Switch", "Line Input"},
219 {"Output Mixer", "Playback Switch", "DAC"},
220 {"Output Mixer", "Mic Sidetone Switch", "Mic Input"},
221
222 /* Outputs */
223 {"RHPOUT", NULL, "Output Mixer"},
224 {"LHPOUT", NULL, "Output Mixer"},
225 {"LOUT", NULL, "Output Mixer"},
226 {"ROUT", NULL, "Output Mixer"},
227
228 /* Inputs */
229 {"Line Input", "NULL", "LLINEIN"},
230 {"Line Input", "NULL", "RLINEIN"},
231
232 {"Mic Input", "NULL", "MICIN"},
233
234 /* input mux */
235 {"Capture Source", "Line", "Line Input"},
236 {"Capture Source", "Mic", "Mic Input"},
237 {"ADC", NULL, "Capture Source"},
238
239};
240
Troy Kisky26df91c2008-11-05 18:53:28 +0000241/* AIC23 driver data */
242struct aic23 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000243 enum snd_soc_control_type control_type;
244 void *control_data;
Troy Kisky26df91c2008-11-05 18:53:28 +0000245 int mclk;
246 int requested_adc;
247 int requested_dac;
Arun KSc1f27192008-10-02 14:45:49 +0530248};
249
Troy Kisky26df91c2008-11-05 18:53:28 +0000250/*
251 * Common Crystals used
252 * 11.2896 Mhz /128 = *88.2k /192 = 58.8k
253 * 12.0000 Mhz /125 = *96k /136 = 88.235K
254 * 12.2880 Mhz /128 = *96k /192 = 64k
255 * 16.9344 Mhz /128 = 132.3k /192 = *88.2k
256 * 18.4320 Mhz /128 = 144k /192 = *96k
257 */
258
259/*
260 * Normal BOSR 0-256/2 = 128, 1-384/2 = 192
261 * USB BOSR 0-250/2 = 125, 1-272/2 = 136
262 */
263static const int bosr_usb_divisor_table[] = {
264 128, 125, 192, 136
265};
266#define LOWER_GROUP ((1<<0) | (1<<1) | (1<<2) | (1<<3) | (1<<6) | (1<<7))
267#define UPPER_GROUP ((1<<8) | (1<<9) | (1<<10) | (1<<11) | (1<<15))
268static const unsigned short sr_valid_mask[] = {
269 LOWER_GROUP|UPPER_GROUP, /* Normal, bosr - 0*/
Troy Kisky26df91c2008-11-05 18:53:28 +0000270 LOWER_GROUP, /* Usb, bosr - 0*/
Troy Kiskybab02122009-11-17 13:51:01 -0700271 LOWER_GROUP|UPPER_GROUP, /* Normal, bosr - 1*/
Troy Kisky26df91c2008-11-05 18:53:28 +0000272 UPPER_GROUP, /* Usb, bosr - 1*/
273};
274/*
275 * Every divisor is a factor of 11*12
276 */
277#define SR_MULT (11*12)
Troy Kiskyccff4b12009-06-05 19:15:58 -0700278#define A(x) (SR_MULT/x)
Troy Kisky26df91c2008-11-05 18:53:28 +0000279static const unsigned char sr_adc_mult_table[] = {
Troy Kiskyccff4b12009-06-05 19:15:58 -0700280 A(2), A(2), A(12), A(12), 0, 0, A(3), A(1),
281 A(2), A(2), A(11), A(11), 0, 0, 0, A(1)
Troy Kisky26df91c2008-11-05 18:53:28 +0000282};
283static const unsigned char sr_dac_mult_table[] = {
Troy Kiskyccff4b12009-06-05 19:15:58 -0700284 A(2), A(12), A(2), A(12), 0, 0, A(3), A(1),
285 A(2), A(11), A(2), A(11), 0, 0, 0, A(1)
Troy Kisky26df91c2008-11-05 18:53:28 +0000286};
287
288static unsigned get_score(int adc, int adc_l, int adc_h, int need_adc,
289 int dac, int dac_l, int dac_h, int need_dac)
290{
291 if ((adc >= adc_l) && (adc <= adc_h) &&
292 (dac >= dac_l) && (dac <= dac_h)) {
293 int diff_adc = need_adc - adc;
294 int diff_dac = need_dac - dac;
295 return abs(diff_adc) + abs(diff_dac);
296 }
297 return UINT_MAX;
298}
299
300static int find_rate(int mclk, u32 need_adc, u32 need_dac)
301{
302 int i, j;
303 int best_i = -1;
304 int best_j = -1;
305 int best_div = 0;
306 unsigned best_score = UINT_MAX;
307 int adc_l, adc_h, dac_l, dac_h;
308
309 need_adc *= SR_MULT;
310 need_dac *= SR_MULT;
311 /*
312 * rates given are +/- 1/32
313 */
314 adc_l = need_adc - (need_adc >> 5);
315 adc_h = need_adc + (need_adc >> 5);
316 dac_l = need_dac - (need_dac >> 5);
317 dac_h = need_dac + (need_dac >> 5);
Mark Brown8d702f22008-11-17 21:42:01 +0000318 for (i = 0; i < ARRAY_SIZE(bosr_usb_divisor_table); i++) {
Troy Kisky26df91c2008-11-05 18:53:28 +0000319 int base = mclk / bosr_usb_divisor_table[i];
320 int mask = sr_valid_mask[i];
Mark Brown8d702f22008-11-17 21:42:01 +0000321 for (j = 0; j < ARRAY_SIZE(sr_adc_mult_table);
322 j++, mask >>= 1) {
Troy Kisky26df91c2008-11-05 18:53:28 +0000323 int adc;
324 int dac;
325 int score;
326 if ((mask & 1) == 0)
327 continue;
328 adc = base * sr_adc_mult_table[j];
329 dac = base * sr_dac_mult_table[j];
330 score = get_score(adc, adc_l, adc_h, need_adc,
331 dac, dac_l, dac_h, need_dac);
332 if (best_score > score) {
333 best_score = score;
334 best_i = i;
335 best_j = j;
336 best_div = 0;
337 }
338 score = get_score((adc >> 1), adc_l, adc_h, need_adc,
339 (dac >> 1), dac_l, dac_h, need_dac);
340 /* prefer to have a /2 */
341 if ((score != UINT_MAX) && (best_score >= score)) {
342 best_score = score;
343 best_i = i;
344 best_j = j;
345 best_div = 1;
346 }
347 }
348 }
349 return (best_j << 2) | best_i | (best_div << TLV320AIC23_CLKIN_SHIFT);
350}
351
Mark Brown8d702f22008-11-17 21:42:01 +0000352#ifdef DEBUG
Troy Kisky26df91c2008-11-05 18:53:28 +0000353static void get_current_sample_rates(struct snd_soc_codec *codec, int mclk,
354 u32 *sample_rate_adc, u32 *sample_rate_dac)
355{
356 int src = tlv320aic23_read_reg_cache(codec, TLV320AIC23_SRATE);
357 int sr = (src >> 2) & 0x0f;
358 int val = (mclk / bosr_usb_divisor_table[src & 3]);
359 int adc = (val * sr_adc_mult_table[sr]) / SR_MULT;
360 int dac = (val * sr_dac_mult_table[sr]) / SR_MULT;
361 if (src & TLV320AIC23_CLKIN_HALF) {
362 adc >>= 1;
363 dac >>= 1;
364 }
365 *sample_rate_adc = adc;
366 *sample_rate_dac = dac;
367}
Mark Brown8d702f22008-11-17 21:42:01 +0000368#endif
Troy Kisky26df91c2008-11-05 18:53:28 +0000369
370static int set_sample_rate_control(struct snd_soc_codec *codec, int mclk,
371 u32 sample_rate_adc, u32 sample_rate_dac)
372{
373 /* Search for the right sample rate */
374 int data = find_rate(mclk, sample_rate_adc, sample_rate_dac);
375 if (data < 0) {
376 printk(KERN_ERR "%s:Invalid rate %u,%u requested\n",
377 __func__, sample_rate_adc, sample_rate_dac);
378 return -EINVAL;
379 }
380 tlv320aic23_write(codec, TLV320AIC23_SRATE, data);
Mark Brown8d702f22008-11-17 21:42:01 +0000381#ifdef DEBUG
382 {
383 u32 adc, dac;
Troy Kisky26df91c2008-11-05 18:53:28 +0000384 get_current_sample_rates(codec, mclk, &adc, &dac);
385 printk(KERN_DEBUG "actual samplerate = %u,%u reg=%x\n",
386 adc, dac, data);
387 }
Mark Brown8d702f22008-11-17 21:42:01 +0000388#endif
Troy Kisky26df91c2008-11-05 18:53:28 +0000389 return 0;
390}
391
Arun KSc1f27192008-10-02 14:45:49 +0530392static int tlv320aic23_add_widgets(struct snd_soc_codec *codec)
393{
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200394 struct snd_soc_dapm_context *dapm = &codec->dapm;
Arun KSc1f27192008-10-02 14:45:49 +0530395
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200396 snd_soc_dapm_new_controls(dapm, tlv320aic23_dapm_widgets,
397 ARRAY_SIZE(tlv320aic23_dapm_widgets));
Arun KSc1f27192008-10-02 14:45:49 +0530398 /* set up audio path interconnects */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200399 snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon));
Arun KSc1f27192008-10-02 14:45:49 +0530400
Arun KSc1f27192008-10-02 14:45:49 +0530401 return 0;
402}
403
404static int tlv320aic23_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000405 struct snd_pcm_hw_params *params,
406 struct snd_soc_dai *dai)
Arun KSc1f27192008-10-02 14:45:49 +0530407{
408 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000409 struct snd_soc_codec *codec = rtd->codec;
Troy Kisky26df91c2008-11-05 18:53:28 +0000410 u16 iface_reg;
411 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000412 struct aic23 *aic23 = snd_soc_codec_get_drvdata(codec);
Troy Kisky26df91c2008-11-05 18:53:28 +0000413 u32 sample_rate_adc = aic23->requested_adc;
414 u32 sample_rate_dac = aic23->requested_dac;
415 u32 sample_rate = params_rate(params);
416
417 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
418 aic23->requested_dac = sample_rate_dac = sample_rate;
419 if (!sample_rate_adc)
420 sample_rate_adc = sample_rate;
421 } else {
422 aic23->requested_adc = sample_rate_adc = sample_rate;
423 if (!sample_rate_dac)
424 sample_rate_dac = sample_rate;
425 }
426 ret = set_sample_rate_control(codec, aic23->mclk, sample_rate_adc,
427 sample_rate_dac);
428 if (ret < 0)
429 return ret;
Arun KSc1f27192008-10-02 14:45:49 +0530430
431 iface_reg =
432 tlv320aic23_read_reg_cache(codec,
433 TLV320AIC23_DIGT_FMT) & ~(0x03 << 2);
Arun KSc1f27192008-10-02 14:45:49 +0530434 switch (params_format(params)) {
435 case SNDRV_PCM_FORMAT_S16_LE:
436 break;
437 case SNDRV_PCM_FORMAT_S20_3LE:
438 iface_reg |= (0x01 << 2);
439 break;
440 case SNDRV_PCM_FORMAT_S24_LE:
441 iface_reg |= (0x02 << 2);
442 break;
443 case SNDRV_PCM_FORMAT_S32_LE:
444 iface_reg |= (0x03 << 2);
445 break;
446 }
447 tlv320aic23_write(codec, TLV320AIC23_DIGT_FMT, iface_reg);
448
449 return 0;
450}
451
Mark Browndee89c42008-11-18 22:11:38 +0000452static int tlv320aic23_pcm_prepare(struct snd_pcm_substream *substream,
453 struct snd_soc_dai *dai)
Arun KSc1f27192008-10-02 14:45:49 +0530454{
455 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000456 struct snd_soc_codec *codec = rtd->codec;
Arun KSc1f27192008-10-02 14:45:49 +0530457
458 /* set active */
459 tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x0001);
460
461 return 0;
462}
463
Mark Browndee89c42008-11-18 22:11:38 +0000464static void tlv320aic23_shutdown(struct snd_pcm_substream *substream,
465 struct snd_soc_dai *dai)
Arun KSc1f27192008-10-02 14:45:49 +0530466{
467 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000468 struct snd_soc_codec *codec = rtd->codec;
469 struct aic23 *aic23 = snd_soc_codec_get_drvdata(codec);
Arun KSc1f27192008-10-02 14:45:49 +0530470
471 /* deactivate */
472 if (!codec->active) {
473 udelay(50);
474 tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x0);
475 }
Troy Kisky26df91c2008-11-05 18:53:28 +0000476 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
477 aic23->requested_dac = 0;
478 else
479 aic23->requested_adc = 0;
Arun KSc1f27192008-10-02 14:45:49 +0530480}
481
482static int tlv320aic23_mute(struct snd_soc_dai *dai, int mute)
483{
484 struct snd_soc_codec *codec = dai->codec;
485 u16 reg;
486
487 reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_DIGT);
488 if (mute)
489 reg |= TLV320AIC23_DACM_MUTE;
490
491 else
492 reg &= ~TLV320AIC23_DACM_MUTE;
493
494 tlv320aic23_write(codec, TLV320AIC23_DIGT, reg);
495
496 return 0;
497}
498
499static int tlv320aic23_set_dai_fmt(struct snd_soc_dai *codec_dai,
500 unsigned int fmt)
501{
502 struct snd_soc_codec *codec = codec_dai->codec;
503 u16 iface_reg;
504
505 iface_reg =
506 tlv320aic23_read_reg_cache(codec, TLV320AIC23_DIGT_FMT) & (~0x03);
507
508 /* set master/slave audio interface */
509 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
510 case SND_SOC_DAIFMT_CBM_CFM:
511 iface_reg |= TLV320AIC23_MS_MASTER;
512 break;
513 case SND_SOC_DAIFMT_CBS_CFS:
514 break;
515 default:
516 return -EINVAL;
517
518 }
519
520 /* interface format */
521 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
522 case SND_SOC_DAIFMT_I2S:
523 iface_reg |= TLV320AIC23_FOR_I2S;
524 break;
Peter Ujfalusi894bf922009-04-09 12:34:40 +0300525 case SND_SOC_DAIFMT_DSP_A:
526 iface_reg |= TLV320AIC23_LRP_ON;
Jarkko Nikulabd258672008-12-22 10:21:36 +0200527 case SND_SOC_DAIFMT_DSP_B:
Arun KSc1f27192008-10-02 14:45:49 +0530528 iface_reg |= TLV320AIC23_FOR_DSP;
529 break;
530 case SND_SOC_DAIFMT_RIGHT_J:
531 break;
532 case SND_SOC_DAIFMT_LEFT_J:
533 iface_reg |= TLV320AIC23_FOR_LJUST;
534 break;
535 default:
536 return -EINVAL;
537
538 }
539
540 tlv320aic23_write(codec, TLV320AIC23_DIGT_FMT, iface_reg);
541
542 return 0;
543}
544
545static int tlv320aic23_set_dai_sysclk(struct snd_soc_dai *codec_dai,
546 int clk_id, unsigned int freq, int dir)
547{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000548 struct aic23 *aic23 = snd_soc_dai_get_drvdata(codec_dai);
Troy Kisky26df91c2008-11-05 18:53:28 +0000549 aic23->mclk = freq;
550 return 0;
Arun KSc1f27192008-10-02 14:45:49 +0530551}
552
553static int tlv320aic23_set_bias_level(struct snd_soc_codec *codec,
554 enum snd_soc_bias_level level)
555{
556 u16 reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_PWR) & 0xff7f;
557
558 switch (level) {
559 case SND_SOC_BIAS_ON:
560 /* vref/mid, osc on, dac unmute */
Eric Bénard3d5a4512010-06-19 19:33:39 +0200561 reg &= ~(TLV320AIC23_DEVICE_PWR_OFF | TLV320AIC23_OSC_OFF | \
562 TLV320AIC23_DAC_OFF);
Arun KSc1f27192008-10-02 14:45:49 +0530563 tlv320aic23_write(codec, TLV320AIC23_PWR, reg);
564 break;
565 case SND_SOC_BIAS_PREPARE:
566 break;
567 case SND_SOC_BIAS_STANDBY:
568 /* everything off except vref/vmid, */
Eric Bénard3d5a4512010-06-19 19:33:39 +0200569 tlv320aic23_write(codec, TLV320AIC23_PWR, reg | \
570 TLV320AIC23_CLK_OFF);
Arun KSc1f27192008-10-02 14:45:49 +0530571 break;
572 case SND_SOC_BIAS_OFF:
573 /* everything off, dac mute, inactive */
574 tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x0);
575 tlv320aic23_write(codec, TLV320AIC23_PWR, 0xffff);
576 break;
577 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200578 codec->dapm.bias_level = level;
Arun KSc1f27192008-10-02 14:45:49 +0530579 return 0;
580}
581
582#define AIC23_RATES SNDRV_PCM_RATE_8000_96000
583#define AIC23_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
584 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)
585
Eric Miao6335d052009-03-03 09:41:00 +0800586static struct snd_soc_dai_ops tlv320aic23_dai_ops = {
587 .prepare = tlv320aic23_pcm_prepare,
588 .hw_params = tlv320aic23_hw_params,
589 .shutdown = tlv320aic23_shutdown,
590 .digital_mute = tlv320aic23_mute,
591 .set_fmt = tlv320aic23_set_dai_fmt,
592 .set_sysclk = tlv320aic23_set_dai_sysclk,
593};
594
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000595static struct snd_soc_dai_driver tlv320aic23_dai = {
596 .name = "tlv320aic23-hifi",
Arun KSc1f27192008-10-02 14:45:49 +0530597 .playback = {
598 .stream_name = "Playback",
599 .channels_min = 2,
600 .channels_max = 2,
601 .rates = AIC23_RATES,
602 .formats = AIC23_FORMATS,},
603 .capture = {
604 .stream_name = "Capture",
605 .channels_min = 2,
606 .channels_max = 2,
607 .rates = AIC23_RATES,
608 .formats = AIC23_FORMATS,},
Eric Miao6335d052009-03-03 09:41:00 +0800609 .ops = &tlv320aic23_dai_ops,
Arun KSc1f27192008-10-02 14:45:49 +0530610};
Arun KSc1f27192008-10-02 14:45:49 +0530611
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000612static int tlv320aic23_suspend(struct snd_soc_codec *codec,
Arun KSc1f27192008-10-02 14:45:49 +0530613 pm_message_t state)
614{
Arun KSc1f27192008-10-02 14:45:49 +0530615 tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_OFF);
616
617 return 0;
618}
619
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000620static int tlv320aic23_resume(struct snd_soc_codec *codec)
Arun KSc1f27192008-10-02 14:45:49 +0530621{
Arun KSc1f27192008-10-02 14:45:49 +0530622 u16 reg;
623
624 /* Sync reg_cache with the hardware */
Anuj Aggarwal3e59aaa2010-01-29 13:58:55 +0530625 for (reg = 0; reg <= TLV320AIC23_ACTIVE; reg++) {
Arun KSc1f27192008-10-02 14:45:49 +0530626 u16 val = tlv320aic23_read_reg_cache(codec, reg);
627 tlv320aic23_write(codec, reg, val);
628 }
Arun KSc1f27192008-10-02 14:45:49 +0530629 tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
Arun KSc1f27192008-10-02 14:45:49 +0530630
631 return 0;
632}
633
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000634static int tlv320aic23_probe(struct snd_soc_codec *codec)
Arun KSc1f27192008-10-02 14:45:49 +0530635{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000636 struct aic23 *aic23 = snd_soc_codec_get_drvdata(codec);
637 int reg;
Arun KSc1f27192008-10-02 14:45:49 +0530638
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000639 printk(KERN_INFO "AIC23 Audio Codec %s\n", AIC23_VERSION);
640 codec->control_data = aic23->control_data;
641 codec->hw_write = (hw_write_t)i2c_master_send;
642 codec->hw_read = NULL;
Arun KSc1f27192008-10-02 14:45:49 +0530643
644 /* Reset codec */
645 tlv320aic23_write(codec, TLV320AIC23_RESET, 0);
646
Arun KSc1f27192008-10-02 14:45:49 +0530647 /* power on device */
648 tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
649
650 tlv320aic23_write(codec, TLV320AIC23_DIGT, TLV320AIC23_DEEMP_44K);
651
652 /* Unmute input */
653 reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_LINVOL);
654 tlv320aic23_write(codec, TLV320AIC23_LINVOL,
655 (reg & (~TLV320AIC23_LIM_MUTED)) |
656 (TLV320AIC23_LRS_ENABLED));
657
658 reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_RINVOL);
659 tlv320aic23_write(codec, TLV320AIC23_RINVOL,
660 (reg & (~TLV320AIC23_LIM_MUTED)) |
661 TLV320AIC23_LRS_ENABLED);
662
663 reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_ANLG);
664 tlv320aic23_write(codec, TLV320AIC23_ANLG,
665 (reg) & (~TLV320AIC23_BYPASS_ON) &
666 (~TLV320AIC23_MICM_MUTED));
667
668 /* Default output volume */
669 tlv320aic23_write(codec, TLV320AIC23_LCHNVOL,
670 TLV320AIC23_DEFAULT_OUT_VOL &
671 TLV320AIC23_OUT_VOL_MASK);
672 tlv320aic23_write(codec, TLV320AIC23_RCHNVOL,
673 TLV320AIC23_DEFAULT_OUT_VOL &
674 TLV320AIC23_OUT_VOL_MASK);
675
676 tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x1);
677
Ian Molton3e8e1952009-01-09 00:23:21 +0000678 snd_soc_add_controls(codec, tlv320aic23_snd_controls,
679 ARRAY_SIZE(tlv320aic23_snd_controls));
Arun KSc1f27192008-10-02 14:45:49 +0530680 tlv320aic23_add_widgets(codec);
Arun KSc1f27192008-10-02 14:45:49 +0530681
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000682 return 0;
Arun KSc1f27192008-10-02 14:45:49 +0530683}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000684
685static int tlv320aic23_remove(struct snd_soc_codec *codec)
686{
687 tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_OFF);
688 return 0;
689}
690
691static struct snd_soc_codec_driver soc_codec_dev_tlv320aic23 = {
692 .reg_cache_size = ARRAY_SIZE(tlv320aic23_reg),
693 .reg_word_size = sizeof(u16),
694 .reg_cache_default = tlv320aic23_reg,
695 .probe = tlv320aic23_probe,
696 .remove = tlv320aic23_remove,
697 .suspend = tlv320aic23_suspend,
698 .resume = tlv320aic23_resume,
699 .read = tlv320aic23_read_reg_cache,
700 .write = tlv320aic23_write,
701 .set_bias_level = tlv320aic23_set_bias_level,
702};
Arun KSc1f27192008-10-02 14:45:49 +0530703
704#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
705/*
706 * If the i2c layer weren't so broken, we could pass this kind of data
707 * around
708 */
709static int tlv320aic23_codec_probe(struct i2c_client *i2c,
710 const struct i2c_device_id *i2c_id)
711{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000712 struct aic23 *aic23;
Arun KSc1f27192008-10-02 14:45:49 +0530713 int ret;
714
715 if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
716 return -EINVAL;
717
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000718 aic23 = kzalloc(sizeof(struct aic23), GFP_KERNEL);
719 if (aic23 == NULL)
720 return -ENOMEM;
Arun KSc1f27192008-10-02 14:45:49 +0530721
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000722 i2c_set_clientdata(i2c, aic23);
723 aic23->control_data = i2c;
724 aic23->control_type = SND_SOC_I2C;
Arun KSc1f27192008-10-02 14:45:49 +0530725
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000726 ret = snd_soc_register_codec(&i2c->dev,
727 &soc_codec_dev_tlv320aic23, &tlv320aic23_dai, 1);
728 if (ret < 0)
729 kfree(aic23);
Arun KSc1f27192008-10-02 14:45:49 +0530730 return ret;
731}
732static int __exit tlv320aic23_i2c_remove(struct i2c_client *i2c)
733{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000734 snd_soc_unregister_codec(&i2c->dev);
735 kfree(i2c_get_clientdata(i2c));
Arun KSc1f27192008-10-02 14:45:49 +0530736 return 0;
737}
738
739static const struct i2c_device_id tlv320aic23_id[] = {
740 {"tlv320aic23", 0},
741 {}
742};
743
744MODULE_DEVICE_TABLE(i2c, tlv320aic23_id);
745
746static struct i2c_driver tlv320aic23_i2c_driver = {
747 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000748 .name = "tlv320aic23-codec",
Arun KSc1f27192008-10-02 14:45:49 +0530749 },
750 .probe = tlv320aic23_codec_probe,
751 .remove = __exit_p(tlv320aic23_i2c_remove),
752 .id_table = tlv320aic23_id,
753};
754
755#endif
756
Takashi Iwaic9b3a402008-12-10 07:47:22 +0100757static int __init tlv320aic23_modinit(void)
Mark Brown64089b82008-12-08 19:17:58 +0000758{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000759 int ret;
760#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
761 ret = i2c_add_driver(&tlv320aic23_i2c_driver);
762 if (ret != 0) {
763 printk(KERN_ERR "Failed to register TLV320AIC23 I2C driver: %d\n",
764 ret);
765 }
766#endif
767 return ret;
Mark Brown64089b82008-12-08 19:17:58 +0000768}
769module_init(tlv320aic23_modinit);
770
771static void __exit tlv320aic23_exit(void)
772{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000773#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
774 i2c_del_driver(&tlv320aic23_i2c_driver);
775#endif
Mark Brown64089b82008-12-08 19:17:58 +0000776}
777module_exit(tlv320aic23_exit);
778
Arun KSc1f27192008-10-02 14:45:49 +0530779MODULE_DESCRIPTION("ASoC TLV320AIC23 codec driver");
780MODULE_AUTHOR("Arun KS <arunks@mistralsolutions.com>");
781MODULE_LICENSE("GPL");