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