blob: 080a840c987a77d928b275ea0cdf836816289fc6 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Arun KSc1f27192008-10-02 14:45:49 +05302/*
3 * ALSA SoC TLV320AIC23 codec driver
4 *
5 * Author: Arun KS, <arunks@mistralsolutions.com>
6 * Copyright: (C) 2008 Mistral Solutions Pvt Ltd.,
7 *
8 * Based on sound/soc/codecs/wm8731.c by Richard Purdie
9 *
Arun KSc1f27192008-10-02 14:45:49 +053010 * Notes:
11 * The AIC23 is a driver for a low power stereo audio
12 * codec tlv320aic23
13 *
14 * The machine layer should disable unsupported inputs/outputs by
15 * snd_soc_dapm_disable_pin(codec, "LHPOUT"), etc.
16 */
17
18#include <linux/module.h>
19#include <linux/moduleparam.h>
20#include <linux/init.h>
21#include <linux/delay.h>
22#include <linux/pm.h>
Mark Brown4aa11d62013-09-24 19:26:08 +010023#include <linux/regmap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Arun KSc1f27192008-10-02 14:45:49 +053025#include <sound/core.h>
26#include <sound/pcm.h>
27#include <sound/pcm_params.h>
28#include <sound/soc.h>
Arun KSc1f27192008-10-02 14:45:49 +053029#include <sound/tlv.h>
30#include <sound/initval.h>
31
32#include "tlv320aic23.h"
33
Arun KSc1f27192008-10-02 14:45:49 +053034/*
35 * AIC23 register cache
36 */
Mark Brown4aa11d62013-09-24 19:26:08 +010037static const struct reg_default tlv320aic23_reg[] = {
38 { 0, 0x0097 },
39 { 1, 0x0097 },
40 { 2, 0x00F9 },
41 { 3, 0x00F9 },
42 { 4, 0x001A },
43 { 5, 0x0004 },
44 { 6, 0x0007 },
45 { 7, 0x0001 },
46 { 8, 0x0020 },
47 { 9, 0x0000 },
48};
49
Max Filippovb3fc5722014-03-06 14:04:41 +040050const struct regmap_config tlv320aic23_regmap = {
Mark Brown4aa11d62013-09-24 19:26:08 +010051 .reg_bits = 7,
52 .val_bits = 9,
53
54 .max_register = TLV320AIC23_RESET,
55 .reg_defaults = tlv320aic23_reg,
56 .num_reg_defaults = ARRAY_SIZE(tlv320aic23_reg),
57 .cache_type = REGCACHE_RBTREE,
Arun KSc1f27192008-10-02 14:45:49 +053058};
Max Filippov40423282014-03-08 13:31:06 +040059EXPORT_SYMBOL(tlv320aic23_regmap);
Arun KSc1f27192008-10-02 14:45:49 +053060
Arun KSc1f27192008-10-02 14:45:49 +053061static const char *rec_src_text[] = { "Line", "Mic" };
62static const char *deemph_text[] = {"None", "32Khz", "44.1Khz", "48Khz"};
Arun KSc1f27192008-10-02 14:45:49 +053063
Takashi Iwai806057c2014-02-18 10:45:53 +010064static SOC_ENUM_SINGLE_DECL(rec_src_enum,
65 TLV320AIC23_ANLG, 2, rec_src_text);
Arun KSc1f27192008-10-02 14:45:49 +053066
67static const struct snd_kcontrol_new tlv320aic23_rec_src_mux_controls =
68SOC_DAPM_ENUM("Input Select", rec_src_enum);
69
Takashi Iwai806057c2014-02-18 10:45:53 +010070static SOC_ENUM_SINGLE_DECL(tlv320aic23_rec_src,
71 TLV320AIC23_ANLG, 2, rec_src_text);
72static SOC_ENUM_SINGLE_DECL(tlv320aic23_deemph,
73 TLV320AIC23_DIGT, 1, deemph_text);
Arun KSc1f27192008-10-02 14:45:49 +053074
75static const DECLARE_TLV_DB_SCALE(out_gain_tlv, -12100, 100, 0);
76static const DECLARE_TLV_DB_SCALE(input_gain_tlv, -1725, 75, 0);
Arun KSdf91ddf2008-10-03 17:07:30 +053077static const DECLARE_TLV_DB_SCALE(sidetone_vol_tlv, -1800, 300, 0);
78
79static int snd_soc_tlv320aic23_put_volsw(struct snd_kcontrol *kcontrol,
80 struct snd_ctl_elem_value *ucontrol)
81{
Kuninori Morimotoff06ac22018-01-29 04:12:48 +000082 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
Arun KSdf91ddf2008-10-03 17:07:30 +053083 u16 val, reg;
84
85 val = (ucontrol->value.integer.value[0] & 0x07);
86
87 /* linear conversion to userspace
88 * 000 = -6db
89 * 001 = -9db
90 * 010 = -12db
91 * 011 = -18db (Min)
92 * 100 = 0db (Max)
93 */
94 val = (val >= 4) ? 4 : (3 - val);
95
Kuninori Morimotoff06ac22018-01-29 04:12:48 +000096 reg = snd_soc_component_read32(component, TLV320AIC23_ANLG) & (~0x1C0);
97 snd_soc_component_write(component, TLV320AIC23_ANLG, reg | (val << 6));
Arun KSdf91ddf2008-10-03 17:07:30 +053098
99 return 0;
100}
101
102static int snd_soc_tlv320aic23_get_volsw(struct snd_kcontrol *kcontrol,
103 struct snd_ctl_elem_value *ucontrol)
104{
Kuninori Morimotoff06ac22018-01-29 04:12:48 +0000105 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
Arun KSdf91ddf2008-10-03 17:07:30 +0530106 u16 val;
107
Kuninori Morimotoff06ac22018-01-29 04:12:48 +0000108 val = snd_soc_component_read32(component, TLV320AIC23_ANLG) & (0x1C0);
Arun KSdf91ddf2008-10-03 17:07:30 +0530109 val = val >> 6;
110 val = (val >= 4) ? 4 : (3 - val);
111 ucontrol->value.integer.value[0] = val;
112 return 0;
113
114}
115
Arun KSc1f27192008-10-02 14:45:49 +0530116static const struct snd_kcontrol_new tlv320aic23_snd_controls[] = {
117 SOC_DOUBLE_R_TLV("Digital Playback Volume", TLV320AIC23_LCHNVOL,
118 TLV320AIC23_RCHNVOL, 0, 127, 0, out_gain_tlv),
119 SOC_SINGLE("Digital Playback Switch", TLV320AIC23_DIGT, 3, 1, 1),
120 SOC_DOUBLE_R("Line Input Switch", TLV320AIC23_LINVOL,
121 TLV320AIC23_RINVOL, 7, 1, 0),
122 SOC_DOUBLE_R_TLV("Line Input Volume", TLV320AIC23_LINVOL,
123 TLV320AIC23_RINVOL, 0, 31, 0, input_gain_tlv),
124 SOC_SINGLE("Mic Input Switch", TLV320AIC23_ANLG, 1, 1, 1),
125 SOC_SINGLE("Mic Booster Switch", TLV320AIC23_ANLG, 0, 1, 0),
Peter Ujfalusi0f9887d2011-10-05 10:29:19 +0300126 SOC_SINGLE_EXT_TLV("Sidetone Volume", TLV320AIC23_ANLG, 6, 4, 0,
127 snd_soc_tlv320aic23_get_volsw,
128 snd_soc_tlv320aic23_put_volsw, sidetone_vol_tlv),
Arun KSc1f27192008-10-02 14:45:49 +0530129 SOC_ENUM("Playback De-emphasis", tlv320aic23_deemph),
130};
131
Arun KSc1f27192008-10-02 14:45:49 +0530132/* PGA Mixer controls for Line and Mic switch */
133static const struct snd_kcontrol_new tlv320aic23_output_mixer_controls[] = {
134 SOC_DAPM_SINGLE("Line Bypass Switch", TLV320AIC23_ANLG, 3, 1, 0),
135 SOC_DAPM_SINGLE("Mic Sidetone Switch", TLV320AIC23_ANLG, 5, 1, 0),
136 SOC_DAPM_SINGLE("Playback Switch", TLV320AIC23_ANLG, 4, 1, 0),
137};
138
139static const struct snd_soc_dapm_widget tlv320aic23_dapm_widgets[] = {
140 SND_SOC_DAPM_DAC("DAC", "Playback", TLV320AIC23_PWR, 3, 1),
141 SND_SOC_DAPM_ADC("ADC", "Capture", TLV320AIC23_PWR, 2, 1),
142 SND_SOC_DAPM_MUX("Capture Source", SND_SOC_NOPM, 0, 0,
143 &tlv320aic23_rec_src_mux_controls),
144 SND_SOC_DAPM_MIXER("Output Mixer", TLV320AIC23_PWR, 4, 1,
145 &tlv320aic23_output_mixer_controls[0],
146 ARRAY_SIZE(tlv320aic23_output_mixer_controls)),
147 SND_SOC_DAPM_PGA("Line Input", TLV320AIC23_PWR, 0, 1, NULL, 0),
148 SND_SOC_DAPM_PGA("Mic Input", TLV320AIC23_PWR, 1, 1, NULL, 0),
149
150 SND_SOC_DAPM_OUTPUT("LHPOUT"),
151 SND_SOC_DAPM_OUTPUT("RHPOUT"),
152 SND_SOC_DAPM_OUTPUT("LOUT"),
153 SND_SOC_DAPM_OUTPUT("ROUT"),
154
155 SND_SOC_DAPM_INPUT("LLINEIN"),
156 SND_SOC_DAPM_INPUT("RLINEIN"),
157
158 SND_SOC_DAPM_INPUT("MICIN"),
159};
160
Lu Guanquna7dca702011-03-30 21:53:16 +0800161static const struct snd_soc_dapm_route tlv320aic23_intercon[] = {
Arun KSc1f27192008-10-02 14:45:49 +0530162 /* Output Mixer */
163 {"Output Mixer", "Line Bypass Switch", "Line Input"},
164 {"Output Mixer", "Playback Switch", "DAC"},
165 {"Output Mixer", "Mic Sidetone Switch", "Mic Input"},
166
167 /* Outputs */
168 {"RHPOUT", NULL, "Output Mixer"},
169 {"LHPOUT", NULL, "Output Mixer"},
170 {"LOUT", NULL, "Output Mixer"},
171 {"ROUT", NULL, "Output Mixer"},
172
173 /* Inputs */
Liviu Dudaua03faba2017-03-01 12:26:28 +0000174 {"Line Input", NULL, "LLINEIN"},
175 {"Line Input", NULL, "RLINEIN"},
176 {"Mic Input", NULL, "MICIN"},
Arun KSc1f27192008-10-02 14:45:49 +0530177
178 /* input mux */
179 {"Capture Source", "Line", "Line Input"},
180 {"Capture Source", "Mic", "Mic Input"},
181 {"ADC", NULL, "Capture Source"},
182
183};
184
Troy Kisky26df91c2008-11-05 18:53:28 +0000185/* AIC23 driver data */
186struct aic23 {
Mark Brown4aa11d62013-09-24 19:26:08 +0100187 struct regmap *regmap;
Troy Kisky26df91c2008-11-05 18:53:28 +0000188 int mclk;
189 int requested_adc;
190 int requested_dac;
Arun KSc1f27192008-10-02 14:45:49 +0530191};
192
Troy Kisky26df91c2008-11-05 18:53:28 +0000193/*
194 * Common Crystals used
195 * 11.2896 Mhz /128 = *88.2k /192 = 58.8k
196 * 12.0000 Mhz /125 = *96k /136 = 88.235K
197 * 12.2880 Mhz /128 = *96k /192 = 64k
198 * 16.9344 Mhz /128 = 132.3k /192 = *88.2k
199 * 18.4320 Mhz /128 = 144k /192 = *96k
200 */
201
202/*
203 * Normal BOSR 0-256/2 = 128, 1-384/2 = 192
204 * USB BOSR 0-250/2 = 125, 1-272/2 = 136
205 */
206static const int bosr_usb_divisor_table[] = {
207 128, 125, 192, 136
208};
209#define LOWER_GROUP ((1<<0) | (1<<1) | (1<<2) | (1<<3) | (1<<6) | (1<<7))
210#define UPPER_GROUP ((1<<8) | (1<<9) | (1<<10) | (1<<11) | (1<<15))
211static const unsigned short sr_valid_mask[] = {
212 LOWER_GROUP|UPPER_GROUP, /* Normal, bosr - 0*/
Troy Kisky26df91c2008-11-05 18:53:28 +0000213 LOWER_GROUP, /* Usb, bosr - 0*/
Troy Kiskybab02122009-11-17 13:51:01 -0700214 LOWER_GROUP|UPPER_GROUP, /* Normal, bosr - 1*/
Troy Kisky26df91c2008-11-05 18:53:28 +0000215 UPPER_GROUP, /* Usb, bosr - 1*/
216};
217/*
218 * Every divisor is a factor of 11*12
219 */
220#define SR_MULT (11*12)
Troy Kiskyccff4b12009-06-05 19:15:58 -0700221#define A(x) (SR_MULT/x)
Troy Kisky26df91c2008-11-05 18:53:28 +0000222static const unsigned char sr_adc_mult_table[] = {
Troy Kiskyccff4b12009-06-05 19:15:58 -0700223 A(2), A(2), A(12), A(12), 0, 0, A(3), A(1),
224 A(2), A(2), A(11), A(11), 0, 0, 0, A(1)
Troy Kisky26df91c2008-11-05 18:53:28 +0000225};
226static const unsigned char sr_dac_mult_table[] = {
Troy Kiskyccff4b12009-06-05 19:15:58 -0700227 A(2), A(12), A(2), A(12), 0, 0, A(3), A(1),
228 A(2), A(11), A(2), A(11), 0, 0, 0, A(1)
Troy Kisky26df91c2008-11-05 18:53:28 +0000229};
230
231static unsigned get_score(int adc, int adc_l, int adc_h, int need_adc,
232 int dac, int dac_l, int dac_h, int need_dac)
233{
234 if ((adc >= adc_l) && (adc <= adc_h) &&
235 (dac >= dac_l) && (dac <= dac_h)) {
236 int diff_adc = need_adc - adc;
237 int diff_dac = need_dac - dac;
238 return abs(diff_adc) + abs(diff_dac);
239 }
240 return UINT_MAX;
241}
242
243static int find_rate(int mclk, u32 need_adc, u32 need_dac)
244{
245 int i, j;
246 int best_i = -1;
247 int best_j = -1;
248 int best_div = 0;
249 unsigned best_score = UINT_MAX;
250 int adc_l, adc_h, dac_l, dac_h;
251
252 need_adc *= SR_MULT;
253 need_dac *= SR_MULT;
254 /*
255 * rates given are +/- 1/32
256 */
257 adc_l = need_adc - (need_adc >> 5);
258 adc_h = need_adc + (need_adc >> 5);
259 dac_l = need_dac - (need_dac >> 5);
260 dac_h = need_dac + (need_dac >> 5);
Mark Brown8d702f22008-11-17 21:42:01 +0000261 for (i = 0; i < ARRAY_SIZE(bosr_usb_divisor_table); i++) {
Troy Kisky26df91c2008-11-05 18:53:28 +0000262 int base = mclk / bosr_usb_divisor_table[i];
263 int mask = sr_valid_mask[i];
Mark Brown8d702f22008-11-17 21:42:01 +0000264 for (j = 0; j < ARRAY_SIZE(sr_adc_mult_table);
265 j++, mask >>= 1) {
Troy Kisky26df91c2008-11-05 18:53:28 +0000266 int adc;
267 int dac;
268 int score;
269 if ((mask & 1) == 0)
270 continue;
271 adc = base * sr_adc_mult_table[j];
272 dac = base * sr_dac_mult_table[j];
273 score = get_score(adc, adc_l, adc_h, need_adc,
274 dac, dac_l, dac_h, need_dac);
275 if (best_score > score) {
276 best_score = score;
277 best_i = i;
278 best_j = j;
279 best_div = 0;
280 }
281 score = get_score((adc >> 1), adc_l, adc_h, need_adc,
282 (dac >> 1), dac_l, dac_h, need_dac);
283 /* prefer to have a /2 */
284 if ((score != UINT_MAX) && (best_score >= score)) {
285 best_score = score;
286 best_i = i;
287 best_j = j;
288 best_div = 1;
289 }
290 }
291 }
292 return (best_j << 2) | best_i | (best_div << TLV320AIC23_CLKIN_SHIFT);
293}
294
Mark Brown8d702f22008-11-17 21:42:01 +0000295#ifdef DEBUG
Kuninori Morimotoff06ac22018-01-29 04:12:48 +0000296static void get_current_sample_rates(struct snd_soc_component *component, int mclk,
Troy Kisky26df91c2008-11-05 18:53:28 +0000297 u32 *sample_rate_adc, u32 *sample_rate_dac)
298{
Kuninori Morimotoff06ac22018-01-29 04:12:48 +0000299 int src = snd_soc_component_read32(component, TLV320AIC23_SRATE);
Troy Kisky26df91c2008-11-05 18:53:28 +0000300 int sr = (src >> 2) & 0x0f;
301 int val = (mclk / bosr_usb_divisor_table[src & 3]);
302 int adc = (val * sr_adc_mult_table[sr]) / SR_MULT;
303 int dac = (val * sr_dac_mult_table[sr]) / SR_MULT;
304 if (src & TLV320AIC23_CLKIN_HALF) {
305 adc >>= 1;
306 dac >>= 1;
307 }
308 *sample_rate_adc = adc;
309 *sample_rate_dac = dac;
310}
Mark Brown8d702f22008-11-17 21:42:01 +0000311#endif
Troy Kisky26df91c2008-11-05 18:53:28 +0000312
Kuninori Morimotoff06ac22018-01-29 04:12:48 +0000313static int set_sample_rate_control(struct snd_soc_component *component, int mclk,
Troy Kisky26df91c2008-11-05 18:53:28 +0000314 u32 sample_rate_adc, u32 sample_rate_dac)
315{
316 /* Search for the right sample rate */
317 int data = find_rate(mclk, sample_rate_adc, sample_rate_dac);
318 if (data < 0) {
319 printk(KERN_ERR "%s:Invalid rate %u,%u requested\n",
320 __func__, sample_rate_adc, sample_rate_dac);
321 return -EINVAL;
322 }
Kuninori Morimotoff06ac22018-01-29 04:12:48 +0000323 snd_soc_component_write(component, TLV320AIC23_SRATE, data);
Mark Brown8d702f22008-11-17 21:42:01 +0000324#ifdef DEBUG
325 {
326 u32 adc, dac;
Kuninori Morimotoff06ac22018-01-29 04:12:48 +0000327 get_current_sample_rates(component, mclk, &adc, &dac);
Troy Kisky26df91c2008-11-05 18:53:28 +0000328 printk(KERN_DEBUG "actual samplerate = %u,%u reg=%x\n",
329 adc, dac, data);
330 }
Mark Brown8d702f22008-11-17 21:42:01 +0000331#endif
Troy Kisky26df91c2008-11-05 18:53:28 +0000332 return 0;
333}
334
Arun KSc1f27192008-10-02 14:45:49 +0530335static int tlv320aic23_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000336 struct snd_pcm_hw_params *params,
337 struct snd_soc_dai *dai)
Arun KSc1f27192008-10-02 14:45:49 +0530338{
Kuninori Morimotoff06ac22018-01-29 04:12:48 +0000339 struct snd_soc_component *component = dai->component;
Troy Kisky26df91c2008-11-05 18:53:28 +0000340 u16 iface_reg;
341 int ret;
Kuninori Morimotoff06ac22018-01-29 04:12:48 +0000342 struct aic23 *aic23 = snd_soc_component_get_drvdata(component);
Troy Kisky26df91c2008-11-05 18:53:28 +0000343 u32 sample_rate_adc = aic23->requested_adc;
344 u32 sample_rate_dac = aic23->requested_dac;
345 u32 sample_rate = params_rate(params);
346
347 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
348 aic23->requested_dac = sample_rate_dac = sample_rate;
349 if (!sample_rate_adc)
350 sample_rate_adc = sample_rate;
351 } else {
352 aic23->requested_adc = sample_rate_adc = sample_rate;
353 if (!sample_rate_dac)
354 sample_rate_dac = sample_rate;
355 }
Kuninori Morimotoff06ac22018-01-29 04:12:48 +0000356 ret = set_sample_rate_control(component, aic23->mclk, sample_rate_adc,
Troy Kisky26df91c2008-11-05 18:53:28 +0000357 sample_rate_dac);
358 if (ret < 0)
359 return ret;
Arun KSc1f27192008-10-02 14:45:49 +0530360
Kuninori Morimotoff06ac22018-01-29 04:12:48 +0000361 iface_reg = snd_soc_component_read32(component, TLV320AIC23_DIGT_FMT) & ~(0x03 << 2);
Axel Linf9dfbf92011-10-13 15:06:43 +0800362
Mark Brown08074dc2014-07-29 20:13:00 +0100363 switch (params_width(params)) {
364 case 16:
Arun KSc1f27192008-10-02 14:45:49 +0530365 break;
Mark Brown08074dc2014-07-29 20:13:00 +0100366 case 20:
Arun KSc1f27192008-10-02 14:45:49 +0530367 iface_reg |= (0x01 << 2);
368 break;
Mark Brown08074dc2014-07-29 20:13:00 +0100369 case 24:
Arun KSc1f27192008-10-02 14:45:49 +0530370 iface_reg |= (0x02 << 2);
371 break;
Mark Brown08074dc2014-07-29 20:13:00 +0100372 case 32:
Arun KSc1f27192008-10-02 14:45:49 +0530373 iface_reg |= (0x03 << 2);
374 break;
375 }
Kuninori Morimotoff06ac22018-01-29 04:12:48 +0000376 snd_soc_component_write(component, TLV320AIC23_DIGT_FMT, iface_reg);
Arun KSc1f27192008-10-02 14:45:49 +0530377
378 return 0;
379}
380
Mark Browndee89c42008-11-18 22:11:38 +0000381static int tlv320aic23_pcm_prepare(struct snd_pcm_substream *substream,
382 struct snd_soc_dai *dai)
Arun KSc1f27192008-10-02 14:45:49 +0530383{
Kuninori Morimotoff06ac22018-01-29 04:12:48 +0000384 struct snd_soc_component *component = dai->component;
Arun KSc1f27192008-10-02 14:45:49 +0530385
386 /* set active */
Kuninori Morimotoff06ac22018-01-29 04:12:48 +0000387 snd_soc_component_write(component, TLV320AIC23_ACTIVE, 0x0001);
Arun KSc1f27192008-10-02 14:45:49 +0530388
389 return 0;
390}
391
Mark Browndee89c42008-11-18 22:11:38 +0000392static void tlv320aic23_shutdown(struct snd_pcm_substream *substream,
393 struct snd_soc_dai *dai)
Arun KSc1f27192008-10-02 14:45:49 +0530394{
Kuninori Morimotoff06ac22018-01-29 04:12:48 +0000395 struct snd_soc_component *component = dai->component;
396 struct aic23 *aic23 = snd_soc_component_get_drvdata(component);
Arun KSc1f27192008-10-02 14:45:49 +0530397
398 /* deactivate */
Kuninori Morimotoff06ac22018-01-29 04:12:48 +0000399 if (!snd_soc_component_is_active(component)) {
Arun KSc1f27192008-10-02 14:45:49 +0530400 udelay(50);
Kuninori Morimotoff06ac22018-01-29 04:12:48 +0000401 snd_soc_component_write(component, TLV320AIC23_ACTIVE, 0x0);
Arun KSc1f27192008-10-02 14:45:49 +0530402 }
Troy Kisky26df91c2008-11-05 18:53:28 +0000403 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
404 aic23->requested_dac = 0;
405 else
406 aic23->requested_adc = 0;
Arun KSc1f27192008-10-02 14:45:49 +0530407}
408
409static int tlv320aic23_mute(struct snd_soc_dai *dai, int mute)
410{
Kuninori Morimotoff06ac22018-01-29 04:12:48 +0000411 struct snd_soc_component *component = dai->component;
Arun KSc1f27192008-10-02 14:45:49 +0530412 u16 reg;
413
Kuninori Morimotoff06ac22018-01-29 04:12:48 +0000414 reg = snd_soc_component_read32(component, TLV320AIC23_DIGT);
Arun KSc1f27192008-10-02 14:45:49 +0530415 if (mute)
416 reg |= TLV320AIC23_DACM_MUTE;
417
418 else
419 reg &= ~TLV320AIC23_DACM_MUTE;
420
Kuninori Morimotoff06ac22018-01-29 04:12:48 +0000421 snd_soc_component_write(component, TLV320AIC23_DIGT, reg);
Arun KSc1f27192008-10-02 14:45:49 +0530422
423 return 0;
424}
425
426static int tlv320aic23_set_dai_fmt(struct snd_soc_dai *codec_dai,
427 unsigned int fmt)
428{
Kuninori Morimotoff06ac22018-01-29 04:12:48 +0000429 struct snd_soc_component *component = codec_dai->component;
Arun KSc1f27192008-10-02 14:45:49 +0530430 u16 iface_reg;
431
Kuninori Morimotoff06ac22018-01-29 04:12:48 +0000432 iface_reg = snd_soc_component_read32(component, TLV320AIC23_DIGT_FMT) & (~0x03);
Arun KSc1f27192008-10-02 14:45:49 +0530433
434 /* set master/slave audio interface */
435 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
436 case SND_SOC_DAIFMT_CBM_CFM:
437 iface_reg |= TLV320AIC23_MS_MASTER;
438 break;
439 case SND_SOC_DAIFMT_CBS_CFS:
Axel Linb01a3d62011-10-27 16:35:49 +0800440 iface_reg &= ~TLV320AIC23_MS_MASTER;
Arun KSc1f27192008-10-02 14:45:49 +0530441 break;
442 default:
443 return -EINVAL;
444
445 }
446
447 /* interface format */
448 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
449 case SND_SOC_DAIFMT_I2S:
450 iface_reg |= TLV320AIC23_FOR_I2S;
451 break;
Peter Ujfalusi894bf922009-04-09 12:34:40 +0300452 case SND_SOC_DAIFMT_DSP_A:
453 iface_reg |= TLV320AIC23_LRP_ON;
Gustavo A. R. Silvaa47043e2017-11-08 14:04:12 -0600454 /* fall through */
Jarkko Nikulabd258672008-12-22 10:21:36 +0200455 case SND_SOC_DAIFMT_DSP_B:
Arun KSc1f27192008-10-02 14:45:49 +0530456 iface_reg |= TLV320AIC23_FOR_DSP;
457 break;
458 case SND_SOC_DAIFMT_RIGHT_J:
459 break;
460 case SND_SOC_DAIFMT_LEFT_J:
461 iface_reg |= TLV320AIC23_FOR_LJUST;
462 break;
463 default:
464 return -EINVAL;
465
466 }
467
Kuninori Morimotoff06ac22018-01-29 04:12:48 +0000468 snd_soc_component_write(component, TLV320AIC23_DIGT_FMT, iface_reg);
Arun KSc1f27192008-10-02 14:45:49 +0530469
470 return 0;
471}
472
473static int tlv320aic23_set_dai_sysclk(struct snd_soc_dai *codec_dai,
474 int clk_id, unsigned int freq, int dir)
475{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000476 struct aic23 *aic23 = snd_soc_dai_get_drvdata(codec_dai);
Troy Kisky26df91c2008-11-05 18:53:28 +0000477 aic23->mclk = freq;
478 return 0;
Arun KSc1f27192008-10-02 14:45:49 +0530479}
480
Kuninori Morimotoff06ac22018-01-29 04:12:48 +0000481static int tlv320aic23_set_bias_level(struct snd_soc_component *component,
Arun KSc1f27192008-10-02 14:45:49 +0530482 enum snd_soc_bias_level level)
483{
Kuninori Morimotoff06ac22018-01-29 04:12:48 +0000484 u16 reg = snd_soc_component_read32(component, TLV320AIC23_PWR) & 0x17f;
Arun KSc1f27192008-10-02 14:45:49 +0530485
486 switch (level) {
487 case SND_SOC_BIAS_ON:
488 /* vref/mid, osc on, dac unmute */
Eric Bénard3d5a4512010-06-19 19:33:39 +0200489 reg &= ~(TLV320AIC23_DEVICE_PWR_OFF | TLV320AIC23_OSC_OFF | \
490 TLV320AIC23_DAC_OFF);
Kuninori Morimotoff06ac22018-01-29 04:12:48 +0000491 snd_soc_component_write(component, TLV320AIC23_PWR, reg);
Arun KSc1f27192008-10-02 14:45:49 +0530492 break;
493 case SND_SOC_BIAS_PREPARE:
494 break;
495 case SND_SOC_BIAS_STANDBY:
496 /* everything off except vref/vmid, */
Kuninori Morimotoff06ac22018-01-29 04:12:48 +0000497 snd_soc_component_write(component, TLV320AIC23_PWR,
Axel Linf9dfbf92011-10-13 15:06:43 +0800498 reg | TLV320AIC23_CLK_OFF);
Arun KSc1f27192008-10-02 14:45:49 +0530499 break;
500 case SND_SOC_BIAS_OFF:
501 /* everything off, dac mute, inactive */
Kuninori Morimotoff06ac22018-01-29 04:12:48 +0000502 snd_soc_component_write(component, TLV320AIC23_ACTIVE, 0x0);
503 snd_soc_component_write(component, TLV320AIC23_PWR, 0x1ff);
Arun KSc1f27192008-10-02 14:45:49 +0530504 break;
505 }
Arun KSc1f27192008-10-02 14:45:49 +0530506 return 0;
507}
508
509#define AIC23_RATES SNDRV_PCM_RATE_8000_96000
510#define AIC23_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
511 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)
512
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100513static const struct snd_soc_dai_ops tlv320aic23_dai_ops = {
Eric Miao6335d052009-03-03 09:41:00 +0800514 .prepare = tlv320aic23_pcm_prepare,
515 .hw_params = tlv320aic23_hw_params,
516 .shutdown = tlv320aic23_shutdown,
517 .digital_mute = tlv320aic23_mute,
518 .set_fmt = tlv320aic23_set_dai_fmt,
519 .set_sysclk = tlv320aic23_set_dai_sysclk,
520};
521
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000522static struct snd_soc_dai_driver tlv320aic23_dai = {
523 .name = "tlv320aic23-hifi",
Arun KSc1f27192008-10-02 14:45:49 +0530524 .playback = {
525 .stream_name = "Playback",
526 .channels_min = 2,
527 .channels_max = 2,
528 .rates = AIC23_RATES,
529 .formats = AIC23_FORMATS,},
530 .capture = {
531 .stream_name = "Capture",
532 .channels_min = 2,
533 .channels_max = 2,
534 .rates = AIC23_RATES,
535 .formats = AIC23_FORMATS,},
Eric Miao6335d052009-03-03 09:41:00 +0800536 .ops = &tlv320aic23_dai_ops,
Arun KSc1f27192008-10-02 14:45:49 +0530537};
Arun KSc1f27192008-10-02 14:45:49 +0530538
Kuninori Morimotoff06ac22018-01-29 04:12:48 +0000539static int tlv320aic23_resume(struct snd_soc_component *component)
Arun KSc1f27192008-10-02 14:45:49 +0530540{
Kuninori Morimotoff06ac22018-01-29 04:12:48 +0000541 struct aic23 *aic23 = snd_soc_component_get_drvdata(component);
Mark Brown4aa11d62013-09-24 19:26:08 +0100542 regcache_mark_dirty(aic23->regmap);
543 regcache_sync(aic23->regmap);
Arun KSc1f27192008-10-02 14:45:49 +0530544
545 return 0;
546}
547
Kuninori Morimotoff06ac22018-01-29 04:12:48 +0000548static int tlv320aic23_component_probe(struct snd_soc_component *component)
Arun KSc1f27192008-10-02 14:45:49 +0530549{
Arun KSc1f27192008-10-02 14:45:49 +0530550 /* Reset codec */
Kuninori Morimotoff06ac22018-01-29 04:12:48 +0000551 snd_soc_component_write(component, TLV320AIC23_RESET, 0);
Axel Linf9dfbf92011-10-13 15:06:43 +0800552
Kuninori Morimotoff06ac22018-01-29 04:12:48 +0000553 snd_soc_component_write(component, TLV320AIC23_DIGT, TLV320AIC23_DEEMP_44K);
Arun KSc1f27192008-10-02 14:45:49 +0530554
555 /* Unmute input */
Kuninori Morimotoff06ac22018-01-29 04:12:48 +0000556 snd_soc_component_update_bits(component, TLV320AIC23_LINVOL,
Axel Linf9dfbf92011-10-13 15:06:43 +0800557 TLV320AIC23_LIM_MUTED, TLV320AIC23_LRS_ENABLED);
Arun KSc1f27192008-10-02 14:45:49 +0530558
Kuninori Morimotoff06ac22018-01-29 04:12:48 +0000559 snd_soc_component_update_bits(component, TLV320AIC23_RINVOL,
Axel Linf9dfbf92011-10-13 15:06:43 +0800560 TLV320AIC23_LIM_MUTED, TLV320AIC23_LRS_ENABLED);
Arun KSc1f27192008-10-02 14:45:49 +0530561
Kuninori Morimotoff06ac22018-01-29 04:12:48 +0000562 snd_soc_component_update_bits(component, TLV320AIC23_ANLG,
Axel Linf9dfbf92011-10-13 15:06:43 +0800563 TLV320AIC23_BYPASS_ON | TLV320AIC23_MICM_MUTED,
564 0);
Arun KSc1f27192008-10-02 14:45:49 +0530565
566 /* Default output volume */
Kuninori Morimotoff06ac22018-01-29 04:12:48 +0000567 snd_soc_component_write(component, TLV320AIC23_LCHNVOL,
Axel Linf9dfbf92011-10-13 15:06:43 +0800568 TLV320AIC23_DEFAULT_OUT_VOL & TLV320AIC23_OUT_VOL_MASK);
Kuninori Morimotoff06ac22018-01-29 04:12:48 +0000569 snd_soc_component_write(component, TLV320AIC23_RCHNVOL,
Axel Linf9dfbf92011-10-13 15:06:43 +0800570 TLV320AIC23_DEFAULT_OUT_VOL & TLV320AIC23_OUT_VOL_MASK);
Arun KSc1f27192008-10-02 14:45:49 +0530571
Kuninori Morimotoff06ac22018-01-29 04:12:48 +0000572 snd_soc_component_write(component, TLV320AIC23_ACTIVE, 0x1);
Arun KSc1f27192008-10-02 14:45:49 +0530573
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000574 return 0;
Arun KSc1f27192008-10-02 14:45:49 +0530575}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000576
Kuninori Morimotoff06ac22018-01-29 04:12:48 +0000577static const struct snd_soc_component_driver soc_component_dev_tlv320aic23 = {
578 .probe = tlv320aic23_component_probe,
579 .resume = tlv320aic23_resume,
580 .set_bias_level = tlv320aic23_set_bias_level,
581 .controls = tlv320aic23_snd_controls,
582 .num_controls = ARRAY_SIZE(tlv320aic23_snd_controls),
583 .dapm_widgets = tlv320aic23_dapm_widgets,
584 .num_dapm_widgets = ARRAY_SIZE(tlv320aic23_dapm_widgets),
585 .dapm_routes = tlv320aic23_intercon,
586 .num_dapm_routes = ARRAY_SIZE(tlv320aic23_intercon),
587 .suspend_bias_off = 1,
588 .idle_bias_on = 1,
589 .use_pmdown_time = 1,
590 .endianness = 1,
591 .non_legacy_dai_naming = 1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000592};
Arun KSc1f27192008-10-02 14:45:49 +0530593
Max Filippovb3fc5722014-03-06 14:04:41 +0400594int tlv320aic23_probe(struct device *dev, struct regmap *regmap)
Arun KSc1f27192008-10-02 14:45:49 +0530595{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000596 struct aic23 *aic23;
Arun KSc1f27192008-10-02 14:45:49 +0530597
Max Filippovb3fc5722014-03-06 14:04:41 +0400598 if (IS_ERR(regmap))
599 return PTR_ERR(regmap);
Arun KSc1f27192008-10-02 14:45:49 +0530600
Max Filippovb3fc5722014-03-06 14:04:41 +0400601 aic23 = devm_kzalloc(dev, sizeof(struct aic23), GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000602 if (aic23 == NULL)
603 return -ENOMEM;
Arun KSc1f27192008-10-02 14:45:49 +0530604
Max Filippovb3fc5722014-03-06 14:04:41 +0400605 aic23->regmap = regmap;
Mark Brown4aa11d62013-09-24 19:26:08 +0100606
Max Filippovb3fc5722014-03-06 14:04:41 +0400607 dev_set_drvdata(dev, aic23);
Arun KSc1f27192008-10-02 14:45:49 +0530608
Kuninori Morimotoff06ac22018-01-29 04:12:48 +0000609 return devm_snd_soc_register_component(dev,
610 &soc_component_dev_tlv320aic23,
Max Filippovb3fc5722014-03-06 14:04:41 +0400611 &tlv320aic23_dai, 1);
Arun KSc1f27192008-10-02 14:45:49 +0530612}
Max Filippov40423282014-03-08 13:31:06 +0400613EXPORT_SYMBOL(tlv320aic23_probe);
Mark Brown64089b82008-12-08 19:17:58 +0000614
Arun KSc1f27192008-10-02 14:45:49 +0530615MODULE_DESCRIPTION("ASoC TLV320AIC23 codec driver");
616MODULE_AUTHOR("Arun KS <arunks@mistralsolutions.com>");
617MODULE_LICENSE("GPL");