1 // SPDX-License-Identifier: GPL-2.0-only 2 // 3 // uda1334.c -- UDA1334 ALSA SoC Audio driver 4 // 5 // Based on WM8523 ALSA SoC Audio driver written by Mark Brown 6 7 #include <linux/module.h> 8 #include <linux/moduleparam.h> 9 #include <linux/init.h> 10 #include <linux/delay.h> 11 #include <linux/slab.h> 12 #include <linux/gpio/consumer.h> 13 #include <sound/core.h> 14 #include <sound/pcm.h> 15 #include <sound/pcm_params.h> 16 #include <sound/soc.h> 17 #include <sound/initval.h> 18 19 #define UDA1334_NUM_RATES 6 20 21 /* codec private data */ 22 struct uda1334_priv { 23 struct gpio_desc *mute; 24 struct gpio_desc *deemph; 25 unsigned int sysclk; 26 unsigned int rate_constraint_list[UDA1334_NUM_RATES]; 27 struct snd_pcm_hw_constraint_list rate_constraint; 28 }; 29 30 static const struct snd_soc_dapm_widget uda1334_dapm_widgets[] = { 31 SND_SOC_DAPM_DAC("DAC", "Playback", SND_SOC_NOPM, 0, 0), 32 SND_SOC_DAPM_OUTPUT("LINEVOUTL"), 33 SND_SOC_DAPM_OUTPUT("LINEVOUTR"), 34 }; 35 36 static const struct snd_soc_dapm_route uda1334_dapm_routes[] = { 37 { "LINEVOUTL", NULL, "DAC" }, 38 { "LINEVOUTR", NULL, "DAC" }, 39 }; 40 41 static int uda1334_put_deemph(struct snd_kcontrol *kcontrol, 42 struct snd_ctl_elem_value *ucontrol) 43 { 44 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 45 struct uda1334_priv *uda1334 = snd_soc_component_get_drvdata(component); 46 int deemph = ucontrol->value.integer.value[0]; 47 48 if (deemph > 1) 49 return -EINVAL; 50 51 gpiod_set_value_cansleep(uda1334->deemph, deemph); 52 53 return 0; 54 }; 55 56 static int uda1334_get_deemph(struct snd_kcontrol *kcontrol, 57 struct snd_ctl_elem_value *ucontrol) 58 { 59 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 60 struct uda1334_priv *uda1334 = snd_soc_component_get_drvdata(component); 61 int ret; 62 63 ret = gpiod_get_value_cansleep(uda1334->deemph); 64 if (ret < 0) 65 return -EINVAL; 66 67 ucontrol->value.integer.value[0] = ret; 68 69 return 0; 70 }; 71 72 static const struct snd_kcontrol_new uda1334_snd_controls[] = { 73 SOC_SINGLE_BOOL_EXT("Playback Deemphasis Switch", 0, 74 uda1334_get_deemph, uda1334_put_deemph), 75 }; 76 77 static const struct { 78 int value; 79 int ratio; 80 } lrclk_ratios[UDA1334_NUM_RATES] = { 81 { 1, 128 }, 82 { 2, 192 }, 83 { 3, 256 }, 84 { 4, 384 }, 85 { 5, 512 }, 86 { 6, 768 }, 87 }; 88 89 static int uda1334_startup(struct snd_pcm_substream *substream, 90 struct snd_soc_dai *dai) 91 { 92 struct snd_soc_component *component = dai->component; 93 struct uda1334_priv *uda1334 = snd_soc_component_get_drvdata(component); 94 95 /* 96 * The set of sample rates that can be supported depends on the 97 * MCLK supplied to the CODEC - enforce this. 98 */ 99 if (!uda1334->sysclk) { 100 dev_err(component->dev, 101 "No MCLK configured, call set_sysclk() on init\n"); 102 return -EINVAL; 103 } 104 105 snd_pcm_hw_constraint_list(substream->runtime, 0, 106 SNDRV_PCM_HW_PARAM_RATE, 107 &uda1334->rate_constraint); 108 109 gpiod_set_value_cansleep(uda1334->mute, 1); 110 111 return 0; 112 } 113 114 static void uda1334_shutdown(struct snd_pcm_substream *substream, 115 struct snd_soc_dai *dai) 116 { 117 struct snd_soc_component *component = dai->component; 118 struct uda1334_priv *uda1334 = snd_soc_component_get_drvdata(component); 119 120 gpiod_set_value_cansleep(uda1334->mute, 0); 121 } 122 123 static int uda1334_set_dai_sysclk(struct snd_soc_dai *codec_dai, 124 int clk_id, unsigned int freq, int dir) 125 { 126 struct snd_soc_component *component = codec_dai->component; 127 struct uda1334_priv *uda1334 = snd_soc_component_get_drvdata(component); 128 unsigned int val; 129 int i, j = 0; 130 131 uda1334->sysclk = freq; 132 133 uda1334->rate_constraint.count = 0; 134 for (i = 0; i < ARRAY_SIZE(lrclk_ratios); i++) { 135 val = freq / lrclk_ratios[i].ratio; 136 /* 137 * Check that it's a standard rate since core can't 138 * cope with others and having the odd rates confuses 139 * constraint matching. 140 */ 141 142 switch (val) { 143 case 8000: 144 case 32000: 145 case 44100: 146 case 48000: 147 case 64000: 148 case 88200: 149 case 96000: 150 dev_dbg(component->dev, "Supported sample rate: %dHz\n", 151 val); 152 uda1334->rate_constraint_list[j++] = val; 153 uda1334->rate_constraint.count++; 154 break; 155 default: 156 dev_dbg(component->dev, "Skipping sample rate: %dHz\n", 157 val); 158 } 159 } 160 161 /* Need at least one supported rate... */ 162 if (uda1334->rate_constraint.count == 0) 163 return -EINVAL; 164 165 return 0; 166 } 167 168 static int uda1334_set_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt) 169 { 170 fmt &= (SND_SOC_DAIFMT_FORMAT_MASK | SND_SOC_DAIFMT_INV_MASK | 171 SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK); 172 173 if (fmt != (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | 174 SND_SOC_DAIFMT_CBC_CFC)) { 175 dev_err(codec_dai->dev, "Invalid DAI format\n"); 176 return -EINVAL; 177 } 178 179 return 0; 180 } 181 182 static int uda1334_mute_stream(struct snd_soc_dai *dai, int mute, int stream) 183 { 184 struct uda1334_priv *uda1334 = snd_soc_component_get_drvdata(dai->component); 185 186 if (uda1334->mute) 187 gpiod_set_value_cansleep(uda1334->mute, mute); 188 189 return 0; 190 } 191 192 #define UDA1334_RATES SNDRV_PCM_RATE_8000_96000 193 194 #define UDA1334_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE) 195 196 static const struct snd_soc_dai_ops uda1334_dai_ops = { 197 .startup = uda1334_startup, 198 .shutdown = uda1334_shutdown, 199 .set_sysclk = uda1334_set_dai_sysclk, 200 .set_fmt = uda1334_set_fmt, 201 .mute_stream = uda1334_mute_stream, 202 }; 203 204 static struct snd_soc_dai_driver uda1334_dai = { 205 .name = "uda1334-hifi", 206 .playback = { 207 .stream_name = "Playback", 208 .channels_min = 2, 209 .channels_max = 2, 210 .rates = UDA1334_RATES, 211 .formats = UDA1334_FORMATS, 212 }, 213 .ops = &uda1334_dai_ops, 214 }; 215 216 static int uda1334_probe(struct snd_soc_component *component) 217 { 218 struct uda1334_priv *uda1334 = snd_soc_component_get_drvdata(component); 219 220 uda1334->rate_constraint.list = &uda1334->rate_constraint_list[0]; 221 uda1334->rate_constraint.count = 222 ARRAY_SIZE(uda1334->rate_constraint_list); 223 224 return 0; 225 } 226 227 static const struct snd_soc_component_driver soc_component_dev_uda1334 = { 228 .probe = uda1334_probe, 229 .controls = uda1334_snd_controls, 230 .num_controls = ARRAY_SIZE(uda1334_snd_controls), 231 .dapm_widgets = uda1334_dapm_widgets, 232 .num_dapm_widgets = ARRAY_SIZE(uda1334_dapm_widgets), 233 .dapm_routes = uda1334_dapm_routes, 234 .num_dapm_routes = ARRAY_SIZE(uda1334_dapm_routes), 235 .idle_bias_on = 1, 236 .use_pmdown_time = 1, 237 .endianness = 1, 238 }; 239 240 static const struct of_device_id uda1334_of_match[] = { 241 { .compatible = "nxp,uda1334" }, 242 { /* sentinel*/ } 243 }; 244 MODULE_DEVICE_TABLE(of, uda1334_of_match); 245 246 static int uda1334_codec_probe(struct platform_device *pdev) 247 { 248 struct uda1334_priv *uda1334; 249 int ret; 250 251 uda1334 = devm_kzalloc(&pdev->dev, sizeof(struct uda1334_priv), 252 GFP_KERNEL); 253 if (!uda1334) 254 return -ENOMEM; 255 256 platform_set_drvdata(pdev, uda1334); 257 258 uda1334->mute = devm_gpiod_get(&pdev->dev, "nxp,mute", GPIOD_OUT_LOW); 259 if (IS_ERR(uda1334->mute)) { 260 ret = PTR_ERR(uda1334->mute); 261 dev_err(&pdev->dev, "Failed to get mute line: %d\n", ret); 262 return ret; 263 } 264 265 uda1334->deemph = devm_gpiod_get(&pdev->dev, "nxp,deemph", GPIOD_OUT_LOW); 266 if (IS_ERR(uda1334->deemph)) { 267 ret = PTR_ERR(uda1334->deemph); 268 dev_err(&pdev->dev, "Failed to get deemph line: %d\n", ret); 269 return ret; 270 } 271 272 ret = devm_snd_soc_register_component(&pdev->dev, 273 &soc_component_dev_uda1334, 274 &uda1334_dai, 1); 275 if (ret < 0) 276 dev_err(&pdev->dev, "Failed to register component: %d\n", ret); 277 278 return ret; 279 } 280 281 static struct platform_driver uda1334_codec_driver = { 282 .probe = uda1334_codec_probe, 283 .driver = { 284 .name = "uda1334-codec", 285 .of_match_table = uda1334_of_match, 286 }, 287 }; 288 module_platform_driver(uda1334_codec_driver); 289 290 MODULE_DESCRIPTION("ASoC UDA1334 driver"); 291 MODULE_AUTHOR("Andra Danciu <andradanciu1997@gmail.com>"); 292 MODULE_ALIAS("platform:uda1334-codec"); 293 MODULE_LICENSE("GPL v2"); 294