11 #include <linux/module.h>
15 #include <linux/slab.h>
17 #include <linux/i2c.h>
33 #define SGTL5000_DAP_REG_OFFSET 0x0100
34 #define SGTL5000_MAX_REG_OFFSET 0x013A
78 #define LDO_CONSUMER_NAME "VDDD_LDO"
79 #define LDO_VOLTAGE 1200000
92 .num_consumer_supplies = 1,
93 .consumer_supplies = &ldo_consumer[0],
172 static const char *adc_mux_text[] = {
176 static const struct soc_enum adc_enum =
183 static const char *dac_mux_text[] = {
187 static const struct soc_enum dac_enum =
230 {
"Capture Mux",
"LINE_IN",
"LINE_IN"},
231 {
"Capture Mux",
"MIC_IN",
"MIC_IN"},
233 {
"ADC",
NULL,
"VAG_POWER"},
234 {
"ADC",
NULL,
"Capture Mux"},
235 {
"AIFOUT",
NULL,
"ADC"},
237 {
"DAC",
NULL,
"VAG_POWER"},
238 {
"DAC",
NULL,
"AIFIN"},
239 {
"Headphone Mux",
"DAC",
"DAC"},
242 {
"LINE_IN",
NULL,
"VAG_POWER"},
243 {
"Headphone Mux",
"LINE_IN",
"LINE_IN"},
244 {
"HP",
NULL,
"Headphone Mux"},
246 {
"LINE_OUT",
NULL,
"LO"},
247 {
"HP_OUT",
NULL,
"HP"},
251 static int dac_info_volsw(
struct snd_kcontrol *kcontrol,
301 l =
clamp(l, 0x3c, 0xfc);
302 r =
clamp(r, 0x3c, 0xfc);
308 ucontrol->
value.integer.value[0] =
l;
309 ucontrol->
value.integer.value[1] =
r;
345 l = ucontrol->
value.integer.value[0];
346 r = ucontrol->
value.integer.value[1];
349 l =
clamp(l, 0, 0xfc - 0x3c);
350 r =
clamp(r, 0, 0xfc - 0x3c);
368 static const unsigned int mic_gain_tlv[] = {
381 .name =
"PCM Playback Volume",
384 .info = dac_info_volsw,
385 .get = dac_get_volsw,
386 .put = dac_put_volsw,
392 8, 2, 0, capture_6db_attenuate),
404 0, 4, 0, mic_gain_tlv),
408 static int sgtl5000_digital_mute(
struct snd_soc_dai *codec_dai,
int mute)
414 adcdac_ctrl, mute ? adcdac_ctrl : 0);
420 static int sgtl5000_set_dai_fmt(
struct snd_soc_dai *codec_dai,
unsigned int fmt)
423 struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
487 static int sgtl5000_set_dai_sysclk(
struct snd_soc_dai *codec_dai,
488 int clk_id,
unsigned int freq,
int dir)
491 struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
517 static int sgtl5000_set_clock(
struct snd_soc_codec *codec,
int frame_rate)
519 struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
528 switch (frame_rate) {
543 switch (sys_fs / frame_rate) {
572 dev_err(codec->
dev,
"frame rate %d not supported\n",
581 switch (sgtl5000->
sysclk / sys_fs) {
601 "PLL not supported in slave mode\n");
611 unsigned int in, int_div, frac_div;
613 if (sgtl5000->
sysclk > 17000000) {
615 in = sgtl5000->
sysclk / 2;
670 struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
678 dev_err(codec->
dev,
"%s: set sysclk first!\n", __func__);
689 channels == 1 ? 0 : stereo);
692 ret = sgtl5000_set_clock(codec,
params_rate(params));
733 #ifdef CONFIG_REGULATOR
747 if (ldo_regulator_is_enabled(dev))
792 static int ldo_regulator_get_voltage(
struct regulator_dev *dev)
801 .enable = ldo_regulator_enable,
802 .disable = ldo_regulator_disable,
803 .get_voltage = ldo_regulator_get_voltage,
806 static int ldo_regulator_register(
struct snd_soc_codec *codec,
811 struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
817 dev_err(codec->
dev,
"failed to allocate ldo_regulator\n");
822 if (!ldo->
desc.name) {
824 dev_err(codec->
dev,
"failed to allocate decs name memory\n");
830 ldo->
desc.ops = &ldo_regulator_ops;
831 ldo->
desc.n_voltages = 1;
841 if (IS_ERR(ldo->
dev)) {
842 int ret = PTR_ERR(ldo->
dev);
844 dev_err(codec->
dev,
"failed to register regulator\n");
857 struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
870 static int ldo_regulator_register(
struct snd_soc_codec *codec,
874 dev_err(codec->
dev,
"this setup needs regulator support in the kernel\n");
894 static int sgtl5000_set_bias_level(
struct snd_soc_codec *codec,
898 struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
925 #define SGTL5000_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
926 SNDRV_PCM_FMTBIT_S20_3LE |\
927 SNDRV_PCM_FMTBIT_S24_LE |\
928 SNDRV_PCM_FMTBIT_S32_LE)
931 .hw_params = sgtl5000_pcm_hw_params,
932 .digital_mute = sgtl5000_digital_mute,
933 .set_fmt = sgtl5000_set_dai_fmt,
934 .set_sysclk = sgtl5000_set_dai_sysclk,
940 .stream_name =
"Playback",
951 .stream_name =
"Capture",
957 .ops = &sgtl5000_ops,
958 .symmetric_rates = 1,
961 static int sgtl5000_volatile_register(
struct snd_soc_codec *codec,
974 #ifdef CONFIG_SUSPEND
987 static int sgtl5000_restore_regs(
struct snd_soc_codec *codec)
1046 sgtl5000_restore_regs(codec);
1050 #define sgtl5000_suspend NULL
1051 #define sgtl5000_resume NULL
1066 static int sgtl5000_set_power_regs(
struct snd_soc_codec *codec)
1074 struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
1081 vddio = vddio / 1000;
1084 if (vdda <= 0 || vddio <= 0 || vddd < 0) {
1085 dev_err(codec->
dev,
"regulator voltage not set correctly\n");
1091 if (vdda > 3600 || vddio > 3600 || vddd > 1980) {
1093 "exceed max voltage vdda %dmV vddio %dmV vddd %dmV\n",
1106 if (vddio < 3100 && vdda < 3100) {
1113 }
else if (vddio >= 3100 && vdda >= 3100) {
1187 static int sgtl5000_replace_vddd_with_ldo(
struct snd_soc_codec *codec)
1189 struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
1193 ret = ldo_regulator_register(codec, &ldo_init_data,
LDO_VOLTAGE);
1196 "Failed to register vddd internal supplies: %d\n", ret);
1206 ldo_regulator_remove(codec);
1207 dev_err(codec->
dev,
"Failed to request supplies: %d\n", ret);
1211 dev_info(codec->
dev,
"Using internal LDO instead of VDDD\n");
1215 static int sgtl5000_enable_regulators(
struct snd_soc_codec *codec)
1221 int external_vddd = 0;
1222 struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
1225 sgtl5000->
supplies[i].supply = supply_names[i];
1232 ret = sgtl5000_replace_vddd_with_ldo(codec);
1240 goto err_regulator_free;
1250 "Device with ID register %x is not a sgtl5000\n", reg);
1252 goto err_regulator_disable;
1256 dev_info(codec->
dev,
"sgtl5000 revision 0x%x\n", rev);
1262 if (external_vddd && rev >= 0x11) {
1270 ret = sgtl5000_replace_vddd_with_ldo(codec);
1277 goto err_regulator_free;
1285 err_regulator_disable:
1292 ldo_regulator_remove(codec);
1300 struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
1305 dev_err(codec->
dev,
"Failed to set cache I/O: %d\n", ret);
1309 ret = sgtl5000_enable_regulators(codec);
1314 ret = sgtl5000_set_power_regs(codec);
1368 ldo_regulator_remove(codec);
1375 struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
1383 ldo_regulator_remove(codec);
1389 .probe = sgtl5000_probe,
1390 .remove = sgtl5000_remove,
1393 .set_bias_level = sgtl5000_set_bias_level,
1395 .reg_word_size =
sizeof(
u16),
1401 .dapm_widgets = sgtl5000_dapm_widgets,
1402 .num_dapm_widgets =
ARRAY_SIZE(sgtl5000_dapm_widgets),
1403 .dapm_routes = sgtl5000_dapm_routes,
1404 .num_dapm_routes =
ARRAY_SIZE(sgtl5000_dapm_routes),
1418 i2c_set_clientdata(client, sgtl5000);
1421 &sgtl5000_driver, &sgtl5000_dai, 1);
1440 { .compatible =
"fsl,sgtl5000", },
1445 static struct i2c_driver sgtl5000_i2c_driver = {
1449 .of_match_table = sgtl5000_dt_ids,
1451 .probe = sgtl5000_i2c_probe,
1453 .id_table = sgtl5000_id,