1c1f27190SArun KS /* 2c1f27190SArun KS * ALSA SoC TLV320AIC23 codec driver 3c1f27190SArun KS * 4c1f27190SArun KS * Author: Arun KS, <arunks@mistralsolutions.com> 5c1f27190SArun KS * Copyright: (C) 2008 Mistral Solutions Pvt Ltd., 6c1f27190SArun KS * 7c1f27190SArun KS * Based on sound/soc/codecs/wm8731.c by Richard Purdie 8c1f27190SArun KS * 9c1f27190SArun KS * This program is free software; you can redistribute it and/or modify 10c1f27190SArun KS * it under the terms of the GNU General Public License version 2 as 11c1f27190SArun KS * published by the Free Software Foundation. 12c1f27190SArun KS * 13c1f27190SArun KS * Notes: 14c1f27190SArun KS * The AIC23 is a driver for a low power stereo audio 15c1f27190SArun KS * codec tlv320aic23 16c1f27190SArun KS * 17c1f27190SArun KS * The machine layer should disable unsupported inputs/outputs by 18c1f27190SArun KS * snd_soc_dapm_disable_pin(codec, "LHPOUT"), etc. 19c1f27190SArun KS */ 20c1f27190SArun KS 21c1f27190SArun KS #include <linux/module.h> 22c1f27190SArun KS #include <linux/moduleparam.h> 23c1f27190SArun KS #include <linux/init.h> 24c1f27190SArun KS #include <linux/delay.h> 25c1f27190SArun KS #include <linux/pm.h> 264aa11d67SMark Brown #include <linux/regmap.h> 275a0e3ad6STejun Heo #include <linux/slab.h> 28c1f27190SArun KS #include <sound/core.h> 29c1f27190SArun KS #include <sound/pcm.h> 30c1f27190SArun KS #include <sound/pcm_params.h> 31c1f27190SArun KS #include <sound/soc.h> 32c1f27190SArun KS #include <sound/tlv.h> 33c1f27190SArun KS #include <sound/initval.h> 34c1f27190SArun KS 35c1f27190SArun KS #include "tlv320aic23.h" 36c1f27190SArun KS 37c1f27190SArun KS /* 38c1f27190SArun KS * AIC23 register cache 39c1f27190SArun KS */ 404aa11d67SMark Brown static const struct reg_default tlv320aic23_reg[] = { 414aa11d67SMark Brown { 0, 0x0097 }, 424aa11d67SMark Brown { 1, 0x0097 }, 434aa11d67SMark Brown { 2, 0x00F9 }, 444aa11d67SMark Brown { 3, 0x00F9 }, 454aa11d67SMark Brown { 4, 0x001A }, 464aa11d67SMark Brown { 5, 0x0004 }, 474aa11d67SMark Brown { 6, 0x0007 }, 484aa11d67SMark Brown { 7, 0x0001 }, 494aa11d67SMark Brown { 8, 0x0020 }, 504aa11d67SMark Brown { 9, 0x0000 }, 514aa11d67SMark Brown }; 524aa11d67SMark Brown 53b3fc5725SMax Filippov const struct regmap_config tlv320aic23_regmap = { 544aa11d67SMark Brown .reg_bits = 7, 554aa11d67SMark Brown .val_bits = 9, 564aa11d67SMark Brown 574aa11d67SMark Brown .max_register = TLV320AIC23_RESET, 584aa11d67SMark Brown .reg_defaults = tlv320aic23_reg, 594aa11d67SMark Brown .num_reg_defaults = ARRAY_SIZE(tlv320aic23_reg), 604aa11d67SMark Brown .cache_type = REGCACHE_RBTREE, 61c1f27190SArun KS }; 62c1f27190SArun KS 63c1f27190SArun KS static const char *rec_src_text[] = { "Line", "Mic" }; 64c1f27190SArun KS static const char *deemph_text[] = {"None", "32Khz", "44.1Khz", "48Khz"}; 65c1f27190SArun KS 66806057ccSTakashi Iwai static SOC_ENUM_SINGLE_DECL(rec_src_enum, 67806057ccSTakashi Iwai TLV320AIC23_ANLG, 2, rec_src_text); 68c1f27190SArun KS 69c1f27190SArun KS static const struct snd_kcontrol_new tlv320aic23_rec_src_mux_controls = 70c1f27190SArun KS SOC_DAPM_ENUM("Input Select", rec_src_enum); 71c1f27190SArun KS 72806057ccSTakashi Iwai static SOC_ENUM_SINGLE_DECL(tlv320aic23_rec_src, 73806057ccSTakashi Iwai TLV320AIC23_ANLG, 2, rec_src_text); 74806057ccSTakashi Iwai static SOC_ENUM_SINGLE_DECL(tlv320aic23_deemph, 75806057ccSTakashi Iwai TLV320AIC23_DIGT, 1, deemph_text); 76c1f27190SArun KS 77c1f27190SArun KS static const DECLARE_TLV_DB_SCALE(out_gain_tlv, -12100, 100, 0); 78c1f27190SArun KS static const DECLARE_TLV_DB_SCALE(input_gain_tlv, -1725, 75, 0); 79df91ddf1SArun KS static const DECLARE_TLV_DB_SCALE(sidetone_vol_tlv, -1800, 300, 0); 80df91ddf1SArun KS 81df91ddf1SArun KS static int snd_soc_tlv320aic23_put_volsw(struct snd_kcontrol *kcontrol, 82df91ddf1SArun KS struct snd_ctl_elem_value *ucontrol) 83df91ddf1SArun KS { 84df91ddf1SArun KS struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 85df91ddf1SArun KS u16 val, reg; 86df91ddf1SArun KS 87df91ddf1SArun KS val = (ucontrol->value.integer.value[0] & 0x07); 88df91ddf1SArun KS 89df91ddf1SArun KS /* linear conversion to userspace 90df91ddf1SArun KS * 000 = -6db 91df91ddf1SArun KS * 001 = -9db 92df91ddf1SArun KS * 010 = -12db 93df91ddf1SArun KS * 011 = -18db (Min) 94df91ddf1SArun KS * 100 = 0db (Max) 95df91ddf1SArun KS */ 96df91ddf1SArun KS val = (val >= 4) ? 4 : (3 - val); 97df91ddf1SArun KS 98f9dfbf91SAxel Lin reg = snd_soc_read(codec, TLV320AIC23_ANLG) & (~0x1C0); 99f9dfbf91SAxel Lin snd_soc_write(codec, TLV320AIC23_ANLG, reg | (val << 6)); 100df91ddf1SArun KS 101df91ddf1SArun KS return 0; 102df91ddf1SArun KS } 103df91ddf1SArun KS 104df91ddf1SArun KS static int snd_soc_tlv320aic23_get_volsw(struct snd_kcontrol *kcontrol, 105df91ddf1SArun KS struct snd_ctl_elem_value *ucontrol) 106df91ddf1SArun KS { 107df91ddf1SArun KS struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 108df91ddf1SArun KS u16 val; 109df91ddf1SArun KS 110f9dfbf91SAxel Lin val = snd_soc_read(codec, TLV320AIC23_ANLG) & (0x1C0); 111df91ddf1SArun KS val = val >> 6; 112df91ddf1SArun KS val = (val >= 4) ? 4 : (3 - val); 113df91ddf1SArun KS ucontrol->value.integer.value[0] = val; 114df91ddf1SArun KS return 0; 115df91ddf1SArun KS 116df91ddf1SArun KS } 117df91ddf1SArun KS 118c1f27190SArun KS static const struct snd_kcontrol_new tlv320aic23_snd_controls[] = { 119c1f27190SArun KS SOC_DOUBLE_R_TLV("Digital Playback Volume", TLV320AIC23_LCHNVOL, 120c1f27190SArun KS TLV320AIC23_RCHNVOL, 0, 127, 0, out_gain_tlv), 121c1f27190SArun KS SOC_SINGLE("Digital Playback Switch", TLV320AIC23_DIGT, 3, 1, 1), 122c1f27190SArun KS SOC_DOUBLE_R("Line Input Switch", TLV320AIC23_LINVOL, 123c1f27190SArun KS TLV320AIC23_RINVOL, 7, 1, 0), 124c1f27190SArun KS SOC_DOUBLE_R_TLV("Line Input Volume", TLV320AIC23_LINVOL, 125c1f27190SArun KS TLV320AIC23_RINVOL, 0, 31, 0, input_gain_tlv), 126c1f27190SArun KS SOC_SINGLE("Mic Input Switch", TLV320AIC23_ANLG, 1, 1, 1), 127c1f27190SArun KS SOC_SINGLE("Mic Booster Switch", TLV320AIC23_ANLG, 0, 1, 0), 1280f9887d1SPeter Ujfalusi SOC_SINGLE_EXT_TLV("Sidetone Volume", TLV320AIC23_ANLG, 6, 4, 0, 1290f9887d1SPeter Ujfalusi snd_soc_tlv320aic23_get_volsw, 1300f9887d1SPeter Ujfalusi snd_soc_tlv320aic23_put_volsw, sidetone_vol_tlv), 131c1f27190SArun KS SOC_ENUM("Playback De-emphasis", tlv320aic23_deemph), 132c1f27190SArun KS }; 133c1f27190SArun KS 134c1f27190SArun KS /* PGA Mixer controls for Line and Mic switch */ 135c1f27190SArun KS static const struct snd_kcontrol_new tlv320aic23_output_mixer_controls[] = { 136c1f27190SArun KS SOC_DAPM_SINGLE("Line Bypass Switch", TLV320AIC23_ANLG, 3, 1, 0), 137c1f27190SArun KS SOC_DAPM_SINGLE("Mic Sidetone Switch", TLV320AIC23_ANLG, 5, 1, 0), 138c1f27190SArun KS SOC_DAPM_SINGLE("Playback Switch", TLV320AIC23_ANLG, 4, 1, 0), 139c1f27190SArun KS }; 140c1f27190SArun KS 141c1f27190SArun KS static const struct snd_soc_dapm_widget tlv320aic23_dapm_widgets[] = { 142c1f27190SArun KS SND_SOC_DAPM_DAC("DAC", "Playback", TLV320AIC23_PWR, 3, 1), 143c1f27190SArun KS SND_SOC_DAPM_ADC("ADC", "Capture", TLV320AIC23_PWR, 2, 1), 144c1f27190SArun KS SND_SOC_DAPM_MUX("Capture Source", SND_SOC_NOPM, 0, 0, 145c1f27190SArun KS &tlv320aic23_rec_src_mux_controls), 146c1f27190SArun KS SND_SOC_DAPM_MIXER("Output Mixer", TLV320AIC23_PWR, 4, 1, 147c1f27190SArun KS &tlv320aic23_output_mixer_controls[0], 148c1f27190SArun KS ARRAY_SIZE(tlv320aic23_output_mixer_controls)), 149c1f27190SArun KS SND_SOC_DAPM_PGA("Line Input", TLV320AIC23_PWR, 0, 1, NULL, 0), 150c1f27190SArun KS SND_SOC_DAPM_PGA("Mic Input", TLV320AIC23_PWR, 1, 1, NULL, 0), 151c1f27190SArun KS 152c1f27190SArun KS SND_SOC_DAPM_OUTPUT("LHPOUT"), 153c1f27190SArun KS SND_SOC_DAPM_OUTPUT("RHPOUT"), 154c1f27190SArun KS SND_SOC_DAPM_OUTPUT("LOUT"), 155c1f27190SArun KS SND_SOC_DAPM_OUTPUT("ROUT"), 156c1f27190SArun KS 157c1f27190SArun KS SND_SOC_DAPM_INPUT("LLINEIN"), 158c1f27190SArun KS SND_SOC_DAPM_INPUT("RLINEIN"), 159c1f27190SArun KS 160c1f27190SArun KS SND_SOC_DAPM_INPUT("MICIN"), 161c1f27190SArun KS }; 162c1f27190SArun KS 163a7dca707SLu Guanqun static const struct snd_soc_dapm_route tlv320aic23_intercon[] = { 164c1f27190SArun KS /* Output Mixer */ 165c1f27190SArun KS {"Output Mixer", "Line Bypass Switch", "Line Input"}, 166c1f27190SArun KS {"Output Mixer", "Playback Switch", "DAC"}, 167c1f27190SArun KS {"Output Mixer", "Mic Sidetone Switch", "Mic Input"}, 168c1f27190SArun KS 169c1f27190SArun KS /* Outputs */ 170c1f27190SArun KS {"RHPOUT", NULL, "Output Mixer"}, 171c1f27190SArun KS {"LHPOUT", NULL, "Output Mixer"}, 172c1f27190SArun KS {"LOUT", NULL, "Output Mixer"}, 173c1f27190SArun KS {"ROUT", NULL, "Output Mixer"}, 174c1f27190SArun KS 175c1f27190SArun KS /* Inputs */ 176c1f27190SArun KS {"Line Input", "NULL", "LLINEIN"}, 177c1f27190SArun KS {"Line Input", "NULL", "RLINEIN"}, 178c1f27190SArun KS 179c1f27190SArun KS {"Mic Input", "NULL", "MICIN"}, 180c1f27190SArun KS 181c1f27190SArun KS /* input mux */ 182c1f27190SArun KS {"Capture Source", "Line", "Line Input"}, 183c1f27190SArun KS {"Capture Source", "Mic", "Mic Input"}, 184c1f27190SArun KS {"ADC", NULL, "Capture Source"}, 185c1f27190SArun KS 186c1f27190SArun KS }; 187c1f27190SArun KS 18826df91c3STroy Kisky /* AIC23 driver data */ 18926df91c3STroy Kisky struct aic23 { 1904aa11d67SMark Brown struct regmap *regmap; 19126df91c3STroy Kisky int mclk; 19226df91c3STroy Kisky int requested_adc; 19326df91c3STroy Kisky int requested_dac; 194c1f27190SArun KS }; 195c1f27190SArun KS 19626df91c3STroy Kisky /* 19726df91c3STroy Kisky * Common Crystals used 19826df91c3STroy Kisky * 11.2896 Mhz /128 = *88.2k /192 = 58.8k 19926df91c3STroy Kisky * 12.0000 Mhz /125 = *96k /136 = 88.235K 20026df91c3STroy Kisky * 12.2880 Mhz /128 = *96k /192 = 64k 20126df91c3STroy Kisky * 16.9344 Mhz /128 = 132.3k /192 = *88.2k 20226df91c3STroy Kisky * 18.4320 Mhz /128 = 144k /192 = *96k 20326df91c3STroy Kisky */ 20426df91c3STroy Kisky 20526df91c3STroy Kisky /* 20626df91c3STroy Kisky * Normal BOSR 0-256/2 = 128, 1-384/2 = 192 20726df91c3STroy Kisky * USB BOSR 0-250/2 = 125, 1-272/2 = 136 20826df91c3STroy Kisky */ 20926df91c3STroy Kisky static const int bosr_usb_divisor_table[] = { 21026df91c3STroy Kisky 128, 125, 192, 136 21126df91c3STroy Kisky }; 21226df91c3STroy Kisky #define LOWER_GROUP ((1<<0) | (1<<1) | (1<<2) | (1<<3) | (1<<6) | (1<<7)) 21326df91c3STroy Kisky #define UPPER_GROUP ((1<<8) | (1<<9) | (1<<10) | (1<<11) | (1<<15)) 21426df91c3STroy Kisky static const unsigned short sr_valid_mask[] = { 21526df91c3STroy Kisky LOWER_GROUP|UPPER_GROUP, /* Normal, bosr - 0*/ 21626df91c3STroy Kisky LOWER_GROUP, /* Usb, bosr - 0*/ 217bab02124STroy Kisky LOWER_GROUP|UPPER_GROUP, /* Normal, bosr - 1*/ 21826df91c3STroy Kisky UPPER_GROUP, /* Usb, bosr - 1*/ 21926df91c3STroy Kisky }; 22026df91c3STroy Kisky /* 22126df91c3STroy Kisky * Every divisor is a factor of 11*12 22226df91c3STroy Kisky */ 22326df91c3STroy Kisky #define SR_MULT (11*12) 224ccff4b15STroy Kisky #define A(x) (SR_MULT/x) 22526df91c3STroy Kisky static const unsigned char sr_adc_mult_table[] = { 226ccff4b15STroy Kisky A(2), A(2), A(12), A(12), 0, 0, A(3), A(1), 227ccff4b15STroy Kisky A(2), A(2), A(11), A(11), 0, 0, 0, A(1) 22826df91c3STroy Kisky }; 22926df91c3STroy Kisky static const unsigned char sr_dac_mult_table[] = { 230ccff4b15STroy Kisky A(2), A(12), A(2), A(12), 0, 0, A(3), A(1), 231ccff4b15STroy Kisky A(2), A(11), A(2), A(11), 0, 0, 0, A(1) 23226df91c3STroy Kisky }; 23326df91c3STroy Kisky 23426df91c3STroy Kisky static unsigned get_score(int adc, int adc_l, int adc_h, int need_adc, 23526df91c3STroy Kisky int dac, int dac_l, int dac_h, int need_dac) 23626df91c3STroy Kisky { 23726df91c3STroy Kisky if ((adc >= adc_l) && (adc <= adc_h) && 23826df91c3STroy Kisky (dac >= dac_l) && (dac <= dac_h)) { 23926df91c3STroy Kisky int diff_adc = need_adc - adc; 24026df91c3STroy Kisky int diff_dac = need_dac - dac; 24126df91c3STroy Kisky return abs(diff_adc) + abs(diff_dac); 24226df91c3STroy Kisky } 24326df91c3STroy Kisky return UINT_MAX; 24426df91c3STroy Kisky } 24526df91c3STroy Kisky 24626df91c3STroy Kisky static int find_rate(int mclk, u32 need_adc, u32 need_dac) 24726df91c3STroy Kisky { 24826df91c3STroy Kisky int i, j; 24926df91c3STroy Kisky int best_i = -1; 25026df91c3STroy Kisky int best_j = -1; 25126df91c3STroy Kisky int best_div = 0; 25226df91c3STroy Kisky unsigned best_score = UINT_MAX; 25326df91c3STroy Kisky int adc_l, adc_h, dac_l, dac_h; 25426df91c3STroy Kisky 25526df91c3STroy Kisky need_adc *= SR_MULT; 25626df91c3STroy Kisky need_dac *= SR_MULT; 25726df91c3STroy Kisky /* 25826df91c3STroy Kisky * rates given are +/- 1/32 25926df91c3STroy Kisky */ 26026df91c3STroy Kisky adc_l = need_adc - (need_adc >> 5); 26126df91c3STroy Kisky adc_h = need_adc + (need_adc >> 5); 26226df91c3STroy Kisky dac_l = need_dac - (need_dac >> 5); 26326df91c3STroy Kisky dac_h = need_dac + (need_dac >> 5); 2648d702f23SMark Brown for (i = 0; i < ARRAY_SIZE(bosr_usb_divisor_table); i++) { 26526df91c3STroy Kisky int base = mclk / bosr_usb_divisor_table[i]; 26626df91c3STroy Kisky int mask = sr_valid_mask[i]; 2678d702f23SMark Brown for (j = 0; j < ARRAY_SIZE(sr_adc_mult_table); 2688d702f23SMark Brown j++, mask >>= 1) { 26926df91c3STroy Kisky int adc; 27026df91c3STroy Kisky int dac; 27126df91c3STroy Kisky int score; 27226df91c3STroy Kisky if ((mask & 1) == 0) 27326df91c3STroy Kisky continue; 27426df91c3STroy Kisky adc = base * sr_adc_mult_table[j]; 27526df91c3STroy Kisky dac = base * sr_dac_mult_table[j]; 27626df91c3STroy Kisky score = get_score(adc, adc_l, adc_h, need_adc, 27726df91c3STroy Kisky dac, dac_l, dac_h, need_dac); 27826df91c3STroy Kisky if (best_score > score) { 27926df91c3STroy Kisky best_score = score; 28026df91c3STroy Kisky best_i = i; 28126df91c3STroy Kisky best_j = j; 28226df91c3STroy Kisky best_div = 0; 28326df91c3STroy Kisky } 28426df91c3STroy Kisky score = get_score((adc >> 1), adc_l, adc_h, need_adc, 28526df91c3STroy Kisky (dac >> 1), dac_l, dac_h, need_dac); 28626df91c3STroy Kisky /* prefer to have a /2 */ 28726df91c3STroy Kisky if ((score != UINT_MAX) && (best_score >= score)) { 28826df91c3STroy Kisky best_score = score; 28926df91c3STroy Kisky best_i = i; 29026df91c3STroy Kisky best_j = j; 29126df91c3STroy Kisky best_div = 1; 29226df91c3STroy Kisky } 29326df91c3STroy Kisky } 29426df91c3STroy Kisky } 29526df91c3STroy Kisky return (best_j << 2) | best_i | (best_div << TLV320AIC23_CLKIN_SHIFT); 29626df91c3STroy Kisky } 29726df91c3STroy Kisky 2988d702f23SMark Brown #ifdef DEBUG 29926df91c3STroy Kisky static void get_current_sample_rates(struct snd_soc_codec *codec, int mclk, 30026df91c3STroy Kisky u32 *sample_rate_adc, u32 *sample_rate_dac) 30126df91c3STroy Kisky { 302f9dfbf91SAxel Lin int src = snd_soc_read(codec, TLV320AIC23_SRATE); 30326df91c3STroy Kisky int sr = (src >> 2) & 0x0f; 30426df91c3STroy Kisky int val = (mclk / bosr_usb_divisor_table[src & 3]); 30526df91c3STroy Kisky int adc = (val * sr_adc_mult_table[sr]) / SR_MULT; 30626df91c3STroy Kisky int dac = (val * sr_dac_mult_table[sr]) / SR_MULT; 30726df91c3STroy Kisky if (src & TLV320AIC23_CLKIN_HALF) { 30826df91c3STroy Kisky adc >>= 1; 30926df91c3STroy Kisky dac >>= 1; 31026df91c3STroy Kisky } 31126df91c3STroy Kisky *sample_rate_adc = adc; 31226df91c3STroy Kisky *sample_rate_dac = dac; 31326df91c3STroy Kisky } 3148d702f23SMark Brown #endif 31526df91c3STroy Kisky 31626df91c3STroy Kisky static int set_sample_rate_control(struct snd_soc_codec *codec, int mclk, 31726df91c3STroy Kisky u32 sample_rate_adc, u32 sample_rate_dac) 31826df91c3STroy Kisky { 31926df91c3STroy Kisky /* Search for the right sample rate */ 32026df91c3STroy Kisky int data = find_rate(mclk, sample_rate_adc, sample_rate_dac); 32126df91c3STroy Kisky if (data < 0) { 32226df91c3STroy Kisky printk(KERN_ERR "%s:Invalid rate %u,%u requested\n", 32326df91c3STroy Kisky __func__, sample_rate_adc, sample_rate_dac); 32426df91c3STroy Kisky return -EINVAL; 32526df91c3STroy Kisky } 326f9dfbf91SAxel Lin snd_soc_write(codec, TLV320AIC23_SRATE, data); 3278d702f23SMark Brown #ifdef DEBUG 3288d702f23SMark Brown { 3298d702f23SMark Brown u32 adc, dac; 33026df91c3STroy Kisky get_current_sample_rates(codec, mclk, &adc, &dac); 33126df91c3STroy Kisky printk(KERN_DEBUG "actual samplerate = %u,%u reg=%x\n", 33226df91c3STroy Kisky adc, dac, data); 33326df91c3STroy Kisky } 3348d702f23SMark Brown #endif 33526df91c3STroy Kisky return 0; 33626df91c3STroy Kisky } 33726df91c3STroy Kisky 338c1f27190SArun KS static int tlv320aic23_hw_params(struct snd_pcm_substream *substream, 339dee89c4dSMark Brown struct snd_pcm_hw_params *params, 340dee89c4dSMark Brown struct snd_soc_dai *dai) 341c1f27190SArun KS { 342e6968a17SMark Brown struct snd_soc_codec *codec = dai->codec; 34326df91c3STroy Kisky u16 iface_reg; 34426df91c3STroy Kisky int ret; 345f0fba2adSLiam Girdwood struct aic23 *aic23 = snd_soc_codec_get_drvdata(codec); 34626df91c3STroy Kisky u32 sample_rate_adc = aic23->requested_adc; 34726df91c3STroy Kisky u32 sample_rate_dac = aic23->requested_dac; 34826df91c3STroy Kisky u32 sample_rate = params_rate(params); 34926df91c3STroy Kisky 35026df91c3STroy Kisky if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 35126df91c3STroy Kisky aic23->requested_dac = sample_rate_dac = sample_rate; 35226df91c3STroy Kisky if (!sample_rate_adc) 35326df91c3STroy Kisky sample_rate_adc = sample_rate; 35426df91c3STroy Kisky } else { 35526df91c3STroy Kisky aic23->requested_adc = sample_rate_adc = sample_rate; 35626df91c3STroy Kisky if (!sample_rate_dac) 35726df91c3STroy Kisky sample_rate_dac = sample_rate; 35826df91c3STroy Kisky } 35926df91c3STroy Kisky ret = set_sample_rate_control(codec, aic23->mclk, sample_rate_adc, 36026df91c3STroy Kisky sample_rate_dac); 36126df91c3STroy Kisky if (ret < 0) 36226df91c3STroy Kisky return ret; 363c1f27190SArun KS 364f9dfbf91SAxel Lin iface_reg = snd_soc_read(codec, TLV320AIC23_DIGT_FMT) & ~(0x03 << 2); 365f9dfbf91SAxel Lin 366c1f27190SArun KS switch (params_format(params)) { 367c1f27190SArun KS case SNDRV_PCM_FORMAT_S16_LE: 368c1f27190SArun KS break; 369c1f27190SArun KS case SNDRV_PCM_FORMAT_S20_3LE: 370c1f27190SArun KS iface_reg |= (0x01 << 2); 371c1f27190SArun KS break; 372c1f27190SArun KS case SNDRV_PCM_FORMAT_S24_LE: 373c1f27190SArun KS iface_reg |= (0x02 << 2); 374c1f27190SArun KS break; 375c1f27190SArun KS case SNDRV_PCM_FORMAT_S32_LE: 376c1f27190SArun KS iface_reg |= (0x03 << 2); 377c1f27190SArun KS break; 378c1f27190SArun KS } 379f9dfbf91SAxel Lin snd_soc_write(codec, TLV320AIC23_DIGT_FMT, iface_reg); 380c1f27190SArun KS 381c1f27190SArun KS return 0; 382c1f27190SArun KS } 383c1f27190SArun KS 384dee89c4dSMark Brown static int tlv320aic23_pcm_prepare(struct snd_pcm_substream *substream, 385dee89c4dSMark Brown struct snd_soc_dai *dai) 386c1f27190SArun KS { 387e6968a17SMark Brown struct snd_soc_codec *codec = dai->codec; 388c1f27190SArun KS 389c1f27190SArun KS /* set active */ 390f9dfbf91SAxel Lin snd_soc_write(codec, TLV320AIC23_ACTIVE, 0x0001); 391c1f27190SArun KS 392c1f27190SArun KS return 0; 393c1f27190SArun KS } 394c1f27190SArun KS 395dee89c4dSMark Brown static void tlv320aic23_shutdown(struct snd_pcm_substream *substream, 396dee89c4dSMark Brown struct snd_soc_dai *dai) 397c1f27190SArun KS { 398e6968a17SMark Brown struct snd_soc_codec *codec = dai->codec; 399f0fba2adSLiam Girdwood struct aic23 *aic23 = snd_soc_codec_get_drvdata(codec); 400c1f27190SArun KS 401c1f27190SArun KS /* deactivate */ 402c1f27190SArun KS if (!codec->active) { 403c1f27190SArun KS udelay(50); 404f9dfbf91SAxel Lin snd_soc_write(codec, TLV320AIC23_ACTIVE, 0x0); 405c1f27190SArun KS } 40626df91c3STroy Kisky if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 40726df91c3STroy Kisky aic23->requested_dac = 0; 40826df91c3STroy Kisky else 40926df91c3STroy Kisky aic23->requested_adc = 0; 410c1f27190SArun KS } 411c1f27190SArun KS 412c1f27190SArun KS static int tlv320aic23_mute(struct snd_soc_dai *dai, int mute) 413c1f27190SArun KS { 414c1f27190SArun KS struct snd_soc_codec *codec = dai->codec; 415c1f27190SArun KS u16 reg; 416c1f27190SArun KS 417f9dfbf91SAxel Lin reg = snd_soc_read(codec, TLV320AIC23_DIGT); 418c1f27190SArun KS if (mute) 419c1f27190SArun KS reg |= TLV320AIC23_DACM_MUTE; 420c1f27190SArun KS 421c1f27190SArun KS else 422c1f27190SArun KS reg &= ~TLV320AIC23_DACM_MUTE; 423c1f27190SArun KS 424f9dfbf91SAxel Lin snd_soc_write(codec, TLV320AIC23_DIGT, reg); 425c1f27190SArun KS 426c1f27190SArun KS return 0; 427c1f27190SArun KS } 428c1f27190SArun KS 429c1f27190SArun KS static int tlv320aic23_set_dai_fmt(struct snd_soc_dai *codec_dai, 430c1f27190SArun KS unsigned int fmt) 431c1f27190SArun KS { 432c1f27190SArun KS struct snd_soc_codec *codec = codec_dai->codec; 433c1f27190SArun KS u16 iface_reg; 434c1f27190SArun KS 435f9dfbf91SAxel Lin iface_reg = snd_soc_read(codec, TLV320AIC23_DIGT_FMT) & (~0x03); 436c1f27190SArun KS 437c1f27190SArun KS /* set master/slave audio interface */ 438c1f27190SArun KS switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { 439c1f27190SArun KS case SND_SOC_DAIFMT_CBM_CFM: 440c1f27190SArun KS iface_reg |= TLV320AIC23_MS_MASTER; 441c1f27190SArun KS break; 442c1f27190SArun KS case SND_SOC_DAIFMT_CBS_CFS: 443b01a3d69SAxel Lin iface_reg &= ~TLV320AIC23_MS_MASTER; 444c1f27190SArun KS break; 445c1f27190SArun KS default: 446c1f27190SArun KS return -EINVAL; 447c1f27190SArun KS 448c1f27190SArun KS } 449c1f27190SArun KS 450c1f27190SArun KS /* interface format */ 451c1f27190SArun KS switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 452c1f27190SArun KS case SND_SOC_DAIFMT_I2S: 453c1f27190SArun KS iface_reg |= TLV320AIC23_FOR_I2S; 454c1f27190SArun KS break; 455894bf92fSPeter Ujfalusi case SND_SOC_DAIFMT_DSP_A: 456894bf92fSPeter Ujfalusi iface_reg |= TLV320AIC23_LRP_ON; 457bd25867aSJarkko Nikula case SND_SOC_DAIFMT_DSP_B: 458c1f27190SArun KS iface_reg |= TLV320AIC23_FOR_DSP; 459c1f27190SArun KS break; 460c1f27190SArun KS case SND_SOC_DAIFMT_RIGHT_J: 461c1f27190SArun KS break; 462c1f27190SArun KS case SND_SOC_DAIFMT_LEFT_J: 463c1f27190SArun KS iface_reg |= TLV320AIC23_FOR_LJUST; 464c1f27190SArun KS break; 465c1f27190SArun KS default: 466c1f27190SArun KS return -EINVAL; 467c1f27190SArun KS 468c1f27190SArun KS } 469c1f27190SArun KS 470f9dfbf91SAxel Lin snd_soc_write(codec, TLV320AIC23_DIGT_FMT, iface_reg); 471c1f27190SArun KS 472c1f27190SArun KS return 0; 473c1f27190SArun KS } 474c1f27190SArun KS 475c1f27190SArun KS static int tlv320aic23_set_dai_sysclk(struct snd_soc_dai *codec_dai, 476c1f27190SArun KS int clk_id, unsigned int freq, int dir) 477c1f27190SArun KS { 478f0fba2adSLiam Girdwood struct aic23 *aic23 = snd_soc_dai_get_drvdata(codec_dai); 47926df91c3STroy Kisky aic23->mclk = freq; 480c1f27190SArun KS return 0; 481c1f27190SArun KS } 482c1f27190SArun KS 483c1f27190SArun KS static int tlv320aic23_set_bias_level(struct snd_soc_codec *codec, 484c1f27190SArun KS enum snd_soc_bias_level level) 485c1f27190SArun KS { 486e875c1e3SEric Bénard u16 reg = snd_soc_read(codec, TLV320AIC23_PWR) & 0x17f; 487c1f27190SArun KS 488c1f27190SArun KS switch (level) { 489c1f27190SArun KS case SND_SOC_BIAS_ON: 490c1f27190SArun KS /* vref/mid, osc on, dac unmute */ 4913d5a4516SEric Bénard reg &= ~(TLV320AIC23_DEVICE_PWR_OFF | TLV320AIC23_OSC_OFF | \ 4923d5a4516SEric Bénard TLV320AIC23_DAC_OFF); 493f9dfbf91SAxel Lin snd_soc_write(codec, TLV320AIC23_PWR, reg); 494c1f27190SArun KS break; 495c1f27190SArun KS case SND_SOC_BIAS_PREPARE: 496c1f27190SArun KS break; 497c1f27190SArun KS case SND_SOC_BIAS_STANDBY: 498c1f27190SArun KS /* everything off except vref/vmid, */ 499f9dfbf91SAxel Lin snd_soc_write(codec, TLV320AIC23_PWR, 500f9dfbf91SAxel Lin reg | TLV320AIC23_CLK_OFF); 501c1f27190SArun KS break; 502c1f27190SArun KS case SND_SOC_BIAS_OFF: 503c1f27190SArun KS /* everything off, dac mute, inactive */ 504f9dfbf91SAxel Lin snd_soc_write(codec, TLV320AIC23_ACTIVE, 0x0); 505e875c1e3SEric Bénard snd_soc_write(codec, TLV320AIC23_PWR, 0x1ff); 506c1f27190SArun KS break; 507c1f27190SArun KS } 508ce6120ccSLiam Girdwood codec->dapm.bias_level = level; 509c1f27190SArun KS return 0; 510c1f27190SArun KS } 511c1f27190SArun KS 512c1f27190SArun KS #define AIC23_RATES SNDRV_PCM_RATE_8000_96000 513c1f27190SArun KS #define AIC23_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \ 514c1f27190SArun KS SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE) 515c1f27190SArun KS 51685e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops tlv320aic23_dai_ops = { 5176335d055SEric Miao .prepare = tlv320aic23_pcm_prepare, 5186335d055SEric Miao .hw_params = tlv320aic23_hw_params, 5196335d055SEric Miao .shutdown = tlv320aic23_shutdown, 5206335d055SEric Miao .digital_mute = tlv320aic23_mute, 5216335d055SEric Miao .set_fmt = tlv320aic23_set_dai_fmt, 5226335d055SEric Miao .set_sysclk = tlv320aic23_set_dai_sysclk, 5236335d055SEric Miao }; 5246335d055SEric Miao 525f0fba2adSLiam Girdwood static struct snd_soc_dai_driver tlv320aic23_dai = { 526f0fba2adSLiam Girdwood .name = "tlv320aic23-hifi", 527c1f27190SArun KS .playback = { 528c1f27190SArun KS .stream_name = "Playback", 529c1f27190SArun KS .channels_min = 2, 530c1f27190SArun KS .channels_max = 2, 531c1f27190SArun KS .rates = AIC23_RATES, 532c1f27190SArun KS .formats = AIC23_FORMATS,}, 533c1f27190SArun KS .capture = { 534c1f27190SArun KS .stream_name = "Capture", 535c1f27190SArun KS .channels_min = 2, 536c1f27190SArun KS .channels_max = 2, 537c1f27190SArun KS .rates = AIC23_RATES, 538c1f27190SArun KS .formats = AIC23_FORMATS,}, 5396335d055SEric Miao .ops = &tlv320aic23_dai_ops, 540c1f27190SArun KS }; 541c1f27190SArun KS 54284b315eeSLars-Peter Clausen static int tlv320aic23_suspend(struct snd_soc_codec *codec) 543c1f27190SArun KS { 544c1f27190SArun KS tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_OFF); 545c1f27190SArun KS 546c1f27190SArun KS return 0; 547c1f27190SArun KS } 548c1f27190SArun KS 549f0fba2adSLiam Girdwood static int tlv320aic23_resume(struct snd_soc_codec *codec) 550c1f27190SArun KS { 5514aa11d67SMark Brown struct aic23 *aic23 = snd_soc_codec_get_drvdata(codec); 5524aa11d67SMark Brown regcache_mark_dirty(aic23->regmap); 5534aa11d67SMark Brown regcache_sync(aic23->regmap); 554c1f27190SArun KS tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_STANDBY); 555c1f27190SArun KS 556c1f27190SArun KS return 0; 557c1f27190SArun KS } 558c1f27190SArun KS 559b3fc5725SMax Filippov static int tlv320aic23_codec_probe(struct snd_soc_codec *codec) 560c1f27190SArun KS { 561f9dfbf91SAxel Lin int ret; 562c1f27190SArun KS 5634aa11d67SMark Brown ret = snd_soc_codec_set_cache_io(codec, 7, 9, SND_SOC_REGMAP); 564f9dfbf91SAxel Lin if (ret < 0) { 565f9dfbf91SAxel Lin dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret); 566f9dfbf91SAxel Lin return ret; 567f9dfbf91SAxel Lin } 568c1f27190SArun KS 569c1f27190SArun KS /* Reset codec */ 570f9dfbf91SAxel Lin snd_soc_write(codec, TLV320AIC23_RESET, 0); 571f9dfbf91SAxel Lin 572c1f27190SArun KS /* power on device */ 573c1f27190SArun KS tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_STANDBY); 574c1f27190SArun KS 575f9dfbf91SAxel Lin snd_soc_write(codec, TLV320AIC23_DIGT, TLV320AIC23_DEEMP_44K); 576c1f27190SArun KS 577c1f27190SArun KS /* Unmute input */ 578f9dfbf91SAxel Lin snd_soc_update_bits(codec, TLV320AIC23_LINVOL, 579f9dfbf91SAxel Lin TLV320AIC23_LIM_MUTED, TLV320AIC23_LRS_ENABLED); 580c1f27190SArun KS 581f9dfbf91SAxel Lin snd_soc_update_bits(codec, TLV320AIC23_RINVOL, 582f9dfbf91SAxel Lin TLV320AIC23_LIM_MUTED, TLV320AIC23_LRS_ENABLED); 583c1f27190SArun KS 584f9dfbf91SAxel Lin snd_soc_update_bits(codec, TLV320AIC23_ANLG, 585f9dfbf91SAxel Lin TLV320AIC23_BYPASS_ON | TLV320AIC23_MICM_MUTED, 586f9dfbf91SAxel Lin 0); 587c1f27190SArun KS 588c1f27190SArun KS /* Default output volume */ 589f9dfbf91SAxel Lin snd_soc_write(codec, TLV320AIC23_LCHNVOL, 590f9dfbf91SAxel Lin TLV320AIC23_DEFAULT_OUT_VOL & TLV320AIC23_OUT_VOL_MASK); 591f9dfbf91SAxel Lin snd_soc_write(codec, TLV320AIC23_RCHNVOL, 592f9dfbf91SAxel Lin TLV320AIC23_DEFAULT_OUT_VOL & TLV320AIC23_OUT_VOL_MASK); 593c1f27190SArun KS 594f9dfbf91SAxel Lin snd_soc_write(codec, TLV320AIC23_ACTIVE, 0x1); 595c1f27190SArun KS 596f0fba2adSLiam Girdwood return 0; 597c1f27190SArun KS } 598f0fba2adSLiam Girdwood 599f0fba2adSLiam Girdwood static int tlv320aic23_remove(struct snd_soc_codec *codec) 600f0fba2adSLiam Girdwood { 601f0fba2adSLiam Girdwood tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_OFF); 602f0fba2adSLiam Girdwood return 0; 603f0fba2adSLiam Girdwood } 604f0fba2adSLiam Girdwood 605f0fba2adSLiam Girdwood static struct snd_soc_codec_driver soc_codec_dev_tlv320aic23 = { 606b3fc5725SMax Filippov .probe = tlv320aic23_codec_probe, 607f0fba2adSLiam Girdwood .remove = tlv320aic23_remove, 608f0fba2adSLiam Girdwood .suspend = tlv320aic23_suspend, 609f0fba2adSLiam Girdwood .resume = tlv320aic23_resume, 610f0fba2adSLiam Girdwood .set_bias_level = tlv320aic23_set_bias_level, 611b07c443fSMark Brown .controls = tlv320aic23_snd_controls, 612b07c443fSMark Brown .num_controls = ARRAY_SIZE(tlv320aic23_snd_controls), 613a7dca707SLu Guanqun .dapm_widgets = tlv320aic23_dapm_widgets, 614a7dca707SLu Guanqun .num_dapm_widgets = ARRAY_SIZE(tlv320aic23_dapm_widgets), 615a7dca707SLu Guanqun .dapm_routes = tlv320aic23_intercon, 616a7dca707SLu Guanqun .num_dapm_routes = ARRAY_SIZE(tlv320aic23_intercon), 617f0fba2adSLiam Girdwood }; 618c1f27190SArun KS 619b3fc5725SMax Filippov int tlv320aic23_probe(struct device *dev, struct regmap *regmap) 620c1f27190SArun KS { 621f0fba2adSLiam Girdwood struct aic23 *aic23; 622c1f27190SArun KS 623b3fc5725SMax Filippov if (IS_ERR(regmap)) 624b3fc5725SMax Filippov return PTR_ERR(regmap); 625c1f27190SArun KS 626b3fc5725SMax Filippov aic23 = devm_kzalloc(dev, sizeof(struct aic23), GFP_KERNEL); 627f0fba2adSLiam Girdwood if (aic23 == NULL) 628f0fba2adSLiam Girdwood return -ENOMEM; 629c1f27190SArun KS 630b3fc5725SMax Filippov aic23->regmap = regmap; 6314aa11d67SMark Brown 632b3fc5725SMax Filippov dev_set_drvdata(dev, aic23); 633c1f27190SArun KS 634b3fc5725SMax Filippov return snd_soc_register_codec(dev, &soc_codec_dev_tlv320aic23, 635b3fc5725SMax Filippov &tlv320aic23_dai, 1); 636c1f27190SArun KS } 63764089b84SMark Brown 638c1f27190SArun KS MODULE_DESCRIPTION("ASoC TLV320AIC23 codec driver"); 639c1f27190SArun KS MODULE_AUTHOR("Arun KS <arunks@mistralsolutions.com>"); 640c1f27190SArun KS MODULE_LICENSE("GPL"); 641