blob: ab369ae76b8c6638b1a94016e246c604ebfb0bc7 [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>
Mark Brown4aa11d62013-09-24 19:26:08 +010026#include <linux/regmap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Arun KSc1f27192008-10-02 14:45:49 +053028#include <sound/core.h>
29#include <sound/pcm.h>
30#include <sound/pcm_params.h>
31#include <sound/soc.h>
Arun KSc1f27192008-10-02 14:45:49 +053032#include <sound/tlv.h>
33#include <sound/initval.h>
34
35#include "tlv320aic23.h"
36
Arun KSc1f27192008-10-02 14:45:49 +053037/*
38 * AIC23 register cache
39 */
Mark Brown4aa11d62013-09-24 19:26:08 +010040static const struct reg_default tlv320aic23_reg[] = {
41 { 0, 0x0097 },
42 { 1, 0x0097 },
43 { 2, 0x00F9 },
44 { 3, 0x00F9 },
45 { 4, 0x001A },
46 { 5, 0x0004 },
47 { 6, 0x0007 },
48 { 7, 0x0001 },
49 { 8, 0x0020 },
50 { 9, 0x0000 },
51};
52
Max Filippovb3fc5722014-03-06 14:04:41 +040053const struct regmap_config tlv320aic23_regmap = {
Mark Brown4aa11d62013-09-24 19:26:08 +010054 .reg_bits = 7,
55 .val_bits = 9,
56
57 .max_register = TLV320AIC23_RESET,
58 .reg_defaults = tlv320aic23_reg,
59 .num_reg_defaults = ARRAY_SIZE(tlv320aic23_reg),
60 .cache_type = REGCACHE_RBTREE,
Arun KSc1f27192008-10-02 14:45:49 +053061};
62
Arun KSc1f27192008-10-02 14:45:49 +053063static const char *rec_src_text[] = { "Line", "Mic" };
64static const char *deemph_text[] = {"None", "32Khz", "44.1Khz", "48Khz"};
Arun KSc1f27192008-10-02 14:45:49 +053065
Takashi Iwai806057c2014-02-18 10:45:53 +010066static SOC_ENUM_SINGLE_DECL(rec_src_enum,
67 TLV320AIC23_ANLG, 2, rec_src_text);
Arun KSc1f27192008-10-02 14:45:49 +053068
69static const struct snd_kcontrol_new tlv320aic23_rec_src_mux_controls =
70SOC_DAPM_ENUM("Input Select", rec_src_enum);
71
Takashi Iwai806057c2014-02-18 10:45:53 +010072static SOC_ENUM_SINGLE_DECL(tlv320aic23_rec_src,
73 TLV320AIC23_ANLG, 2, rec_src_text);
74static SOC_ENUM_SINGLE_DECL(tlv320aic23_deemph,
75 TLV320AIC23_DIGT, 1, deemph_text);
Arun KSc1f27192008-10-02 14:45:49 +053076
77static const DECLARE_TLV_DB_SCALE(out_gain_tlv, -12100, 100, 0);
78static const DECLARE_TLV_DB_SCALE(input_gain_tlv, -1725, 75, 0);
Arun KSdf91ddf2008-10-03 17:07:30 +053079static const DECLARE_TLV_DB_SCALE(sidetone_vol_tlv, -1800, 300, 0);
80
81static int snd_soc_tlv320aic23_put_volsw(struct snd_kcontrol *kcontrol,
82 struct snd_ctl_elem_value *ucontrol)
83{
84 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
85 u16 val, reg;
86
87 val = (ucontrol->value.integer.value[0] & 0x07);
88
89 /* linear conversion to userspace
90 * 000 = -6db
91 * 001 = -9db
92 * 010 = -12db
93 * 011 = -18db (Min)
94 * 100 = 0db (Max)
95 */
96 val = (val >= 4) ? 4 : (3 - val);
97
Axel Linf9dfbf92011-10-13 15:06:43 +080098 reg = snd_soc_read(codec, TLV320AIC23_ANLG) & (~0x1C0);
99 snd_soc_write(codec, TLV320AIC23_ANLG, reg | (val << 6));
Arun KSdf91ddf2008-10-03 17:07:30 +0530100
101 return 0;
102}
103
104static int snd_soc_tlv320aic23_get_volsw(struct snd_kcontrol *kcontrol,
105 struct snd_ctl_elem_value *ucontrol)
106{
107 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
108 u16 val;
109
Axel Linf9dfbf92011-10-13 15:06:43 +0800110 val = snd_soc_read(codec, TLV320AIC23_ANLG) & (0x1C0);
Arun KSdf91ddf2008-10-03 17:07:30 +0530111 val = val >> 6;
112 val = (val >= 4) ? 4 : (3 - val);
113 ucontrol->value.integer.value[0] = val;
114 return 0;
115
116}
117
Arun KSc1f27192008-10-02 14:45:49 +0530118static const struct snd_kcontrol_new tlv320aic23_snd_controls[] = {
119 SOC_DOUBLE_R_TLV("Digital Playback Volume", TLV320AIC23_LCHNVOL,
120 TLV320AIC23_RCHNVOL, 0, 127, 0, out_gain_tlv),
121 SOC_SINGLE("Digital Playback Switch", TLV320AIC23_DIGT, 3, 1, 1),
122 SOC_DOUBLE_R("Line Input Switch", TLV320AIC23_LINVOL,
123 TLV320AIC23_RINVOL, 7, 1, 0),
124 SOC_DOUBLE_R_TLV("Line Input Volume", TLV320AIC23_LINVOL,
125 TLV320AIC23_RINVOL, 0, 31, 0, input_gain_tlv),
126 SOC_SINGLE("Mic Input Switch", TLV320AIC23_ANLG, 1, 1, 1),
127 SOC_SINGLE("Mic Booster Switch", TLV320AIC23_ANLG, 0, 1, 0),
Peter Ujfalusi0f9887d2011-10-05 10:29:19 +0300128 SOC_SINGLE_EXT_TLV("Sidetone Volume", TLV320AIC23_ANLG, 6, 4, 0,
129 snd_soc_tlv320aic23_get_volsw,
130 snd_soc_tlv320aic23_put_volsw, sidetone_vol_tlv),
Arun KSc1f27192008-10-02 14:45:49 +0530131 SOC_ENUM("Playback De-emphasis", tlv320aic23_deemph),
132};
133
Arun KSc1f27192008-10-02 14:45:49 +0530134/* PGA Mixer controls for Line and Mic switch */
135static const struct snd_kcontrol_new tlv320aic23_output_mixer_controls[] = {
136 SOC_DAPM_SINGLE("Line Bypass Switch", TLV320AIC23_ANLG, 3, 1, 0),
137 SOC_DAPM_SINGLE("Mic Sidetone Switch", TLV320AIC23_ANLG, 5, 1, 0),
138 SOC_DAPM_SINGLE("Playback Switch", TLV320AIC23_ANLG, 4, 1, 0),
139};
140
141static const struct snd_soc_dapm_widget tlv320aic23_dapm_widgets[] = {
142 SND_SOC_DAPM_DAC("DAC", "Playback", TLV320AIC23_PWR, 3, 1),
143 SND_SOC_DAPM_ADC("ADC", "Capture", TLV320AIC23_PWR, 2, 1),
144 SND_SOC_DAPM_MUX("Capture Source", SND_SOC_NOPM, 0, 0,
145 &tlv320aic23_rec_src_mux_controls),
146 SND_SOC_DAPM_MIXER("Output Mixer", TLV320AIC23_PWR, 4, 1,
147 &tlv320aic23_output_mixer_controls[0],
148 ARRAY_SIZE(tlv320aic23_output_mixer_controls)),
149 SND_SOC_DAPM_PGA("Line Input", TLV320AIC23_PWR, 0, 1, NULL, 0),
150 SND_SOC_DAPM_PGA("Mic Input", TLV320AIC23_PWR, 1, 1, NULL, 0),
151
152 SND_SOC_DAPM_OUTPUT("LHPOUT"),
153 SND_SOC_DAPM_OUTPUT("RHPOUT"),
154 SND_SOC_DAPM_OUTPUT("LOUT"),
155 SND_SOC_DAPM_OUTPUT("ROUT"),
156
157 SND_SOC_DAPM_INPUT("LLINEIN"),
158 SND_SOC_DAPM_INPUT("RLINEIN"),
159
160 SND_SOC_DAPM_INPUT("MICIN"),
161};
162
Lu Guanquna7dca702011-03-30 21:53:16 +0800163static const struct snd_soc_dapm_route tlv320aic23_intercon[] = {
Arun KSc1f27192008-10-02 14:45:49 +0530164 /* Output Mixer */
165 {"Output Mixer", "Line Bypass Switch", "Line Input"},
166 {"Output Mixer", "Playback Switch", "DAC"},
167 {"Output Mixer", "Mic Sidetone Switch", "Mic Input"},
168
169 /* Outputs */
170 {"RHPOUT", NULL, "Output Mixer"},
171 {"LHPOUT", NULL, "Output Mixer"},
172 {"LOUT", NULL, "Output Mixer"},
173 {"ROUT", NULL, "Output Mixer"},
174
175 /* Inputs */
176 {"Line Input", "NULL", "LLINEIN"},
177 {"Line Input", "NULL", "RLINEIN"},
178
179 {"Mic Input", "NULL", "MICIN"},
180
181 /* input mux */
182 {"Capture Source", "Line", "Line Input"},
183 {"Capture Source", "Mic", "Mic Input"},
184 {"ADC", NULL, "Capture Source"},
185
186};
187
Troy Kisky26df91c2008-11-05 18:53:28 +0000188/* AIC23 driver data */
189struct aic23 {
Mark Brown4aa11d62013-09-24 19:26:08 +0100190 struct regmap *regmap;
Troy Kisky26df91c2008-11-05 18:53:28 +0000191 int mclk;
192 int requested_adc;
193 int requested_dac;
Arun KSc1f27192008-10-02 14:45:49 +0530194};
195
Troy Kisky26df91c2008-11-05 18:53:28 +0000196/*
197 * Common Crystals used
198 * 11.2896 Mhz /128 = *88.2k /192 = 58.8k
199 * 12.0000 Mhz /125 = *96k /136 = 88.235K
200 * 12.2880 Mhz /128 = *96k /192 = 64k
201 * 16.9344 Mhz /128 = 132.3k /192 = *88.2k
202 * 18.4320 Mhz /128 = 144k /192 = *96k
203 */
204
205/*
206 * Normal BOSR 0-256/2 = 128, 1-384/2 = 192
207 * USB BOSR 0-250/2 = 125, 1-272/2 = 136
208 */
209static const int bosr_usb_divisor_table[] = {
210 128, 125, 192, 136
211};
212#define LOWER_GROUP ((1<<0) | (1<<1) | (1<<2) | (1<<3) | (1<<6) | (1<<7))
213#define UPPER_GROUP ((1<<8) | (1<<9) | (1<<10) | (1<<11) | (1<<15))
214static const unsigned short sr_valid_mask[] = {
215 LOWER_GROUP|UPPER_GROUP, /* Normal, bosr - 0*/
Troy Kisky26df91c2008-11-05 18:53:28 +0000216 LOWER_GROUP, /* Usb, bosr - 0*/
Troy Kiskybab02122009-11-17 13:51:01 -0700217 LOWER_GROUP|UPPER_GROUP, /* Normal, bosr - 1*/
Troy Kisky26df91c2008-11-05 18:53:28 +0000218 UPPER_GROUP, /* Usb, bosr - 1*/
219};
220/*
221 * Every divisor is a factor of 11*12
222 */
223#define SR_MULT (11*12)
Troy Kiskyccff4b12009-06-05 19:15:58 -0700224#define A(x) (SR_MULT/x)
Troy Kisky26df91c2008-11-05 18:53:28 +0000225static const unsigned char sr_adc_mult_table[] = {
Troy Kiskyccff4b12009-06-05 19:15:58 -0700226 A(2), A(2), A(12), A(12), 0, 0, A(3), A(1),
227 A(2), A(2), A(11), A(11), 0, 0, 0, A(1)
Troy Kisky26df91c2008-11-05 18:53:28 +0000228};
229static const unsigned char sr_dac_mult_table[] = {
Troy Kiskyccff4b12009-06-05 19:15:58 -0700230 A(2), A(12), A(2), A(12), 0, 0, A(3), A(1),
231 A(2), A(11), A(2), A(11), 0, 0, 0, A(1)
Troy Kisky26df91c2008-11-05 18:53:28 +0000232};
233
234static unsigned get_score(int adc, int adc_l, int adc_h, int need_adc,
235 int dac, int dac_l, int dac_h, int need_dac)
236{
237 if ((adc >= adc_l) && (adc <= adc_h) &&
238 (dac >= dac_l) && (dac <= dac_h)) {
239 int diff_adc = need_adc - adc;
240 int diff_dac = need_dac - dac;
241 return abs(diff_adc) + abs(diff_dac);
242 }
243 return UINT_MAX;
244}
245
246static int find_rate(int mclk, u32 need_adc, u32 need_dac)
247{
248 int i, j;
249 int best_i = -1;
250 int best_j = -1;
251 int best_div = 0;
252 unsigned best_score = UINT_MAX;
253 int adc_l, adc_h, dac_l, dac_h;
254
255 need_adc *= SR_MULT;
256 need_dac *= SR_MULT;
257 /*
258 * rates given are +/- 1/32
259 */
260 adc_l = need_adc - (need_adc >> 5);
261 adc_h = need_adc + (need_adc >> 5);
262 dac_l = need_dac - (need_dac >> 5);
263 dac_h = need_dac + (need_dac >> 5);
Mark Brown8d702f22008-11-17 21:42:01 +0000264 for (i = 0; i < ARRAY_SIZE(bosr_usb_divisor_table); i++) {
Troy Kisky26df91c2008-11-05 18:53:28 +0000265 int base = mclk / bosr_usb_divisor_table[i];
266 int mask = sr_valid_mask[i];
Mark Brown8d702f22008-11-17 21:42:01 +0000267 for (j = 0; j < ARRAY_SIZE(sr_adc_mult_table);
268 j++, mask >>= 1) {
Troy Kisky26df91c2008-11-05 18:53:28 +0000269 int adc;
270 int dac;
271 int score;
272 if ((mask & 1) == 0)
273 continue;
274 adc = base * sr_adc_mult_table[j];
275 dac = base * sr_dac_mult_table[j];
276 score = get_score(adc, adc_l, adc_h, need_adc,
277 dac, dac_l, dac_h, need_dac);
278 if (best_score > score) {
279 best_score = score;
280 best_i = i;
281 best_j = j;
282 best_div = 0;
283 }
284 score = get_score((adc >> 1), adc_l, adc_h, need_adc,
285 (dac >> 1), dac_l, dac_h, need_dac);
286 /* prefer to have a /2 */
287 if ((score != UINT_MAX) && (best_score >= score)) {
288 best_score = score;
289 best_i = i;
290 best_j = j;
291 best_div = 1;
292 }
293 }
294 }
295 return (best_j << 2) | best_i | (best_div << TLV320AIC23_CLKIN_SHIFT);
296}
297
Mark Brown8d702f22008-11-17 21:42:01 +0000298#ifdef DEBUG
Troy Kisky26df91c2008-11-05 18:53:28 +0000299static void get_current_sample_rates(struct snd_soc_codec *codec, int mclk,
300 u32 *sample_rate_adc, u32 *sample_rate_dac)
301{
Axel Linf9dfbf92011-10-13 15:06:43 +0800302 int src = snd_soc_read(codec, TLV320AIC23_SRATE);
Troy Kisky26df91c2008-11-05 18:53:28 +0000303 int sr = (src >> 2) & 0x0f;
304 int val = (mclk / bosr_usb_divisor_table[src & 3]);
305 int adc = (val * sr_adc_mult_table[sr]) / SR_MULT;
306 int dac = (val * sr_dac_mult_table[sr]) / SR_MULT;
307 if (src & TLV320AIC23_CLKIN_HALF) {
308 adc >>= 1;
309 dac >>= 1;
310 }
311 *sample_rate_adc = adc;
312 *sample_rate_dac = dac;
313}
Mark Brown8d702f22008-11-17 21:42:01 +0000314#endif
Troy Kisky26df91c2008-11-05 18:53:28 +0000315
316static int set_sample_rate_control(struct snd_soc_codec *codec, int mclk,
317 u32 sample_rate_adc, u32 sample_rate_dac)
318{
319 /* Search for the right sample rate */
320 int data = find_rate(mclk, sample_rate_adc, sample_rate_dac);
321 if (data < 0) {
322 printk(KERN_ERR "%s:Invalid rate %u,%u requested\n",
323 __func__, sample_rate_adc, sample_rate_dac);
324 return -EINVAL;
325 }
Axel Linf9dfbf92011-10-13 15:06:43 +0800326 snd_soc_write(codec, TLV320AIC23_SRATE, data);
Mark Brown8d702f22008-11-17 21:42:01 +0000327#ifdef DEBUG
328 {
329 u32 adc, dac;
Troy Kisky26df91c2008-11-05 18:53:28 +0000330 get_current_sample_rates(codec, mclk, &adc, &dac);
331 printk(KERN_DEBUG "actual samplerate = %u,%u reg=%x\n",
332 adc, dac, data);
333 }
Mark Brown8d702f22008-11-17 21:42:01 +0000334#endif
Troy Kisky26df91c2008-11-05 18:53:28 +0000335 return 0;
336}
337
Arun KSc1f27192008-10-02 14:45:49 +0530338static int tlv320aic23_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000339 struct snd_pcm_hw_params *params,
340 struct snd_soc_dai *dai)
Arun KSc1f27192008-10-02 14:45:49 +0530341{
Mark Browne6968a12012-04-04 15:58:16 +0100342 struct snd_soc_codec *codec = dai->codec;
Troy Kisky26df91c2008-11-05 18:53:28 +0000343 u16 iface_reg;
344 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000345 struct aic23 *aic23 = snd_soc_codec_get_drvdata(codec);
Troy Kisky26df91c2008-11-05 18:53:28 +0000346 u32 sample_rate_adc = aic23->requested_adc;
347 u32 sample_rate_dac = aic23->requested_dac;
348 u32 sample_rate = params_rate(params);
349
350 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
351 aic23->requested_dac = sample_rate_dac = sample_rate;
352 if (!sample_rate_adc)
353 sample_rate_adc = sample_rate;
354 } else {
355 aic23->requested_adc = sample_rate_adc = sample_rate;
356 if (!sample_rate_dac)
357 sample_rate_dac = sample_rate;
358 }
359 ret = set_sample_rate_control(codec, aic23->mclk, sample_rate_adc,
360 sample_rate_dac);
361 if (ret < 0)
362 return ret;
Arun KSc1f27192008-10-02 14:45:49 +0530363
Axel Linf9dfbf92011-10-13 15:06:43 +0800364 iface_reg = snd_soc_read(codec, TLV320AIC23_DIGT_FMT) & ~(0x03 << 2);
365
Arun KSc1f27192008-10-02 14:45:49 +0530366 switch (params_format(params)) {
367 case SNDRV_PCM_FORMAT_S16_LE:
368 break;
369 case SNDRV_PCM_FORMAT_S20_3LE:
370 iface_reg |= (0x01 << 2);
371 break;
372 case SNDRV_PCM_FORMAT_S24_LE:
373 iface_reg |= (0x02 << 2);
374 break;
375 case SNDRV_PCM_FORMAT_S32_LE:
376 iface_reg |= (0x03 << 2);
377 break;
378 }
Axel Linf9dfbf92011-10-13 15:06:43 +0800379 snd_soc_write(codec, TLV320AIC23_DIGT_FMT, iface_reg);
Arun KSc1f27192008-10-02 14:45:49 +0530380
381 return 0;
382}
383
Mark Browndee89c42008-11-18 22:11:38 +0000384static int tlv320aic23_pcm_prepare(struct snd_pcm_substream *substream,
385 struct snd_soc_dai *dai)
Arun KSc1f27192008-10-02 14:45:49 +0530386{
Mark Browne6968a12012-04-04 15:58:16 +0100387 struct snd_soc_codec *codec = dai->codec;
Arun KSc1f27192008-10-02 14:45:49 +0530388
389 /* set active */
Axel Linf9dfbf92011-10-13 15:06:43 +0800390 snd_soc_write(codec, TLV320AIC23_ACTIVE, 0x0001);
Arun KSc1f27192008-10-02 14:45:49 +0530391
392 return 0;
393}
394
Mark Browndee89c42008-11-18 22:11:38 +0000395static void tlv320aic23_shutdown(struct snd_pcm_substream *substream,
396 struct snd_soc_dai *dai)
Arun KSc1f27192008-10-02 14:45:49 +0530397{
Mark Browne6968a12012-04-04 15:58:16 +0100398 struct snd_soc_codec *codec = dai->codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000399 struct aic23 *aic23 = snd_soc_codec_get_drvdata(codec);
Arun KSc1f27192008-10-02 14:45:49 +0530400
401 /* deactivate */
402 if (!codec->active) {
403 udelay(50);
Axel Linf9dfbf92011-10-13 15:06:43 +0800404 snd_soc_write(codec, TLV320AIC23_ACTIVE, 0x0);
Arun KSc1f27192008-10-02 14:45:49 +0530405 }
Troy Kisky26df91c2008-11-05 18:53:28 +0000406 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
407 aic23->requested_dac = 0;
408 else
409 aic23->requested_adc = 0;
Arun KSc1f27192008-10-02 14:45:49 +0530410}
411
412static int tlv320aic23_mute(struct snd_soc_dai *dai, int mute)
413{
414 struct snd_soc_codec *codec = dai->codec;
415 u16 reg;
416
Axel Linf9dfbf92011-10-13 15:06:43 +0800417 reg = snd_soc_read(codec, TLV320AIC23_DIGT);
Arun KSc1f27192008-10-02 14:45:49 +0530418 if (mute)
419 reg |= TLV320AIC23_DACM_MUTE;
420
421 else
422 reg &= ~TLV320AIC23_DACM_MUTE;
423
Axel Linf9dfbf92011-10-13 15:06:43 +0800424 snd_soc_write(codec, TLV320AIC23_DIGT, reg);
Arun KSc1f27192008-10-02 14:45:49 +0530425
426 return 0;
427}
428
429static int tlv320aic23_set_dai_fmt(struct snd_soc_dai *codec_dai,
430 unsigned int fmt)
431{
432 struct snd_soc_codec *codec = codec_dai->codec;
433 u16 iface_reg;
434
Axel Linf9dfbf92011-10-13 15:06:43 +0800435 iface_reg = snd_soc_read(codec, TLV320AIC23_DIGT_FMT) & (~0x03);
Arun KSc1f27192008-10-02 14:45:49 +0530436
437 /* set master/slave audio interface */
438 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
439 case SND_SOC_DAIFMT_CBM_CFM:
440 iface_reg |= TLV320AIC23_MS_MASTER;
441 break;
442 case SND_SOC_DAIFMT_CBS_CFS:
Axel Linb01a3d62011-10-27 16:35:49 +0800443 iface_reg &= ~TLV320AIC23_MS_MASTER;
Arun KSc1f27192008-10-02 14:45:49 +0530444 break;
445 default:
446 return -EINVAL;
447
448 }
449
450 /* interface format */
451 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
452 case SND_SOC_DAIFMT_I2S:
453 iface_reg |= TLV320AIC23_FOR_I2S;
454 break;
Peter Ujfalusi894bf922009-04-09 12:34:40 +0300455 case SND_SOC_DAIFMT_DSP_A:
456 iface_reg |= TLV320AIC23_LRP_ON;
Jarkko Nikulabd258672008-12-22 10:21:36 +0200457 case SND_SOC_DAIFMT_DSP_B:
Arun KSc1f27192008-10-02 14:45:49 +0530458 iface_reg |= TLV320AIC23_FOR_DSP;
459 break;
460 case SND_SOC_DAIFMT_RIGHT_J:
461 break;
462 case SND_SOC_DAIFMT_LEFT_J:
463 iface_reg |= TLV320AIC23_FOR_LJUST;
464 break;
465 default:
466 return -EINVAL;
467
468 }
469
Axel Linf9dfbf92011-10-13 15:06:43 +0800470 snd_soc_write(codec, TLV320AIC23_DIGT_FMT, iface_reg);
Arun KSc1f27192008-10-02 14:45:49 +0530471
472 return 0;
473}
474
475static int tlv320aic23_set_dai_sysclk(struct snd_soc_dai *codec_dai,
476 int clk_id, unsigned int freq, int dir)
477{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000478 struct aic23 *aic23 = snd_soc_dai_get_drvdata(codec_dai);
Troy Kisky26df91c2008-11-05 18:53:28 +0000479 aic23->mclk = freq;
480 return 0;
Arun KSc1f27192008-10-02 14:45:49 +0530481}
482
483static int tlv320aic23_set_bias_level(struct snd_soc_codec *codec,
484 enum snd_soc_bias_level level)
485{
Eric Bénarde875c1e2012-04-29 17:37:57 +0200486 u16 reg = snd_soc_read(codec, TLV320AIC23_PWR) & 0x17f;
Arun KSc1f27192008-10-02 14:45:49 +0530487
488 switch (level) {
489 case SND_SOC_BIAS_ON:
490 /* vref/mid, osc on, dac unmute */
Eric Bénard3d5a4512010-06-19 19:33:39 +0200491 reg &= ~(TLV320AIC23_DEVICE_PWR_OFF | TLV320AIC23_OSC_OFF | \
492 TLV320AIC23_DAC_OFF);
Axel Linf9dfbf92011-10-13 15:06:43 +0800493 snd_soc_write(codec, TLV320AIC23_PWR, reg);
Arun KSc1f27192008-10-02 14:45:49 +0530494 break;
495 case SND_SOC_BIAS_PREPARE:
496 break;
497 case SND_SOC_BIAS_STANDBY:
498 /* everything off except vref/vmid, */
Axel Linf9dfbf92011-10-13 15:06:43 +0800499 snd_soc_write(codec, TLV320AIC23_PWR,
500 reg | TLV320AIC23_CLK_OFF);
Arun KSc1f27192008-10-02 14:45:49 +0530501 break;
502 case SND_SOC_BIAS_OFF:
503 /* everything off, dac mute, inactive */
Axel Linf9dfbf92011-10-13 15:06:43 +0800504 snd_soc_write(codec, TLV320AIC23_ACTIVE, 0x0);
Eric Bénarde875c1e2012-04-29 17:37:57 +0200505 snd_soc_write(codec, TLV320AIC23_PWR, 0x1ff);
Arun KSc1f27192008-10-02 14:45:49 +0530506 break;
507 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200508 codec->dapm.bias_level = level;
Arun KSc1f27192008-10-02 14:45:49 +0530509 return 0;
510}
511
512#define AIC23_RATES SNDRV_PCM_RATE_8000_96000
513#define AIC23_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
514 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)
515
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100516static const struct snd_soc_dai_ops tlv320aic23_dai_ops = {
Eric Miao6335d052009-03-03 09:41:00 +0800517 .prepare = tlv320aic23_pcm_prepare,
518 .hw_params = tlv320aic23_hw_params,
519 .shutdown = tlv320aic23_shutdown,
520 .digital_mute = tlv320aic23_mute,
521 .set_fmt = tlv320aic23_set_dai_fmt,
522 .set_sysclk = tlv320aic23_set_dai_sysclk,
523};
524
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000525static struct snd_soc_dai_driver tlv320aic23_dai = {
526 .name = "tlv320aic23-hifi",
Arun KSc1f27192008-10-02 14:45:49 +0530527 .playback = {
528 .stream_name = "Playback",
529 .channels_min = 2,
530 .channels_max = 2,
531 .rates = AIC23_RATES,
532 .formats = AIC23_FORMATS,},
533 .capture = {
534 .stream_name = "Capture",
535 .channels_min = 2,
536 .channels_max = 2,
537 .rates = AIC23_RATES,
538 .formats = AIC23_FORMATS,},
Eric Miao6335d052009-03-03 09:41:00 +0800539 .ops = &tlv320aic23_dai_ops,
Arun KSc1f27192008-10-02 14:45:49 +0530540};
Arun KSc1f27192008-10-02 14:45:49 +0530541
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +0100542static int tlv320aic23_suspend(struct snd_soc_codec *codec)
Arun KSc1f27192008-10-02 14:45:49 +0530543{
Arun KSc1f27192008-10-02 14:45:49 +0530544 tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_OFF);
545
546 return 0;
547}
548
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000549static int tlv320aic23_resume(struct snd_soc_codec *codec)
Arun KSc1f27192008-10-02 14:45:49 +0530550{
Mark Brown4aa11d62013-09-24 19:26:08 +0100551 struct aic23 *aic23 = snd_soc_codec_get_drvdata(codec);
552 regcache_mark_dirty(aic23->regmap);
553 regcache_sync(aic23->regmap);
Arun KSc1f27192008-10-02 14:45:49 +0530554 tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
Arun KSc1f27192008-10-02 14:45:49 +0530555
556 return 0;
557}
558
Max Filippovb3fc5722014-03-06 14:04:41 +0400559static int tlv320aic23_codec_probe(struct snd_soc_codec *codec)
Arun KSc1f27192008-10-02 14:45:49 +0530560{
Axel Linf9dfbf92011-10-13 15:06:43 +0800561 int ret;
Arun KSc1f27192008-10-02 14:45:49 +0530562
Mark Brown4aa11d62013-09-24 19:26:08 +0100563 ret = snd_soc_codec_set_cache_io(codec, 7, 9, SND_SOC_REGMAP);
Axel Linf9dfbf92011-10-13 15:06:43 +0800564 if (ret < 0) {
565 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
566 return ret;
567 }
Arun KSc1f27192008-10-02 14:45:49 +0530568
569 /* Reset codec */
Axel Linf9dfbf92011-10-13 15:06:43 +0800570 snd_soc_write(codec, TLV320AIC23_RESET, 0);
571
Arun KSc1f27192008-10-02 14:45:49 +0530572 /* power on device */
573 tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
574
Axel Linf9dfbf92011-10-13 15:06:43 +0800575 snd_soc_write(codec, TLV320AIC23_DIGT, TLV320AIC23_DEEMP_44K);
Arun KSc1f27192008-10-02 14:45:49 +0530576
577 /* Unmute input */
Axel Linf9dfbf92011-10-13 15:06:43 +0800578 snd_soc_update_bits(codec, TLV320AIC23_LINVOL,
579 TLV320AIC23_LIM_MUTED, TLV320AIC23_LRS_ENABLED);
Arun KSc1f27192008-10-02 14:45:49 +0530580
Axel Linf9dfbf92011-10-13 15:06:43 +0800581 snd_soc_update_bits(codec, TLV320AIC23_RINVOL,
582 TLV320AIC23_LIM_MUTED, TLV320AIC23_LRS_ENABLED);
Arun KSc1f27192008-10-02 14:45:49 +0530583
Axel Linf9dfbf92011-10-13 15:06:43 +0800584 snd_soc_update_bits(codec, TLV320AIC23_ANLG,
585 TLV320AIC23_BYPASS_ON | TLV320AIC23_MICM_MUTED,
586 0);
Arun KSc1f27192008-10-02 14:45:49 +0530587
588 /* Default output volume */
Axel Linf9dfbf92011-10-13 15:06:43 +0800589 snd_soc_write(codec, TLV320AIC23_LCHNVOL,
590 TLV320AIC23_DEFAULT_OUT_VOL & TLV320AIC23_OUT_VOL_MASK);
591 snd_soc_write(codec, TLV320AIC23_RCHNVOL,
592 TLV320AIC23_DEFAULT_OUT_VOL & TLV320AIC23_OUT_VOL_MASK);
Arun KSc1f27192008-10-02 14:45:49 +0530593
Axel Linf9dfbf92011-10-13 15:06:43 +0800594 snd_soc_write(codec, TLV320AIC23_ACTIVE, 0x1);
Arun KSc1f27192008-10-02 14:45:49 +0530595
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000596 return 0;
Arun KSc1f27192008-10-02 14:45:49 +0530597}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000598
599static int tlv320aic23_remove(struct snd_soc_codec *codec)
600{
601 tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_OFF);
602 return 0;
603}
604
605static struct snd_soc_codec_driver soc_codec_dev_tlv320aic23 = {
Max Filippovb3fc5722014-03-06 14:04:41 +0400606 .probe = tlv320aic23_codec_probe,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000607 .remove = tlv320aic23_remove,
608 .suspend = tlv320aic23_suspend,
609 .resume = tlv320aic23_resume,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000610 .set_bias_level = tlv320aic23_set_bias_level,
Mark Brownb07c4432013-09-24 18:51:26 +0100611 .controls = tlv320aic23_snd_controls,
612 .num_controls = ARRAY_SIZE(tlv320aic23_snd_controls),
Lu Guanquna7dca702011-03-30 21:53:16 +0800613 .dapm_widgets = tlv320aic23_dapm_widgets,
614 .num_dapm_widgets = ARRAY_SIZE(tlv320aic23_dapm_widgets),
615 .dapm_routes = tlv320aic23_intercon,
616 .num_dapm_routes = ARRAY_SIZE(tlv320aic23_intercon),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000617};
Arun KSc1f27192008-10-02 14:45:49 +0530618
Max Filippovb3fc5722014-03-06 14:04:41 +0400619int tlv320aic23_probe(struct device *dev, struct regmap *regmap)
Arun KSc1f27192008-10-02 14:45:49 +0530620{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000621 struct aic23 *aic23;
Arun KSc1f27192008-10-02 14:45:49 +0530622
Max Filippovb3fc5722014-03-06 14:04:41 +0400623 if (IS_ERR(regmap))
624 return PTR_ERR(regmap);
Arun KSc1f27192008-10-02 14:45:49 +0530625
Max Filippovb3fc5722014-03-06 14:04:41 +0400626 aic23 = devm_kzalloc(dev, sizeof(struct aic23), GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000627 if (aic23 == NULL)
628 return -ENOMEM;
Arun KSc1f27192008-10-02 14:45:49 +0530629
Max Filippovb3fc5722014-03-06 14:04:41 +0400630 aic23->regmap = regmap;
Mark Brown4aa11d62013-09-24 19:26:08 +0100631
Max Filippovb3fc5722014-03-06 14:04:41 +0400632 dev_set_drvdata(dev, aic23);
Arun KSc1f27192008-10-02 14:45:49 +0530633
Max Filippovb3fc5722014-03-06 14:04:41 +0400634 return snd_soc_register_codec(dev, &soc_codec_dev_tlv320aic23,
635 &tlv320aic23_dai, 1);
Arun KSc1f27192008-10-02 14:45:49 +0530636}
Mark Brown64089b82008-12-08 19:17:58 +0000637
Arun KSc1f27192008-10-02 14:45:49 +0530638MODULE_DESCRIPTION("ASoC TLV320AIC23 codec driver");
639MODULE_AUTHOR("Arun KS <arunks@mistralsolutions.com>");
640MODULE_LICENSE("GPL");