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 u64 uda1334_selectable_formats = 197 SND_SOC_POSSIBLE_DAIFMT_I2S | 198 SND_SOC_POSSIBLE_DAIFMT_NB_NF; 199 200 static const struct snd_soc_dai_ops uda1334_dai_ops = { 201 .startup = uda1334_startup, 202 .shutdown = uda1334_shutdown, 203 .set_sysclk = uda1334_set_dai_sysclk, 204 .set_fmt = uda1334_set_fmt, 205 .mute_stream = uda1334_mute_stream, 206 .auto_selectable_formats = &uda1334_selectable_formats, 207 .num_auto_selectable_formats = 1, 208 }; 209 210 static struct snd_soc_dai_driver uda1334_dai = { 211 .name = "uda1334-hifi", 212 .playback = { 213 .stream_name = "Playback", 214 .channels_min = 2, 215 .channels_max = 2, 216 .rates = UDA1334_RATES, 217 .formats = UDA1334_FORMATS, 218 }, 219 .ops = &uda1334_dai_ops, 220 }; 221 222 static int uda1334_probe(struct snd_soc_component *component) 223 { 224 struct uda1334_priv *uda1334 = snd_soc_component_get_drvdata(component); 225 226 uda1334->rate_constraint.list = &uda1334->rate_constraint_list[0]; 227 uda1334->rate_constraint.count = 228 ARRAY_SIZE(uda1334->rate_constraint_list); 229 230 return 0; 231 } 232 233 static const struct snd_soc_component_driver soc_component_dev_uda1334 = { 234 .probe = uda1334_probe, 235 .controls = uda1334_snd_controls, 236 .num_controls = ARRAY_SIZE(uda1334_snd_controls), 237 .dapm_widgets = uda1334_dapm_widgets, 238 .num_dapm_widgets = ARRAY_SIZE(uda1334_dapm_widgets), 239 .dapm_routes = uda1334_dapm_routes, 240 .num_dapm_routes = ARRAY_SIZE(uda1334_dapm_routes), 241 .idle_bias_on = 1, 242 .use_pmdown_time = 1, 243 .endianness = 1, 244 }; 245 246 static const struct of_device_id uda1334_of_match[] = { 247 { .compatible = "nxp,uda1334" }, 248 { /* sentinel*/ } 249 }; 250 MODULE_DEVICE_TABLE(of, uda1334_of_match); 251 252 static int uda1334_codec_probe(struct platform_device *pdev) 253 { 254 struct uda1334_priv *uda1334; 255 int ret; 256 257 uda1334 = devm_kzalloc(&pdev->dev, sizeof(struct uda1334_priv), 258 GFP_KERNEL); 259 if (!uda1334) 260 return -ENOMEM; 261 262 platform_set_drvdata(pdev, uda1334); 263 264 uda1334->mute = devm_gpiod_get(&pdev->dev, "nxp,mute", GPIOD_OUT_LOW); 265 if (IS_ERR(uda1334->mute)) { 266 ret = PTR_ERR(uda1334->mute); 267 dev_err(&pdev->dev, "Failed to get mute line: %d\n", ret); 268 return ret; 269 } 270 271 uda1334->deemph = devm_gpiod_get(&pdev->dev, "nxp,deemph", GPIOD_OUT_LOW); 272 if (IS_ERR(uda1334->deemph)) { 273 ret = PTR_ERR(uda1334->deemph); 274 dev_err(&pdev->dev, "Failed to get deemph line: %d\n", ret); 275 return ret; 276 } 277 278 ret = devm_snd_soc_register_component(&pdev->dev, 279 &soc_component_dev_uda1334, 280 &uda1334_dai, 1); 281 if (ret < 0) 282 dev_err(&pdev->dev, "Failed to register component: %d\n", ret); 283 284 return ret; 285 } 286 287 static struct platform_driver uda1334_codec_driver = { 288 .probe = uda1334_codec_probe, 289 .driver = { 290 .name = "uda1334-codec", 291 .of_match_table = uda1334_of_match, 292 }, 293 }; 294 module_platform_driver(uda1334_codec_driver); 295 296 MODULE_DESCRIPTION("ASoC UDA1334 driver"); 297 MODULE_AUTHOR("Andra Danciu <andradanciu1997@gmail.com>"); 298 MODULE_ALIAS("platform:uda1334-codec"); 299 MODULE_LICENSE("GPL v2"); 300