magician.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * SoC audio for HTC Magician
  4. *
  5. * Copyright (c) 2006 Philipp Zabel <[email protected]>
  6. *
  7. * based on spitz.c,
  8. * Authors: Liam Girdwood <[email protected]>
  9. * Richard Purdie <[email protected]>
  10. */
  11. #include <linux/module.h>
  12. #include <linux/timer.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/delay.h>
  16. #include <linux/gpio/consumer.h>
  17. #include <linux/i2c.h>
  18. #include <sound/core.h>
  19. #include <sound/pcm.h>
  20. #include <sound/pcm_params.h>
  21. #include <sound/soc.h>
  22. #include <asm/mach-types.h>
  23. #include "../codecs/uda1380.h"
  24. #include "pxa2xx-i2s.h"
  25. #include "pxa-ssp.h"
  26. #define MAGICIAN_MIC 0
  27. #define MAGICIAN_MIC_EXT 1
  28. static int magician_hp_switch;
  29. static int magician_spk_switch = 1;
  30. static int magician_in_sel = MAGICIAN_MIC;
  31. static struct gpio_desc *gpiod_spk_power, *gpiod_ep_power, *gpiod_mic_power;
  32. static struct gpio_desc *gpiod_in_sel0, *gpiod_in_sel1;
  33. static void magician_ext_control(struct snd_soc_dapm_context *dapm)
  34. {
  35. snd_soc_dapm_mutex_lock(dapm);
  36. if (magician_spk_switch)
  37. snd_soc_dapm_enable_pin_unlocked(dapm, "Speaker");
  38. else
  39. snd_soc_dapm_disable_pin_unlocked(dapm, "Speaker");
  40. if (magician_hp_switch)
  41. snd_soc_dapm_enable_pin_unlocked(dapm, "Headphone Jack");
  42. else
  43. snd_soc_dapm_disable_pin_unlocked(dapm, "Headphone Jack");
  44. switch (magician_in_sel) {
  45. case MAGICIAN_MIC:
  46. snd_soc_dapm_disable_pin_unlocked(dapm, "Headset Mic");
  47. snd_soc_dapm_enable_pin_unlocked(dapm, "Call Mic");
  48. break;
  49. case MAGICIAN_MIC_EXT:
  50. snd_soc_dapm_disable_pin_unlocked(dapm, "Call Mic");
  51. snd_soc_dapm_enable_pin_unlocked(dapm, "Headset Mic");
  52. break;
  53. }
  54. snd_soc_dapm_sync_unlocked(dapm);
  55. snd_soc_dapm_mutex_unlock(dapm);
  56. }
  57. static int magician_startup(struct snd_pcm_substream *substream)
  58. {
  59. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  60. /* check the jack status at stream startup */
  61. magician_ext_control(&rtd->card->dapm);
  62. return 0;
  63. }
  64. /*
  65. * Magician uses SSP port for playback.
  66. */
  67. static int magician_playback_hw_params(struct snd_pcm_substream *substream,
  68. struct snd_pcm_hw_params *params)
  69. {
  70. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  71. struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
  72. struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
  73. unsigned int width;
  74. int ret = 0;
  75. /* set codec DAI configuration */
  76. ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_MSB |
  77. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_BC_FC);
  78. if (ret < 0)
  79. return ret;
  80. /* set cpu DAI configuration */
  81. ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_DSP_A |
  82. SND_SOC_DAIFMT_NB_IF | SND_SOC_DAIFMT_BP_FP);
  83. if (ret < 0)
  84. return ret;
  85. width = snd_pcm_format_physical_width(params_format(params));
  86. ret = snd_soc_dai_set_tdm_slot(cpu_dai, 1, 0, 1, width);
  87. if (ret < 0)
  88. return ret;
  89. /* set audio clock as clock source */
  90. ret = snd_soc_dai_set_sysclk(cpu_dai, PXA_SSP_CLK_AUDIO, 0,
  91. SND_SOC_CLOCK_OUT);
  92. if (ret < 0)
  93. return ret;
  94. return 0;
  95. }
  96. /*
  97. * Magician uses I2S for capture.
  98. */
  99. static int magician_capture_hw_params(struct snd_pcm_substream *substream,
  100. struct snd_pcm_hw_params *params)
  101. {
  102. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  103. struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
  104. struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
  105. int ret = 0;
  106. /* set codec DAI configuration */
  107. ret = snd_soc_dai_set_fmt(codec_dai,
  108. SND_SOC_DAIFMT_MSB | SND_SOC_DAIFMT_NB_NF |
  109. SND_SOC_DAIFMT_BC_FC);
  110. if (ret < 0)
  111. return ret;
  112. /* set cpu DAI configuration */
  113. ret = snd_soc_dai_set_fmt(cpu_dai,
  114. SND_SOC_DAIFMT_MSB | SND_SOC_DAIFMT_NB_NF |
  115. SND_SOC_DAIFMT_BP_FP);
  116. if (ret < 0)
  117. return ret;
  118. /* set the I2S system clock as output */
  119. ret = snd_soc_dai_set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
  120. SND_SOC_CLOCK_OUT);
  121. if (ret < 0)
  122. return ret;
  123. return 0;
  124. }
  125. static const struct snd_soc_ops magician_capture_ops = {
  126. .startup = magician_startup,
  127. .hw_params = magician_capture_hw_params,
  128. };
  129. static const struct snd_soc_ops magician_playback_ops = {
  130. .startup = magician_startup,
  131. .hw_params = magician_playback_hw_params,
  132. };
  133. static int magician_get_hp(struct snd_kcontrol *kcontrol,
  134. struct snd_ctl_elem_value *ucontrol)
  135. {
  136. ucontrol->value.integer.value[0] = magician_hp_switch;
  137. return 0;
  138. }
  139. static int magician_set_hp(struct snd_kcontrol *kcontrol,
  140. struct snd_ctl_elem_value *ucontrol)
  141. {
  142. struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
  143. if (magician_hp_switch == ucontrol->value.integer.value[0])
  144. return 0;
  145. magician_hp_switch = ucontrol->value.integer.value[0];
  146. magician_ext_control(&card->dapm);
  147. return 1;
  148. }
  149. static int magician_get_spk(struct snd_kcontrol *kcontrol,
  150. struct snd_ctl_elem_value *ucontrol)
  151. {
  152. ucontrol->value.integer.value[0] = magician_spk_switch;
  153. return 0;
  154. }
  155. static int magician_set_spk(struct snd_kcontrol *kcontrol,
  156. struct snd_ctl_elem_value *ucontrol)
  157. {
  158. struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
  159. if (magician_spk_switch == ucontrol->value.integer.value[0])
  160. return 0;
  161. magician_spk_switch = ucontrol->value.integer.value[0];
  162. magician_ext_control(&card->dapm);
  163. return 1;
  164. }
  165. static int magician_get_input(struct snd_kcontrol *kcontrol,
  166. struct snd_ctl_elem_value *ucontrol)
  167. {
  168. ucontrol->value.enumerated.item[0] = magician_in_sel;
  169. return 0;
  170. }
  171. static int magician_set_input(struct snd_kcontrol *kcontrol,
  172. struct snd_ctl_elem_value *ucontrol)
  173. {
  174. if (magician_in_sel == ucontrol->value.enumerated.item[0])
  175. return 0;
  176. magician_in_sel = ucontrol->value.enumerated.item[0];
  177. switch (magician_in_sel) {
  178. case MAGICIAN_MIC:
  179. gpiod_set_value(gpiod_in_sel1, 1);
  180. break;
  181. case MAGICIAN_MIC_EXT:
  182. gpiod_set_value(gpiod_in_sel1, 0);
  183. }
  184. return 1;
  185. }
  186. static int magician_spk_power(struct snd_soc_dapm_widget *w,
  187. struct snd_kcontrol *k, int event)
  188. {
  189. gpiod_set_value(gpiod_spk_power, SND_SOC_DAPM_EVENT_ON(event));
  190. return 0;
  191. }
  192. static int magician_hp_power(struct snd_soc_dapm_widget *w,
  193. struct snd_kcontrol *k, int event)
  194. {
  195. gpiod_set_value(gpiod_ep_power, SND_SOC_DAPM_EVENT_ON(event));
  196. return 0;
  197. }
  198. static int magician_mic_bias(struct snd_soc_dapm_widget *w,
  199. struct snd_kcontrol *k, int event)
  200. {
  201. gpiod_set_value(gpiod_mic_power, SND_SOC_DAPM_EVENT_ON(event));
  202. return 0;
  203. }
  204. /* magician machine dapm widgets */
  205. static const struct snd_soc_dapm_widget uda1380_dapm_widgets[] = {
  206. SND_SOC_DAPM_HP("Headphone Jack", magician_hp_power),
  207. SND_SOC_DAPM_SPK("Speaker", magician_spk_power),
  208. SND_SOC_DAPM_MIC("Call Mic", magician_mic_bias),
  209. SND_SOC_DAPM_MIC("Headset Mic", magician_mic_bias),
  210. };
  211. /* magician machine audio_map */
  212. static const struct snd_soc_dapm_route audio_map[] = {
  213. /* Headphone connected to VOUTL, VOUTR */
  214. {"Headphone Jack", NULL, "VOUTL"},
  215. {"Headphone Jack", NULL, "VOUTR"},
  216. /* Speaker connected to VOUTL, VOUTR */
  217. {"Speaker", NULL, "VOUTL"},
  218. {"Speaker", NULL, "VOUTR"},
  219. /* Mics are connected to VINM */
  220. {"VINM", NULL, "Headset Mic"},
  221. {"VINM", NULL, "Call Mic"},
  222. };
  223. static const char * const input_select[] = {"Call Mic", "Headset Mic"};
  224. static const struct soc_enum magician_in_sel_enum =
  225. SOC_ENUM_SINGLE_EXT(2, input_select);
  226. static const struct snd_kcontrol_new uda1380_magician_controls[] = {
  227. SOC_SINGLE_BOOL_EXT("Headphone Switch",
  228. (unsigned long)&magician_hp_switch,
  229. magician_get_hp, magician_set_hp),
  230. SOC_SINGLE_BOOL_EXT("Speaker Switch",
  231. (unsigned long)&magician_spk_switch,
  232. magician_get_spk, magician_set_spk),
  233. SOC_ENUM_EXT("Input Select", magician_in_sel_enum,
  234. magician_get_input, magician_set_input),
  235. };
  236. /* magician digital audio interface glue - connects codec <--> CPU */
  237. SND_SOC_DAILINK_DEFS(playback,
  238. DAILINK_COMP_ARRAY(COMP_CPU("pxa-ssp-dai.0")),
  239. DAILINK_COMP_ARRAY(COMP_CODEC("uda1380-codec.0-0018",
  240. "uda1380-hifi-playback")),
  241. DAILINK_COMP_ARRAY(COMP_PLATFORM("pxa-pcm-audio")));
  242. SND_SOC_DAILINK_DEFS(capture,
  243. DAILINK_COMP_ARRAY(COMP_CPU("pxa2xx-i2s")),
  244. DAILINK_COMP_ARRAY(COMP_CODEC("uda1380-codec.0-0018",
  245. "uda1380-hifi-capture")),
  246. DAILINK_COMP_ARRAY(COMP_PLATFORM("pxa-pcm-audio")));
  247. static struct snd_soc_dai_link magician_dai[] = {
  248. {
  249. .name = "uda1380",
  250. .stream_name = "UDA1380 Playback",
  251. .ops = &magician_playback_ops,
  252. SND_SOC_DAILINK_REG(playback),
  253. },
  254. {
  255. .name = "uda1380",
  256. .stream_name = "UDA1380 Capture",
  257. .ops = &magician_capture_ops,
  258. SND_SOC_DAILINK_REG(capture),
  259. }
  260. };
  261. /* magician audio machine driver */
  262. static struct snd_soc_card snd_soc_card_magician = {
  263. .name = "Magician",
  264. .owner = THIS_MODULE,
  265. .dai_link = magician_dai,
  266. .num_links = ARRAY_SIZE(magician_dai),
  267. .controls = uda1380_magician_controls,
  268. .num_controls = ARRAY_SIZE(uda1380_magician_controls),
  269. .dapm_widgets = uda1380_dapm_widgets,
  270. .num_dapm_widgets = ARRAY_SIZE(uda1380_dapm_widgets),
  271. .dapm_routes = audio_map,
  272. .num_dapm_routes = ARRAY_SIZE(audio_map),
  273. .fully_routed = true,
  274. };
  275. static int magician_audio_probe(struct platform_device *pdev)
  276. {
  277. struct device *dev = &pdev->dev;
  278. gpiod_spk_power = devm_gpiod_get(dev, "SPK_POWER", GPIOD_OUT_LOW);
  279. if (IS_ERR(gpiod_spk_power))
  280. return PTR_ERR(gpiod_spk_power);
  281. gpiod_ep_power = devm_gpiod_get(dev, "EP_POWER", GPIOD_OUT_LOW);
  282. if (IS_ERR(gpiod_ep_power))
  283. return PTR_ERR(gpiod_ep_power);
  284. gpiod_mic_power = devm_gpiod_get(dev, "MIC_POWER", GPIOD_OUT_LOW);
  285. if (IS_ERR(gpiod_mic_power))
  286. return PTR_ERR(gpiod_mic_power);
  287. gpiod_in_sel0 = devm_gpiod_get(dev, "IN_SEL0", GPIOD_OUT_HIGH);
  288. if (IS_ERR(gpiod_in_sel0))
  289. return PTR_ERR(gpiod_in_sel0);
  290. gpiod_in_sel1 = devm_gpiod_get(dev, "IN_SEL1", GPIOD_OUT_LOW);
  291. if (IS_ERR(gpiod_in_sel1))
  292. return PTR_ERR(gpiod_in_sel1);
  293. snd_soc_card_magician.dev = &pdev->dev;
  294. return devm_snd_soc_register_card(&pdev->dev, &snd_soc_card_magician);
  295. }
  296. static struct platform_driver magician_audio_driver = {
  297. .driver.name = "magician-audio",
  298. .driver.pm = &snd_soc_pm_ops,
  299. .probe = magician_audio_probe,
  300. };
  301. module_platform_driver(magician_audio_driver);
  302. MODULE_AUTHOR("Philipp Zabel");
  303. MODULE_DESCRIPTION("ALSA SoC Magician");
  304. MODULE_LICENSE("GPL");
  305. MODULE_ALIAS("platform:magician-audio");