1 // SPDX-License-Identifier: GPL-2.0 2 // 3 // TSE-850 audio - ASoC driver for the Axentia TSE-850 with a PCM5142 codec 4 // 5 // Copyright (C) 2016 Axentia Technologies AB 6 // 7 // Author: Peter Rosin <peda@axentia.se> 8 // 9 // loop1 relays 10 // IN1 +---o +------------+ o---+ OUT1 11 // \ / 12 // + + 13 // | / | 14 // +--o +--. | 15 // | add | | 16 // | V | 17 // | .---. | 18 // DAC +----------->|Sum|---+ 19 // | '---' | 20 // | | 21 // + + 22 // 23 // IN2 +---o--+------------+--o---+ OUT2 24 // loop2 relays 25 // 26 // The 'loop1' gpio pin controls two relays, which are either in loop 27 // position, meaning that input and output are directly connected, or 28 // they are in mixer position, meaning that the signal is passed through 29 // the 'Sum' mixer. Similarly for 'loop2'. 30 // 31 // In the above, the 'loop1' relays are inactive, thus feeding IN1 to the 32 // mixer (if 'add' is active) and feeding the mixer output to OUT1. The 33 // 'loop2' relays are active, short-cutting the TSE-850 from channel 2. 34 // IN1, IN2, OUT1 and OUT2 are TSE-850 connectors and DAC is the PCB name 35 // of the (filtered) output from the PCM5142 codec. 36 37 #include <linux/clk.h> 38 #include <linux/gpio/consumer.h> 39 #include <linux/module.h> 40 #include <linux/of.h> 41 #include <linux/regulator/consumer.h> 42 43 #include <sound/soc.h> 44 #include <sound/pcm_params.h> 45 46 struct tse850_priv { 47 struct gpio_desc *add; 48 struct gpio_desc *loop1; 49 struct gpio_desc *loop2; 50 51 struct regulator *ana; 52 53 int add_cache; 54 int loop1_cache; 55 int loop2_cache; 56 }; 57 58 static int tse850_get_mux1(struct snd_kcontrol *kctrl, 59 struct snd_ctl_elem_value *ucontrol) 60 { 61 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kctrl); 62 struct snd_soc_card *card = dapm->card; 63 struct tse850_priv *tse850 = snd_soc_card_get_drvdata(card); 64 65 ucontrol->value.enumerated.item[0] = tse850->loop1_cache; 66 67 return 0; 68 } 69 70 static int tse850_put_mux1(struct snd_kcontrol *kctrl, 71 struct snd_ctl_elem_value *ucontrol) 72 { 73 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kctrl); 74 struct snd_soc_card *card = dapm->card; 75 struct tse850_priv *tse850 = snd_soc_card_get_drvdata(card); 76 struct soc_enum *e = (struct soc_enum *)kctrl->private_value; 77 unsigned int val = ucontrol->value.enumerated.item[0]; 78 79 if (val >= e->items) 80 return -EINVAL; 81 82 gpiod_set_value_cansleep(tse850->loop1, val); 83 tse850->loop1_cache = val; 84 85 return snd_soc_dapm_put_enum_double(kctrl, ucontrol); 86 } 87 88 static int tse850_get_mux2(struct snd_kcontrol *kctrl, 89 struct snd_ctl_elem_value *ucontrol) 90 { 91 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kctrl); 92 struct snd_soc_card *card = dapm->card; 93 struct tse850_priv *tse850 = snd_soc_card_get_drvdata(card); 94 95 ucontrol->value.enumerated.item[0] = tse850->loop2_cache; 96 97 return 0; 98 } 99 100 static int tse850_put_mux2(struct snd_kcontrol *kctrl, 101 struct snd_ctl_elem_value *ucontrol) 102 { 103 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kctrl); 104 struct snd_soc_card *card = dapm->card; 105 struct tse850_priv *tse850 = snd_soc_card_get_drvdata(card); 106 struct soc_enum *e = (struct soc_enum *)kctrl->private_value; 107 unsigned int val = ucontrol->value.enumerated.item[0]; 108 109 if (val >= e->items) 110 return -EINVAL; 111 112 gpiod_set_value_cansleep(tse850->loop2, val); 113 tse850->loop2_cache = val; 114 115 return snd_soc_dapm_put_enum_double(kctrl, ucontrol); 116 } 117 118 static int tse850_get_mix(struct snd_kcontrol *kctrl, 119 struct snd_ctl_elem_value *ucontrol) 120 { 121 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kctrl); 122 struct snd_soc_card *card = dapm->card; 123 struct tse850_priv *tse850 = snd_soc_card_get_drvdata(card); 124 125 ucontrol->value.enumerated.item[0] = tse850->add_cache; 126 127 return 0; 128 } 129 130 static int tse850_put_mix(struct snd_kcontrol *kctrl, 131 struct snd_ctl_elem_value *ucontrol) 132 { 133 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kctrl); 134 struct snd_soc_card *card = dapm->card; 135 struct tse850_priv *tse850 = snd_soc_card_get_drvdata(card); 136 int connect = !!ucontrol->value.integer.value[0]; 137 138 if (tse850->add_cache == connect) 139 return 0; 140 141 /* 142 * Hmmm, this gpiod_set_value_cansleep call should probably happen 143 * inside snd_soc_dapm_mixer_update_power in the loop. 144 */ 145 gpiod_set_value_cansleep(tse850->add, connect); 146 tse850->add_cache = connect; 147 148 snd_soc_dapm_mixer_update_power(dapm, kctrl, connect, NULL); 149 return 1; 150 } 151 152 static int tse850_get_ana(struct snd_kcontrol *kctrl, 153 struct snd_ctl_elem_value *ucontrol) 154 { 155 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kctrl); 156 struct snd_soc_card *card = dapm->card; 157 struct tse850_priv *tse850 = snd_soc_card_get_drvdata(card); 158 int ret; 159 160 ret = regulator_get_voltage(tse850->ana); 161 if (ret < 0) 162 return ret; 163 164 /* 165 * Map regulator output values like so: 166 * -11.5V to "Low" (enum 0) 167 * 11.5V-12.5V to "12V" (enum 1) 168 * 12.5V-13.5V to "13V" (enum 2) 169 * ... 170 * 18.5V-19.5V to "19V" (enum 8) 171 * 19.5V- to "20V" (enum 9) 172 */ 173 if (ret < 11000000) 174 ret = 11000000; 175 else if (ret > 20000000) 176 ret = 20000000; 177 ret -= 11000000; 178 ret = (ret + 500000) / 1000000; 179 180 ucontrol->value.enumerated.item[0] = ret; 181 182 return 0; 183 } 184 185 static int tse850_put_ana(struct snd_kcontrol *kctrl, 186 struct snd_ctl_elem_value *ucontrol) 187 { 188 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kctrl); 189 struct snd_soc_card *card = dapm->card; 190 struct tse850_priv *tse850 = snd_soc_card_get_drvdata(card); 191 struct soc_enum *e = (struct soc_enum *)kctrl->private_value; 192 unsigned int uV = ucontrol->value.enumerated.item[0]; 193 int ret; 194 195 if (uV >= e->items) 196 return -EINVAL; 197 198 /* 199 * Map enum zero (Low) to 2 volts on the regulator, do this since 200 * the ana regulator is supplied by the system 12V voltage and 201 * requesting anything below the system voltage causes the system 202 * voltage to be passed through the regulator. Also, the ana 203 * regulator induces noise when requesting voltages near the 204 * system voltage. So, by mapping Low to 2V, that noise is 205 * eliminated when all that is needed is 12V (the system voltage). 206 */ 207 if (uV) 208 uV = 11000000 + (1000000 * uV); 209 else 210 uV = 2000000; 211 212 ret = regulator_set_voltage(tse850->ana, uV, uV); 213 if (ret < 0) 214 return ret; 215 216 return snd_soc_dapm_put_enum_double(kctrl, ucontrol); 217 } 218 219 static const char * const mux_text[] = { "Mixer", "Loop" }; 220 221 static const struct soc_enum mux_enum = 222 SOC_ENUM_SINGLE(SND_SOC_NOPM, 0, ARRAY_SIZE(mux_text), mux_text); 223 224 static const struct snd_kcontrol_new mux1 = 225 SOC_DAPM_ENUM_EXT("MUX1", mux_enum, tse850_get_mux1, tse850_put_mux1); 226 227 static const struct snd_kcontrol_new mux2 = 228 SOC_DAPM_ENUM_EXT("MUX2", mux_enum, tse850_get_mux2, tse850_put_mux2); 229 230 #define TSE850_DAPM_SINGLE_EXT(xname, reg, shift, max, invert, xget, xput) \ 231 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \ 232 .info = snd_soc_info_volsw, \ 233 .get = xget, \ 234 .put = xput, \ 235 .private_value = SOC_SINGLE_VALUE(reg, shift, max, invert, 0) } 236 237 static const struct snd_kcontrol_new mix[] = { 238 TSE850_DAPM_SINGLE_EXT("IN Switch", SND_SOC_NOPM, 0, 1, 0, 239 tse850_get_mix, tse850_put_mix), 240 }; 241 242 static const char * const ana_text[] = { 243 "Low", "12V", "13V", "14V", "15V", "16V", "17V", "18V", "19V", "20V" 244 }; 245 246 static const struct soc_enum ana_enum = 247 SOC_ENUM_SINGLE(SND_SOC_NOPM, 0, ARRAY_SIZE(ana_text), ana_text); 248 249 static const struct snd_kcontrol_new out = 250 SOC_DAPM_ENUM_EXT("ANA", ana_enum, tse850_get_ana, tse850_put_ana); 251 252 static const struct snd_soc_dapm_widget tse850_dapm_widgets[] = { 253 SND_SOC_DAPM_LINE("OUT1", NULL), 254 SND_SOC_DAPM_LINE("OUT2", NULL), 255 SND_SOC_DAPM_LINE("IN1", NULL), 256 SND_SOC_DAPM_LINE("IN2", NULL), 257 SND_SOC_DAPM_INPUT("DAC"), 258 SND_SOC_DAPM_AIF_IN("AIFINL", "Playback", 0, SND_SOC_NOPM, 0, 0), 259 SND_SOC_DAPM_AIF_IN("AIFINR", "Playback", 1, SND_SOC_NOPM, 0, 0), 260 SOC_MIXER_ARRAY("MIX", SND_SOC_NOPM, 0, 0, mix), 261 SND_SOC_DAPM_MUX("MUX1", SND_SOC_NOPM, 0, 0, &mux1), 262 SND_SOC_DAPM_MUX("MUX2", SND_SOC_NOPM, 0, 0, &mux2), 263 SND_SOC_DAPM_OUT_DRV("OUT", SND_SOC_NOPM, 0, 0, &out, 1), 264 }; 265 266 /* 267 * These connections are not entirely correct, since both IN1 and IN2 268 * are always fed to MIX (if the "IN switch" is set so), i.e. without 269 * regard to the loop1 and loop2 relays that according to this only 270 * control MUX1 and MUX2 but in fact also control how the input signals 271 * are routed. 272 * But, 1) I don't know how to do it right, and 2) it doesn't seem to 273 * matter in practice since nothing is powered in those sections anyway. 274 */ 275 static const struct snd_soc_dapm_route tse850_intercon[] = { 276 { "OUT1", NULL, "MUX1" }, 277 { "OUT2", NULL, "MUX2" }, 278 279 { "MUX1", "Loop", "IN1" }, 280 { "MUX1", "Mixer", "OUT" }, 281 282 { "MUX2", "Loop", "IN2" }, 283 { "MUX2", "Mixer", "OUT" }, 284 285 { "OUT", NULL, "MIX" }, 286 287 { "MIX", NULL, "DAC" }, 288 { "MIX", "IN Switch", "IN1" }, 289 { "MIX", "IN Switch", "IN2" }, 290 291 /* connect board input to the codec left channel output pin */ 292 { "DAC", NULL, "OUTL" }, 293 }; 294 295 SND_SOC_DAILINK_DEFS(pcm, 296 DAILINK_COMP_ARRAY(COMP_EMPTY()), 297 DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "pcm512x-hifi")), 298 DAILINK_COMP_ARRAY(COMP_EMPTY())); 299 300 static struct snd_soc_dai_link tse850_dailink = { 301 .name = "TSE-850", 302 .stream_name = "TSE-850-PCM", 303 .dai_fmt = SND_SOC_DAIFMT_I2S 304 | SND_SOC_DAIFMT_NB_NF 305 | SND_SOC_DAIFMT_CBP_CFC, 306 SND_SOC_DAILINK_REG(pcm), 307 }; 308 309 static struct snd_soc_card tse850_card = { 310 .name = "TSE-850-ASoC", 311 .owner = THIS_MODULE, 312 .dai_link = &tse850_dailink, 313 .num_links = 1, 314 .dapm_widgets = tse850_dapm_widgets, 315 .num_dapm_widgets = ARRAY_SIZE(tse850_dapm_widgets), 316 .dapm_routes = tse850_intercon, 317 .num_dapm_routes = ARRAY_SIZE(tse850_intercon), 318 .fully_routed = true, 319 }; 320 321 static int tse850_dt_init(struct platform_device *pdev) 322 { 323 struct device_node *np = pdev->dev.of_node; 324 struct device_node *codec_np, *cpu_np; 325 struct snd_soc_dai_link *dailink = &tse850_dailink; 326 327 if (!np) { 328 dev_err(&pdev->dev, "only device tree supported\n"); 329 return -EINVAL; 330 } 331 332 cpu_np = of_parse_phandle(np, "axentia,cpu-dai", 0); 333 if (!cpu_np) { 334 dev_err(&pdev->dev, "failed to get cpu dai\n"); 335 return -EINVAL; 336 } 337 dailink->cpus->of_node = cpu_np; 338 dailink->platforms->of_node = cpu_np; 339 of_node_put(cpu_np); 340 341 codec_np = of_parse_phandle(np, "axentia,audio-codec", 0); 342 if (!codec_np) { 343 dev_err(&pdev->dev, "failed to get codec info\n"); 344 return -EINVAL; 345 } 346 dailink->codecs->of_node = codec_np; 347 of_node_put(codec_np); 348 349 return 0; 350 } 351 352 static int tse850_probe(struct platform_device *pdev) 353 { 354 struct snd_soc_card *card = &tse850_card; 355 struct device *dev = card->dev = &pdev->dev; 356 struct tse850_priv *tse850; 357 int ret; 358 359 tse850 = devm_kzalloc(dev, sizeof(*tse850), GFP_KERNEL); 360 if (!tse850) 361 return -ENOMEM; 362 363 snd_soc_card_set_drvdata(card, tse850); 364 365 ret = tse850_dt_init(pdev); 366 if (ret) { 367 dev_err(dev, "failed to init dt info\n"); 368 return ret; 369 } 370 371 tse850->add = devm_gpiod_get(dev, "axentia,add", GPIOD_OUT_HIGH); 372 if (IS_ERR(tse850->add)) 373 return dev_err_probe(dev, PTR_ERR(tse850->add), 374 "failed to get 'add' gpio\n"); 375 tse850->add_cache = 1; 376 377 tse850->loop1 = devm_gpiod_get(dev, "axentia,loop1", GPIOD_OUT_HIGH); 378 if (IS_ERR(tse850->loop1)) 379 return dev_err_probe(dev, PTR_ERR(tse850->loop1), 380 "failed to get 'loop1' gpio\n"); 381 tse850->loop1_cache = 1; 382 383 tse850->loop2 = devm_gpiod_get(dev, "axentia,loop2", GPIOD_OUT_HIGH); 384 if (IS_ERR(tse850->loop2)) 385 return dev_err_probe(dev, PTR_ERR(tse850->loop2), 386 "failed to get 'loop2' gpio\n"); 387 tse850->loop2_cache = 1; 388 389 tse850->ana = devm_regulator_get(dev, "axentia,ana"); 390 if (IS_ERR(tse850->ana)) 391 return dev_err_probe(dev, PTR_ERR(tse850->ana), 392 "failed to get 'ana' regulator\n"); 393 394 ret = regulator_enable(tse850->ana); 395 if (ret < 0) { 396 dev_err(dev, "failed to enable the 'ana' regulator\n"); 397 return ret; 398 } 399 400 ret = snd_soc_register_card(card); 401 if (ret) { 402 dev_err(dev, "snd_soc_register_card failed\n"); 403 goto err_disable_ana; 404 } 405 406 return 0; 407 408 err_disable_ana: 409 regulator_disable(tse850->ana); 410 return ret; 411 } 412 413 static void tse850_remove(struct platform_device *pdev) 414 { 415 struct snd_soc_card *card = platform_get_drvdata(pdev); 416 struct tse850_priv *tse850 = snd_soc_card_get_drvdata(card); 417 418 snd_soc_unregister_card(card); 419 regulator_disable(tse850->ana); 420 } 421 422 static const struct of_device_id tse850_dt_ids[] = { 423 { .compatible = "axentia,tse850-pcm5142", }, 424 { /* sentinel */ } 425 }; 426 MODULE_DEVICE_TABLE(of, tse850_dt_ids); 427 428 static struct platform_driver tse850_driver = { 429 .driver = { 430 .name = "axentia-tse850-pcm5142", 431 .of_match_table = tse850_dt_ids, 432 }, 433 .probe = tse850_probe, 434 .remove = tse850_remove, 435 }; 436 437 module_platform_driver(tse850_driver); 438 439 /* Module information */ 440 MODULE_AUTHOR("Peter Rosin <peda@axentia.se>"); 441 MODULE_DESCRIPTION("ALSA SoC driver for TSE-850 with PCM5142 codec"); 442 MODULE_LICENSE("GPL v2"); 443