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> 26c1f27190SArun KS #include <linux/i2c.h> 27c1f27190SArun KS #include <linux/platform_device.h> 285a0e3ad6STejun Heo #include <linux/slab.h> 29c1f27190SArun KS #include <sound/core.h> 30c1f27190SArun KS #include <sound/pcm.h> 31c1f27190SArun KS #include <sound/pcm_params.h> 32c1f27190SArun KS #include <sound/soc.h> 33c1f27190SArun KS #include <sound/tlv.h> 34c1f27190SArun KS #include <sound/initval.h> 35c1f27190SArun KS 36c1f27190SArun KS #include "tlv320aic23.h" 37c1f27190SArun KS 38c1f27190SArun KS #define AIC23_VERSION "0.1" 39c1f27190SArun KS 40c1f27190SArun KS /* 41c1f27190SArun KS * AIC23 register cache 42c1f27190SArun KS */ 43c1f27190SArun KS static const u16 tlv320aic23_reg[] = { 44c1f27190SArun KS 0x0097, 0x0097, 0x00F9, 0x00F9, /* 0 */ 45c1f27190SArun KS 0x001A, 0x0004, 0x0007, 0x0001, /* 4 */ 46c1f27190SArun KS 0x0020, 0x0000, 0x0000, 0x0000, /* 8 */ 47c1f27190SArun KS 0x0000, 0x0000, 0x0000, 0x0000, /* 12 */ 48c1f27190SArun KS }; 49c1f27190SArun KS 50c1f27190SArun KS static const char *rec_src_text[] = { "Line", "Mic" }; 51c1f27190SArun KS static const char *deemph_text[] = {"None", "32Khz", "44.1Khz", "48Khz"}; 52c1f27190SArun KS 53c1f27190SArun KS static const struct soc_enum rec_src_enum = 54c1f27190SArun KS SOC_ENUM_SINGLE(TLV320AIC23_ANLG, 2, 2, rec_src_text); 55c1f27190SArun KS 56c1f27190SArun KS static const struct snd_kcontrol_new tlv320aic23_rec_src_mux_controls = 57c1f27190SArun KS SOC_DAPM_ENUM("Input Select", rec_src_enum); 58c1f27190SArun KS 59c1f27190SArun KS static const struct soc_enum tlv320aic23_rec_src = 60c1f27190SArun KS SOC_ENUM_SINGLE(TLV320AIC23_ANLG, 2, 2, rec_src_text); 61c1f27190SArun KS static const struct soc_enum tlv320aic23_deemph = 62c1f27190SArun KS SOC_ENUM_SINGLE(TLV320AIC23_DIGT, 1, 4, deemph_text); 63c1f27190SArun KS 64c1f27190SArun KS static const DECLARE_TLV_DB_SCALE(out_gain_tlv, -12100, 100, 0); 65c1f27190SArun KS static const DECLARE_TLV_DB_SCALE(input_gain_tlv, -1725, 75, 0); 66df91ddf1SArun KS static const DECLARE_TLV_DB_SCALE(sidetone_vol_tlv, -1800, 300, 0); 67df91ddf1SArun KS 68df91ddf1SArun KS static int snd_soc_tlv320aic23_put_volsw(struct snd_kcontrol *kcontrol, 69df91ddf1SArun KS struct snd_ctl_elem_value *ucontrol) 70df91ddf1SArun KS { 71df91ddf1SArun KS struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 72df91ddf1SArun KS u16 val, reg; 73df91ddf1SArun KS 74df91ddf1SArun KS val = (ucontrol->value.integer.value[0] & 0x07); 75df91ddf1SArun KS 76df91ddf1SArun KS /* linear conversion to userspace 77df91ddf1SArun KS * 000 = -6db 78df91ddf1SArun KS * 001 = -9db 79df91ddf1SArun KS * 010 = -12db 80df91ddf1SArun KS * 011 = -18db (Min) 81df91ddf1SArun KS * 100 = 0db (Max) 82df91ddf1SArun KS */ 83df91ddf1SArun KS val = (val >= 4) ? 4 : (3 - val); 84df91ddf1SArun KS 85f9dfbf91SAxel Lin reg = snd_soc_read(codec, TLV320AIC23_ANLG) & (~0x1C0); 86f9dfbf91SAxel Lin snd_soc_write(codec, TLV320AIC23_ANLG, reg | (val << 6)); 87df91ddf1SArun KS 88df91ddf1SArun KS return 0; 89df91ddf1SArun KS } 90df91ddf1SArun KS 91df91ddf1SArun KS static int snd_soc_tlv320aic23_get_volsw(struct snd_kcontrol *kcontrol, 92df91ddf1SArun KS struct snd_ctl_elem_value *ucontrol) 93df91ddf1SArun KS { 94df91ddf1SArun KS struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 95df91ddf1SArun KS u16 val; 96df91ddf1SArun KS 97f9dfbf91SAxel Lin val = snd_soc_read(codec, TLV320AIC23_ANLG) & (0x1C0); 98df91ddf1SArun KS val = val >> 6; 99df91ddf1SArun KS val = (val >= 4) ? 4 : (3 - val); 100df91ddf1SArun KS ucontrol->value.integer.value[0] = val; 101df91ddf1SArun KS return 0; 102df91ddf1SArun KS 103df91ddf1SArun KS } 104df91ddf1SArun KS 105c1f27190SArun KS static const struct snd_kcontrol_new tlv320aic23_snd_controls[] = { 106c1f27190SArun KS SOC_DOUBLE_R_TLV("Digital Playback Volume", TLV320AIC23_LCHNVOL, 107c1f27190SArun KS TLV320AIC23_RCHNVOL, 0, 127, 0, out_gain_tlv), 108c1f27190SArun KS SOC_SINGLE("Digital Playback Switch", TLV320AIC23_DIGT, 3, 1, 1), 109c1f27190SArun KS SOC_DOUBLE_R("Line Input Switch", TLV320AIC23_LINVOL, 110c1f27190SArun KS TLV320AIC23_RINVOL, 7, 1, 0), 111c1f27190SArun KS SOC_DOUBLE_R_TLV("Line Input Volume", TLV320AIC23_LINVOL, 112c1f27190SArun KS TLV320AIC23_RINVOL, 0, 31, 0, input_gain_tlv), 113c1f27190SArun KS SOC_SINGLE("Mic Input Switch", TLV320AIC23_ANLG, 1, 1, 1), 114c1f27190SArun KS SOC_SINGLE("Mic Booster Switch", TLV320AIC23_ANLG, 0, 1, 0), 1150f9887d1SPeter Ujfalusi SOC_SINGLE_EXT_TLV("Sidetone Volume", TLV320AIC23_ANLG, 6, 4, 0, 1160f9887d1SPeter Ujfalusi snd_soc_tlv320aic23_get_volsw, 1170f9887d1SPeter Ujfalusi snd_soc_tlv320aic23_put_volsw, sidetone_vol_tlv), 118c1f27190SArun KS SOC_ENUM("Playback De-emphasis", tlv320aic23_deemph), 119c1f27190SArun KS }; 120c1f27190SArun KS 121c1f27190SArun KS /* PGA Mixer controls for Line and Mic switch */ 122c1f27190SArun KS static const struct snd_kcontrol_new tlv320aic23_output_mixer_controls[] = { 123c1f27190SArun KS SOC_DAPM_SINGLE("Line Bypass Switch", TLV320AIC23_ANLG, 3, 1, 0), 124c1f27190SArun KS SOC_DAPM_SINGLE("Mic Sidetone Switch", TLV320AIC23_ANLG, 5, 1, 0), 125c1f27190SArun KS SOC_DAPM_SINGLE("Playback Switch", TLV320AIC23_ANLG, 4, 1, 0), 126c1f27190SArun KS }; 127c1f27190SArun KS 128c1f27190SArun KS static const struct snd_soc_dapm_widget tlv320aic23_dapm_widgets[] = { 129c1f27190SArun KS SND_SOC_DAPM_DAC("DAC", "Playback", TLV320AIC23_PWR, 3, 1), 130c1f27190SArun KS SND_SOC_DAPM_ADC("ADC", "Capture", TLV320AIC23_PWR, 2, 1), 131c1f27190SArun KS SND_SOC_DAPM_MUX("Capture Source", SND_SOC_NOPM, 0, 0, 132c1f27190SArun KS &tlv320aic23_rec_src_mux_controls), 133c1f27190SArun KS SND_SOC_DAPM_MIXER("Output Mixer", TLV320AIC23_PWR, 4, 1, 134c1f27190SArun KS &tlv320aic23_output_mixer_controls[0], 135c1f27190SArun KS ARRAY_SIZE(tlv320aic23_output_mixer_controls)), 136c1f27190SArun KS SND_SOC_DAPM_PGA("Line Input", TLV320AIC23_PWR, 0, 1, NULL, 0), 137c1f27190SArun KS SND_SOC_DAPM_PGA("Mic Input", TLV320AIC23_PWR, 1, 1, NULL, 0), 138c1f27190SArun KS 139c1f27190SArun KS SND_SOC_DAPM_OUTPUT("LHPOUT"), 140c1f27190SArun KS SND_SOC_DAPM_OUTPUT("RHPOUT"), 141c1f27190SArun KS SND_SOC_DAPM_OUTPUT("LOUT"), 142c1f27190SArun KS SND_SOC_DAPM_OUTPUT("ROUT"), 143c1f27190SArun KS 144c1f27190SArun KS SND_SOC_DAPM_INPUT("LLINEIN"), 145c1f27190SArun KS SND_SOC_DAPM_INPUT("RLINEIN"), 146c1f27190SArun KS 147c1f27190SArun KS SND_SOC_DAPM_INPUT("MICIN"), 148c1f27190SArun KS }; 149c1f27190SArun KS 150a7dca707SLu Guanqun static const struct snd_soc_dapm_route tlv320aic23_intercon[] = { 151c1f27190SArun KS /* Output Mixer */ 152c1f27190SArun KS {"Output Mixer", "Line Bypass Switch", "Line Input"}, 153c1f27190SArun KS {"Output Mixer", "Playback Switch", "DAC"}, 154c1f27190SArun KS {"Output Mixer", "Mic Sidetone Switch", "Mic Input"}, 155c1f27190SArun KS 156c1f27190SArun KS /* Outputs */ 157c1f27190SArun KS {"RHPOUT", NULL, "Output Mixer"}, 158c1f27190SArun KS {"LHPOUT", NULL, "Output Mixer"}, 159c1f27190SArun KS {"LOUT", NULL, "Output Mixer"}, 160c1f27190SArun KS {"ROUT", NULL, "Output Mixer"}, 161c1f27190SArun KS 162c1f27190SArun KS /* Inputs */ 163c1f27190SArun KS {"Line Input", "NULL", "LLINEIN"}, 164c1f27190SArun KS {"Line Input", "NULL", "RLINEIN"}, 165c1f27190SArun KS 166c1f27190SArun KS {"Mic Input", "NULL", "MICIN"}, 167c1f27190SArun KS 168c1f27190SArun KS /* input mux */ 169c1f27190SArun KS {"Capture Source", "Line", "Line Input"}, 170c1f27190SArun KS {"Capture Source", "Mic", "Mic Input"}, 171c1f27190SArun KS {"ADC", NULL, "Capture Source"}, 172c1f27190SArun KS 173c1f27190SArun KS }; 174c1f27190SArun KS 17526df91c3STroy Kisky /* AIC23 driver data */ 17626df91c3STroy Kisky struct aic23 { 177f0fba2adSLiam Girdwood enum snd_soc_control_type control_type; 17826df91c3STroy Kisky int mclk; 17926df91c3STroy Kisky int requested_adc; 18026df91c3STroy Kisky int requested_dac; 181c1f27190SArun KS }; 182c1f27190SArun KS 18326df91c3STroy Kisky /* 18426df91c3STroy Kisky * Common Crystals used 18526df91c3STroy Kisky * 11.2896 Mhz /128 = *88.2k /192 = 58.8k 18626df91c3STroy Kisky * 12.0000 Mhz /125 = *96k /136 = 88.235K 18726df91c3STroy Kisky * 12.2880 Mhz /128 = *96k /192 = 64k 18826df91c3STroy Kisky * 16.9344 Mhz /128 = 132.3k /192 = *88.2k 18926df91c3STroy Kisky * 18.4320 Mhz /128 = 144k /192 = *96k 19026df91c3STroy Kisky */ 19126df91c3STroy Kisky 19226df91c3STroy Kisky /* 19326df91c3STroy Kisky * Normal BOSR 0-256/2 = 128, 1-384/2 = 192 19426df91c3STroy Kisky * USB BOSR 0-250/2 = 125, 1-272/2 = 136 19526df91c3STroy Kisky */ 19626df91c3STroy Kisky static const int bosr_usb_divisor_table[] = { 19726df91c3STroy Kisky 128, 125, 192, 136 19826df91c3STroy Kisky }; 19926df91c3STroy Kisky #define LOWER_GROUP ((1<<0) | (1<<1) | (1<<2) | (1<<3) | (1<<6) | (1<<7)) 20026df91c3STroy Kisky #define UPPER_GROUP ((1<<8) | (1<<9) | (1<<10) | (1<<11) | (1<<15)) 20126df91c3STroy Kisky static const unsigned short sr_valid_mask[] = { 20226df91c3STroy Kisky LOWER_GROUP|UPPER_GROUP, /* Normal, bosr - 0*/ 20326df91c3STroy Kisky LOWER_GROUP, /* Usb, bosr - 0*/ 204bab02124STroy Kisky LOWER_GROUP|UPPER_GROUP, /* Normal, bosr - 1*/ 20526df91c3STroy Kisky UPPER_GROUP, /* Usb, bosr - 1*/ 20626df91c3STroy Kisky }; 20726df91c3STroy Kisky /* 20826df91c3STroy Kisky * Every divisor is a factor of 11*12 20926df91c3STroy Kisky */ 21026df91c3STroy Kisky #define SR_MULT (11*12) 211ccff4b15STroy Kisky #define A(x) (SR_MULT/x) 21226df91c3STroy Kisky static const unsigned char sr_adc_mult_table[] = { 213ccff4b15STroy Kisky A(2), A(2), A(12), A(12), 0, 0, A(3), A(1), 214ccff4b15STroy Kisky A(2), A(2), A(11), A(11), 0, 0, 0, A(1) 21526df91c3STroy Kisky }; 21626df91c3STroy Kisky static const unsigned char sr_dac_mult_table[] = { 217ccff4b15STroy Kisky A(2), A(12), A(2), A(12), 0, 0, A(3), A(1), 218ccff4b15STroy Kisky A(2), A(11), A(2), A(11), 0, 0, 0, A(1) 21926df91c3STroy Kisky }; 22026df91c3STroy Kisky 22126df91c3STroy Kisky static unsigned get_score(int adc, int adc_l, int adc_h, int need_adc, 22226df91c3STroy Kisky int dac, int dac_l, int dac_h, int need_dac) 22326df91c3STroy Kisky { 22426df91c3STroy Kisky if ((adc >= adc_l) && (adc <= adc_h) && 22526df91c3STroy Kisky (dac >= dac_l) && (dac <= dac_h)) { 22626df91c3STroy Kisky int diff_adc = need_adc - adc; 22726df91c3STroy Kisky int diff_dac = need_dac - dac; 22826df91c3STroy Kisky return abs(diff_adc) + abs(diff_dac); 22926df91c3STroy Kisky } 23026df91c3STroy Kisky return UINT_MAX; 23126df91c3STroy Kisky } 23226df91c3STroy Kisky 23326df91c3STroy Kisky static int find_rate(int mclk, u32 need_adc, u32 need_dac) 23426df91c3STroy Kisky { 23526df91c3STroy Kisky int i, j; 23626df91c3STroy Kisky int best_i = -1; 23726df91c3STroy Kisky int best_j = -1; 23826df91c3STroy Kisky int best_div = 0; 23926df91c3STroy Kisky unsigned best_score = UINT_MAX; 24026df91c3STroy Kisky int adc_l, adc_h, dac_l, dac_h; 24126df91c3STroy Kisky 24226df91c3STroy Kisky need_adc *= SR_MULT; 24326df91c3STroy Kisky need_dac *= SR_MULT; 24426df91c3STroy Kisky /* 24526df91c3STroy Kisky * rates given are +/- 1/32 24626df91c3STroy Kisky */ 24726df91c3STroy Kisky adc_l = need_adc - (need_adc >> 5); 24826df91c3STroy Kisky adc_h = need_adc + (need_adc >> 5); 24926df91c3STroy Kisky dac_l = need_dac - (need_dac >> 5); 25026df91c3STroy Kisky dac_h = need_dac + (need_dac >> 5); 2518d702f23SMark Brown for (i = 0; i < ARRAY_SIZE(bosr_usb_divisor_table); i++) { 25226df91c3STroy Kisky int base = mclk / bosr_usb_divisor_table[i]; 25326df91c3STroy Kisky int mask = sr_valid_mask[i]; 2548d702f23SMark Brown for (j = 0; j < ARRAY_SIZE(sr_adc_mult_table); 2558d702f23SMark Brown j++, mask >>= 1) { 25626df91c3STroy Kisky int adc; 25726df91c3STroy Kisky int dac; 25826df91c3STroy Kisky int score; 25926df91c3STroy Kisky if ((mask & 1) == 0) 26026df91c3STroy Kisky continue; 26126df91c3STroy Kisky adc = base * sr_adc_mult_table[j]; 26226df91c3STroy Kisky dac = base * sr_dac_mult_table[j]; 26326df91c3STroy Kisky score = get_score(adc, adc_l, adc_h, need_adc, 26426df91c3STroy Kisky dac, dac_l, dac_h, need_dac); 26526df91c3STroy Kisky if (best_score > score) { 26626df91c3STroy Kisky best_score = score; 26726df91c3STroy Kisky best_i = i; 26826df91c3STroy Kisky best_j = j; 26926df91c3STroy Kisky best_div = 0; 27026df91c3STroy Kisky } 27126df91c3STroy Kisky score = get_score((adc >> 1), adc_l, adc_h, need_adc, 27226df91c3STroy Kisky (dac >> 1), dac_l, dac_h, need_dac); 27326df91c3STroy Kisky /* prefer to have a /2 */ 27426df91c3STroy Kisky if ((score != UINT_MAX) && (best_score >= score)) { 27526df91c3STroy Kisky best_score = score; 27626df91c3STroy Kisky best_i = i; 27726df91c3STroy Kisky best_j = j; 27826df91c3STroy Kisky best_div = 1; 27926df91c3STroy Kisky } 28026df91c3STroy Kisky } 28126df91c3STroy Kisky } 28226df91c3STroy Kisky return (best_j << 2) | best_i | (best_div << TLV320AIC23_CLKIN_SHIFT); 28326df91c3STroy Kisky } 28426df91c3STroy Kisky 2858d702f23SMark Brown #ifdef DEBUG 28626df91c3STroy Kisky static void get_current_sample_rates(struct snd_soc_codec *codec, int mclk, 28726df91c3STroy Kisky u32 *sample_rate_adc, u32 *sample_rate_dac) 28826df91c3STroy Kisky { 289f9dfbf91SAxel Lin int src = snd_soc_read(codec, TLV320AIC23_SRATE); 29026df91c3STroy Kisky int sr = (src >> 2) & 0x0f; 29126df91c3STroy Kisky int val = (mclk / bosr_usb_divisor_table[src & 3]); 29226df91c3STroy Kisky int adc = (val * sr_adc_mult_table[sr]) / SR_MULT; 29326df91c3STroy Kisky int dac = (val * sr_dac_mult_table[sr]) / SR_MULT; 29426df91c3STroy Kisky if (src & TLV320AIC23_CLKIN_HALF) { 29526df91c3STroy Kisky adc >>= 1; 29626df91c3STroy Kisky dac >>= 1; 29726df91c3STroy Kisky } 29826df91c3STroy Kisky *sample_rate_adc = adc; 29926df91c3STroy Kisky *sample_rate_dac = dac; 30026df91c3STroy Kisky } 3018d702f23SMark Brown #endif 30226df91c3STroy Kisky 30326df91c3STroy Kisky static int set_sample_rate_control(struct snd_soc_codec *codec, int mclk, 30426df91c3STroy Kisky u32 sample_rate_adc, u32 sample_rate_dac) 30526df91c3STroy Kisky { 30626df91c3STroy Kisky /* Search for the right sample rate */ 30726df91c3STroy Kisky int data = find_rate(mclk, sample_rate_adc, sample_rate_dac); 30826df91c3STroy Kisky if (data < 0) { 30926df91c3STroy Kisky printk(KERN_ERR "%s:Invalid rate %u,%u requested\n", 31026df91c3STroy Kisky __func__, sample_rate_adc, sample_rate_dac); 31126df91c3STroy Kisky return -EINVAL; 31226df91c3STroy Kisky } 313f9dfbf91SAxel Lin snd_soc_write(codec, TLV320AIC23_SRATE, data); 3148d702f23SMark Brown #ifdef DEBUG 3158d702f23SMark Brown { 3168d702f23SMark Brown u32 adc, dac; 31726df91c3STroy Kisky get_current_sample_rates(codec, mclk, &adc, &dac); 31826df91c3STroy Kisky printk(KERN_DEBUG "actual samplerate = %u,%u reg=%x\n", 31926df91c3STroy Kisky adc, dac, data); 32026df91c3STroy Kisky } 3218d702f23SMark Brown #endif 32226df91c3STroy Kisky return 0; 32326df91c3STroy Kisky } 32426df91c3STroy Kisky 325c1f27190SArun KS static int tlv320aic23_hw_params(struct snd_pcm_substream *substream, 326dee89c4dSMark Brown struct snd_pcm_hw_params *params, 327dee89c4dSMark Brown struct snd_soc_dai *dai) 328c1f27190SArun KS { 329c1f27190SArun KS struct snd_soc_pcm_runtime *rtd = substream->private_data; 330f0fba2adSLiam Girdwood struct snd_soc_codec *codec = rtd->codec; 33126df91c3STroy Kisky u16 iface_reg; 33226df91c3STroy Kisky int ret; 333f0fba2adSLiam Girdwood struct aic23 *aic23 = snd_soc_codec_get_drvdata(codec); 33426df91c3STroy Kisky u32 sample_rate_adc = aic23->requested_adc; 33526df91c3STroy Kisky u32 sample_rate_dac = aic23->requested_dac; 33626df91c3STroy Kisky u32 sample_rate = params_rate(params); 33726df91c3STroy Kisky 33826df91c3STroy Kisky if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 33926df91c3STroy Kisky aic23->requested_dac = sample_rate_dac = sample_rate; 34026df91c3STroy Kisky if (!sample_rate_adc) 34126df91c3STroy Kisky sample_rate_adc = sample_rate; 34226df91c3STroy Kisky } else { 34326df91c3STroy Kisky aic23->requested_adc = sample_rate_adc = sample_rate; 34426df91c3STroy Kisky if (!sample_rate_dac) 34526df91c3STroy Kisky sample_rate_dac = sample_rate; 34626df91c3STroy Kisky } 34726df91c3STroy Kisky ret = set_sample_rate_control(codec, aic23->mclk, sample_rate_adc, 34826df91c3STroy Kisky sample_rate_dac); 34926df91c3STroy Kisky if (ret < 0) 35026df91c3STroy Kisky return ret; 351c1f27190SArun KS 352f9dfbf91SAxel Lin iface_reg = snd_soc_read(codec, TLV320AIC23_DIGT_FMT) & ~(0x03 << 2); 353f9dfbf91SAxel Lin 354c1f27190SArun KS switch (params_format(params)) { 355c1f27190SArun KS case SNDRV_PCM_FORMAT_S16_LE: 356c1f27190SArun KS break; 357c1f27190SArun KS case SNDRV_PCM_FORMAT_S20_3LE: 358c1f27190SArun KS iface_reg |= (0x01 << 2); 359c1f27190SArun KS break; 360c1f27190SArun KS case SNDRV_PCM_FORMAT_S24_LE: 361c1f27190SArun KS iface_reg |= (0x02 << 2); 362c1f27190SArun KS break; 363c1f27190SArun KS case SNDRV_PCM_FORMAT_S32_LE: 364c1f27190SArun KS iface_reg |= (0x03 << 2); 365c1f27190SArun KS break; 366c1f27190SArun KS } 367f9dfbf91SAxel Lin snd_soc_write(codec, TLV320AIC23_DIGT_FMT, iface_reg); 368c1f27190SArun KS 369c1f27190SArun KS return 0; 370c1f27190SArun KS } 371c1f27190SArun KS 372dee89c4dSMark Brown static int tlv320aic23_pcm_prepare(struct snd_pcm_substream *substream, 373dee89c4dSMark Brown struct snd_soc_dai *dai) 374c1f27190SArun KS { 375c1f27190SArun KS struct snd_soc_pcm_runtime *rtd = substream->private_data; 376f0fba2adSLiam Girdwood struct snd_soc_codec *codec = rtd->codec; 377c1f27190SArun KS 378c1f27190SArun KS /* set active */ 379f9dfbf91SAxel Lin snd_soc_write(codec, TLV320AIC23_ACTIVE, 0x0001); 380c1f27190SArun KS 381c1f27190SArun KS return 0; 382c1f27190SArun KS } 383c1f27190SArun KS 384dee89c4dSMark Brown static void tlv320aic23_shutdown(struct snd_pcm_substream *substream, 385dee89c4dSMark Brown struct snd_soc_dai *dai) 386c1f27190SArun KS { 387c1f27190SArun KS struct snd_soc_pcm_runtime *rtd = substream->private_data; 388f0fba2adSLiam Girdwood struct snd_soc_codec *codec = rtd->codec; 389f0fba2adSLiam Girdwood struct aic23 *aic23 = snd_soc_codec_get_drvdata(codec); 390c1f27190SArun KS 391c1f27190SArun KS /* deactivate */ 392c1f27190SArun KS if (!codec->active) { 393c1f27190SArun KS udelay(50); 394f9dfbf91SAxel Lin snd_soc_write(codec, TLV320AIC23_ACTIVE, 0x0); 395c1f27190SArun KS } 39626df91c3STroy Kisky if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 39726df91c3STroy Kisky aic23->requested_dac = 0; 39826df91c3STroy Kisky else 39926df91c3STroy Kisky aic23->requested_adc = 0; 400c1f27190SArun KS } 401c1f27190SArun KS 402c1f27190SArun KS static int tlv320aic23_mute(struct snd_soc_dai *dai, int mute) 403c1f27190SArun KS { 404c1f27190SArun KS struct snd_soc_codec *codec = dai->codec; 405c1f27190SArun KS u16 reg; 406c1f27190SArun KS 407f9dfbf91SAxel Lin reg = snd_soc_read(codec, TLV320AIC23_DIGT); 408c1f27190SArun KS if (mute) 409c1f27190SArun KS reg |= TLV320AIC23_DACM_MUTE; 410c1f27190SArun KS 411c1f27190SArun KS else 412c1f27190SArun KS reg &= ~TLV320AIC23_DACM_MUTE; 413c1f27190SArun KS 414f9dfbf91SAxel Lin snd_soc_write(codec, TLV320AIC23_DIGT, reg); 415c1f27190SArun KS 416c1f27190SArun KS return 0; 417c1f27190SArun KS } 418c1f27190SArun KS 419c1f27190SArun KS static int tlv320aic23_set_dai_fmt(struct snd_soc_dai *codec_dai, 420c1f27190SArun KS unsigned int fmt) 421c1f27190SArun KS { 422c1f27190SArun KS struct snd_soc_codec *codec = codec_dai->codec; 423c1f27190SArun KS u16 iface_reg; 424c1f27190SArun KS 425f9dfbf91SAxel Lin iface_reg = snd_soc_read(codec, TLV320AIC23_DIGT_FMT) & (~0x03); 426c1f27190SArun KS 427c1f27190SArun KS /* set master/slave audio interface */ 428c1f27190SArun KS switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { 429c1f27190SArun KS case SND_SOC_DAIFMT_CBM_CFM: 430c1f27190SArun KS iface_reg |= TLV320AIC23_MS_MASTER; 431c1f27190SArun KS break; 432c1f27190SArun KS case SND_SOC_DAIFMT_CBS_CFS: 433b01a3d69SAxel Lin iface_reg &= ~TLV320AIC23_MS_MASTER; 434c1f27190SArun KS break; 435c1f27190SArun KS default: 436c1f27190SArun KS return -EINVAL; 437c1f27190SArun KS 438c1f27190SArun KS } 439c1f27190SArun KS 440c1f27190SArun KS /* interface format */ 441c1f27190SArun KS switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 442c1f27190SArun KS case SND_SOC_DAIFMT_I2S: 443c1f27190SArun KS iface_reg |= TLV320AIC23_FOR_I2S; 444c1f27190SArun KS break; 445894bf92fSPeter Ujfalusi case SND_SOC_DAIFMT_DSP_A: 446894bf92fSPeter Ujfalusi iface_reg |= TLV320AIC23_LRP_ON; 447bd25867aSJarkko Nikula case SND_SOC_DAIFMT_DSP_B: 448c1f27190SArun KS iface_reg |= TLV320AIC23_FOR_DSP; 449c1f27190SArun KS break; 450c1f27190SArun KS case SND_SOC_DAIFMT_RIGHT_J: 451c1f27190SArun KS break; 452c1f27190SArun KS case SND_SOC_DAIFMT_LEFT_J: 453c1f27190SArun KS iface_reg |= TLV320AIC23_FOR_LJUST; 454c1f27190SArun KS break; 455c1f27190SArun KS default: 456c1f27190SArun KS return -EINVAL; 457c1f27190SArun KS 458c1f27190SArun KS } 459c1f27190SArun KS 460f9dfbf91SAxel Lin snd_soc_write(codec, TLV320AIC23_DIGT_FMT, iface_reg); 461c1f27190SArun KS 462c1f27190SArun KS return 0; 463c1f27190SArun KS } 464c1f27190SArun KS 465c1f27190SArun KS static int tlv320aic23_set_dai_sysclk(struct snd_soc_dai *codec_dai, 466c1f27190SArun KS int clk_id, unsigned int freq, int dir) 467c1f27190SArun KS { 468f0fba2adSLiam Girdwood struct aic23 *aic23 = snd_soc_dai_get_drvdata(codec_dai); 46926df91c3STroy Kisky aic23->mclk = freq; 470c1f27190SArun KS return 0; 471c1f27190SArun KS } 472c1f27190SArun KS 473c1f27190SArun KS static int tlv320aic23_set_bias_level(struct snd_soc_codec *codec, 474c1f27190SArun KS enum snd_soc_bias_level level) 475c1f27190SArun KS { 476f9dfbf91SAxel Lin u16 reg = snd_soc_read(codec, TLV320AIC23_PWR) & 0xff7f; 477c1f27190SArun KS 478c1f27190SArun KS switch (level) { 479c1f27190SArun KS case SND_SOC_BIAS_ON: 480c1f27190SArun KS /* vref/mid, osc on, dac unmute */ 4813d5a4516SEric Bénard reg &= ~(TLV320AIC23_DEVICE_PWR_OFF | TLV320AIC23_OSC_OFF | \ 4823d5a4516SEric Bénard TLV320AIC23_DAC_OFF); 483f9dfbf91SAxel Lin snd_soc_write(codec, TLV320AIC23_PWR, reg); 484c1f27190SArun KS break; 485c1f27190SArun KS case SND_SOC_BIAS_PREPARE: 486c1f27190SArun KS break; 487c1f27190SArun KS case SND_SOC_BIAS_STANDBY: 488c1f27190SArun KS /* everything off except vref/vmid, */ 489f9dfbf91SAxel Lin snd_soc_write(codec, TLV320AIC23_PWR, 490f9dfbf91SAxel Lin reg | TLV320AIC23_CLK_OFF); 491c1f27190SArun KS break; 492c1f27190SArun KS case SND_SOC_BIAS_OFF: 493c1f27190SArun KS /* everything off, dac mute, inactive */ 494f9dfbf91SAxel Lin snd_soc_write(codec, TLV320AIC23_ACTIVE, 0x0); 495f9dfbf91SAxel Lin snd_soc_write(codec, TLV320AIC23_PWR, 0xffff); 496c1f27190SArun KS break; 497c1f27190SArun KS } 498ce6120ccSLiam Girdwood codec->dapm.bias_level = level; 499c1f27190SArun KS return 0; 500c1f27190SArun KS } 501c1f27190SArun KS 502c1f27190SArun KS #define AIC23_RATES SNDRV_PCM_RATE_8000_96000 503c1f27190SArun KS #define AIC23_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \ 504c1f27190SArun KS SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE) 505c1f27190SArun KS 5066335d055SEric Miao static struct snd_soc_dai_ops tlv320aic23_dai_ops = { 5076335d055SEric Miao .prepare = tlv320aic23_pcm_prepare, 5086335d055SEric Miao .hw_params = tlv320aic23_hw_params, 5096335d055SEric Miao .shutdown = tlv320aic23_shutdown, 5106335d055SEric Miao .digital_mute = tlv320aic23_mute, 5116335d055SEric Miao .set_fmt = tlv320aic23_set_dai_fmt, 5126335d055SEric Miao .set_sysclk = tlv320aic23_set_dai_sysclk, 5136335d055SEric Miao }; 5146335d055SEric Miao 515f0fba2adSLiam Girdwood static struct snd_soc_dai_driver tlv320aic23_dai = { 516f0fba2adSLiam Girdwood .name = "tlv320aic23-hifi", 517c1f27190SArun KS .playback = { 518c1f27190SArun KS .stream_name = "Playback", 519c1f27190SArun KS .channels_min = 2, 520c1f27190SArun KS .channels_max = 2, 521c1f27190SArun KS .rates = AIC23_RATES, 522c1f27190SArun KS .formats = AIC23_FORMATS,}, 523c1f27190SArun KS .capture = { 524c1f27190SArun KS .stream_name = "Capture", 525c1f27190SArun KS .channels_min = 2, 526c1f27190SArun KS .channels_max = 2, 527c1f27190SArun KS .rates = AIC23_RATES, 528c1f27190SArun KS .formats = AIC23_FORMATS,}, 5296335d055SEric Miao .ops = &tlv320aic23_dai_ops, 530c1f27190SArun KS }; 531c1f27190SArun KS 532f0fba2adSLiam Girdwood static int tlv320aic23_suspend(struct snd_soc_codec *codec, 533c1f27190SArun KS pm_message_t state) 534c1f27190SArun KS { 535c1f27190SArun KS tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_OFF); 536c1f27190SArun KS 537c1f27190SArun KS return 0; 538c1f27190SArun KS } 539c1f27190SArun KS 540f0fba2adSLiam Girdwood static int tlv320aic23_resume(struct snd_soc_codec *codec) 541c1f27190SArun KS { 542f9dfbf91SAxel Lin snd_soc_cache_sync(codec); 543c1f27190SArun KS tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_STANDBY); 544c1f27190SArun KS 545c1f27190SArun KS return 0; 546c1f27190SArun KS } 547c1f27190SArun KS 548f0fba2adSLiam Girdwood static int tlv320aic23_probe(struct snd_soc_codec *codec) 549c1f27190SArun KS { 550f0fba2adSLiam Girdwood struct aic23 *aic23 = snd_soc_codec_get_drvdata(codec); 551f9dfbf91SAxel Lin int ret; 552c1f27190SArun KS 553f0fba2adSLiam Girdwood printk(KERN_INFO "AIC23 Audio Codec %s\n", AIC23_VERSION); 554f9dfbf91SAxel Lin 555f9dfbf91SAxel Lin ret = snd_soc_codec_set_cache_io(codec, 7, 9, aic23->control_type); 556f9dfbf91SAxel Lin if (ret < 0) { 557f9dfbf91SAxel Lin dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret); 558f9dfbf91SAxel Lin return ret; 559f9dfbf91SAxel Lin } 560c1f27190SArun KS 561c1f27190SArun KS /* Reset codec */ 562f9dfbf91SAxel Lin snd_soc_write(codec, TLV320AIC23_RESET, 0); 563f9dfbf91SAxel Lin 564f9dfbf91SAxel Lin /* Write the register default value to cache for reserved registers, 565f9dfbf91SAxel Lin * so the write to the these registers are suppressed by the cache 566f9dfbf91SAxel Lin * restore code when it skips writes of default registers. 567f9dfbf91SAxel Lin */ 568f9dfbf91SAxel Lin snd_soc_cache_write(codec, 0x0A, 0); 569f9dfbf91SAxel Lin snd_soc_cache_write(codec, 0x0B, 0); 570f9dfbf91SAxel Lin snd_soc_cache_write(codec, 0x0C, 0); 571f9dfbf91SAxel Lin snd_soc_cache_write(codec, 0x0D, 0); 572f9dfbf91SAxel Lin snd_soc_cache_write(codec, 0x0E, 0); 573c1f27190SArun KS 574c1f27190SArun KS /* power on device */ 575c1f27190SArun KS tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_STANDBY); 576c1f27190SArun KS 577f9dfbf91SAxel Lin snd_soc_write(codec, TLV320AIC23_DIGT, TLV320AIC23_DEEMP_44K); 578c1f27190SArun KS 579c1f27190SArun KS /* Unmute input */ 580f9dfbf91SAxel Lin snd_soc_update_bits(codec, TLV320AIC23_LINVOL, 581f9dfbf91SAxel Lin TLV320AIC23_LIM_MUTED, TLV320AIC23_LRS_ENABLED); 582c1f27190SArun KS 583f9dfbf91SAxel Lin snd_soc_update_bits(codec, TLV320AIC23_RINVOL, 584f9dfbf91SAxel Lin TLV320AIC23_LIM_MUTED, TLV320AIC23_LRS_ENABLED); 585c1f27190SArun KS 586f9dfbf91SAxel Lin snd_soc_update_bits(codec, TLV320AIC23_ANLG, 587f9dfbf91SAxel Lin TLV320AIC23_BYPASS_ON | TLV320AIC23_MICM_MUTED, 588f9dfbf91SAxel Lin 0); 589c1f27190SArun KS 590c1f27190SArun KS /* Default output volume */ 591f9dfbf91SAxel Lin snd_soc_write(codec, TLV320AIC23_LCHNVOL, 592f9dfbf91SAxel Lin TLV320AIC23_DEFAULT_OUT_VOL & TLV320AIC23_OUT_VOL_MASK); 593f9dfbf91SAxel Lin snd_soc_write(codec, TLV320AIC23_RCHNVOL, 594f9dfbf91SAxel Lin TLV320AIC23_DEFAULT_OUT_VOL & TLV320AIC23_OUT_VOL_MASK); 595c1f27190SArun KS 596f9dfbf91SAxel Lin snd_soc_write(codec, TLV320AIC23_ACTIVE, 0x1); 597c1f27190SArun KS 5983e8e1952SIan Molton snd_soc_add_controls(codec, tlv320aic23_snd_controls, 5993e8e1952SIan Molton ARRAY_SIZE(tlv320aic23_snd_controls)); 600c1f27190SArun KS 601f0fba2adSLiam Girdwood return 0; 602c1f27190SArun KS } 603f0fba2adSLiam Girdwood 604f0fba2adSLiam Girdwood static int tlv320aic23_remove(struct snd_soc_codec *codec) 605f0fba2adSLiam Girdwood { 606f0fba2adSLiam Girdwood tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_OFF); 607f0fba2adSLiam Girdwood return 0; 608f0fba2adSLiam Girdwood } 609f0fba2adSLiam Girdwood 610f0fba2adSLiam Girdwood static struct snd_soc_codec_driver soc_codec_dev_tlv320aic23 = { 611f0fba2adSLiam Girdwood .reg_cache_size = ARRAY_SIZE(tlv320aic23_reg), 612f0fba2adSLiam Girdwood .reg_word_size = sizeof(u16), 613f0fba2adSLiam Girdwood .reg_cache_default = tlv320aic23_reg, 614f0fba2adSLiam Girdwood .probe = tlv320aic23_probe, 615f0fba2adSLiam Girdwood .remove = tlv320aic23_remove, 616f0fba2adSLiam Girdwood .suspend = tlv320aic23_suspend, 617f0fba2adSLiam Girdwood .resume = tlv320aic23_resume, 618f0fba2adSLiam Girdwood .set_bias_level = tlv320aic23_set_bias_level, 619a7dca707SLu Guanqun .dapm_widgets = tlv320aic23_dapm_widgets, 620a7dca707SLu Guanqun .num_dapm_widgets = ARRAY_SIZE(tlv320aic23_dapm_widgets), 621a7dca707SLu Guanqun .dapm_routes = tlv320aic23_intercon, 622a7dca707SLu Guanqun .num_dapm_routes = ARRAY_SIZE(tlv320aic23_intercon), 623f0fba2adSLiam Girdwood }; 624c1f27190SArun KS 625c1f27190SArun KS #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) 626c1f27190SArun KS /* 627c1f27190SArun KS * If the i2c layer weren't so broken, we could pass this kind of data 628c1f27190SArun KS * around 629c1f27190SArun KS */ 630c1f27190SArun KS static int tlv320aic23_codec_probe(struct i2c_client *i2c, 631c1f27190SArun KS const struct i2c_device_id *i2c_id) 632c1f27190SArun KS { 633f0fba2adSLiam Girdwood struct aic23 *aic23; 634c1f27190SArun KS int ret; 635c1f27190SArun KS 636c1f27190SArun KS if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA)) 637c1f27190SArun KS return -EINVAL; 638c1f27190SArun KS 639f0fba2adSLiam Girdwood aic23 = kzalloc(sizeof(struct aic23), GFP_KERNEL); 640f0fba2adSLiam Girdwood if (aic23 == NULL) 641f0fba2adSLiam Girdwood return -ENOMEM; 642c1f27190SArun KS 643f0fba2adSLiam Girdwood i2c_set_clientdata(i2c, aic23); 644f0fba2adSLiam Girdwood aic23->control_type = SND_SOC_I2C; 645c1f27190SArun KS 646f0fba2adSLiam Girdwood ret = snd_soc_register_codec(&i2c->dev, 647f0fba2adSLiam Girdwood &soc_codec_dev_tlv320aic23, &tlv320aic23_dai, 1); 648f0fba2adSLiam Girdwood if (ret < 0) 649f0fba2adSLiam Girdwood kfree(aic23); 650c1f27190SArun KS return ret; 651c1f27190SArun KS } 652c1f27190SArun KS static int __exit tlv320aic23_i2c_remove(struct i2c_client *i2c) 653c1f27190SArun KS { 654f0fba2adSLiam Girdwood snd_soc_unregister_codec(&i2c->dev); 655f0fba2adSLiam Girdwood kfree(i2c_get_clientdata(i2c)); 656c1f27190SArun KS return 0; 657c1f27190SArun KS } 658c1f27190SArun KS 659c1f27190SArun KS static const struct i2c_device_id tlv320aic23_id[] = { 660c1f27190SArun KS {"tlv320aic23", 0}, 661c1f27190SArun KS {} 662c1f27190SArun KS }; 663c1f27190SArun KS 664c1f27190SArun KS MODULE_DEVICE_TABLE(i2c, tlv320aic23_id); 665c1f27190SArun KS 666c1f27190SArun KS static struct i2c_driver tlv320aic23_i2c_driver = { 667c1f27190SArun KS .driver = { 668f0fba2adSLiam Girdwood .name = "tlv320aic23-codec", 669c1f27190SArun KS }, 670c1f27190SArun KS .probe = tlv320aic23_codec_probe, 671c1f27190SArun KS .remove = __exit_p(tlv320aic23_i2c_remove), 672c1f27190SArun KS .id_table = tlv320aic23_id, 673c1f27190SArun KS }; 674c1f27190SArun KS 675c1f27190SArun KS #endif 676c1f27190SArun KS 677c9b3a40fSTakashi Iwai static int __init tlv320aic23_modinit(void) 67864089b84SMark Brown { 679f0fba2adSLiam Girdwood int ret; 680f0fba2adSLiam Girdwood #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) 681f0fba2adSLiam Girdwood ret = i2c_add_driver(&tlv320aic23_i2c_driver); 682f0fba2adSLiam Girdwood if (ret != 0) { 683f0fba2adSLiam Girdwood printk(KERN_ERR "Failed to register TLV320AIC23 I2C driver: %d\n", 684f0fba2adSLiam Girdwood ret); 685f0fba2adSLiam Girdwood } 686f0fba2adSLiam Girdwood #endif 687f0fba2adSLiam Girdwood return ret; 68864089b84SMark Brown } 68964089b84SMark Brown module_init(tlv320aic23_modinit); 69064089b84SMark Brown 69164089b84SMark Brown static void __exit tlv320aic23_exit(void) 69264089b84SMark Brown { 693f0fba2adSLiam Girdwood #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) 694f0fba2adSLiam Girdwood i2c_del_driver(&tlv320aic23_i2c_driver); 695f0fba2adSLiam Girdwood #endif 69664089b84SMark Brown } 69764089b84SMark Brown module_exit(tlv320aic23_exit); 69864089b84SMark Brown 699c1f27190SArun KS MODULE_DESCRIPTION("ASoC TLV320AIC23 codec driver"); 700c1f27190SArun KS MODULE_AUTHOR("Arun KS <arunks@mistralsolutions.com>"); 701c1f27190SArun KS MODULE_LICENSE("GPL"); 702