tse850-pcm5142.c 13 KB

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