spitz.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * spitz.c -- SoC audio for Sharp SL-Cxx00 models Spitz, Borzoi and Akita
  4. *
  5. * Copyright 2005 Wolfson Microelectronics PLC.
  6. * Copyright 2005 Openedhand Ltd.
  7. *
  8. * Authors: Liam Girdwood <[email protected]>
  9. * Richard Purdie <[email protected]>
  10. */
  11. #include <linux/module.h>
  12. #include <linux/moduleparam.h>
  13. #include <linux/timer.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/gpio/consumer.h>
  17. #include <sound/core.h>
  18. #include <sound/pcm.h>
  19. #include <sound/soc.h>
  20. #include <asm/mach-types.h>
  21. #include "../codecs/wm8750.h"
  22. #include "pxa2xx-i2s.h"
  23. #define SPITZ_HP 0
  24. #define SPITZ_MIC 1
  25. #define SPITZ_LINE 2
  26. #define SPITZ_HEADSET 3
  27. #define SPITZ_HP_OFF 4
  28. #define SPITZ_SPK_ON 0
  29. #define SPITZ_SPK_OFF 1
  30. /* audio clock in Hz - rounded from 12.235MHz */
  31. #define SPITZ_AUDIO_CLOCK 12288000
  32. static int spitz_jack_func;
  33. static int spitz_spk_func;
  34. static struct gpio_desc *gpiod_mic, *gpiod_mute_l, *gpiod_mute_r;
  35. static void spitz_ext_control(struct snd_soc_dapm_context *dapm)
  36. {
  37. snd_soc_dapm_mutex_lock(dapm);
  38. if (spitz_spk_func == SPITZ_SPK_ON)
  39. snd_soc_dapm_enable_pin_unlocked(dapm, "Ext Spk");
  40. else
  41. snd_soc_dapm_disable_pin_unlocked(dapm, "Ext Spk");
  42. /* set up jack connection */
  43. switch (spitz_jack_func) {
  44. case SPITZ_HP:
  45. /* enable and unmute hp jack, disable mic bias */
  46. snd_soc_dapm_disable_pin_unlocked(dapm, "Headset Jack");
  47. snd_soc_dapm_disable_pin_unlocked(dapm, "Mic Jack");
  48. snd_soc_dapm_disable_pin_unlocked(dapm, "Line Jack");
  49. snd_soc_dapm_enable_pin_unlocked(dapm, "Headphone Jack");
  50. gpiod_set_value(gpiod_mute_l, 1);
  51. gpiod_set_value(gpiod_mute_r, 1);
  52. break;
  53. case SPITZ_MIC:
  54. /* enable mic jack and bias, mute hp */
  55. snd_soc_dapm_disable_pin_unlocked(dapm, "Headphone Jack");
  56. snd_soc_dapm_disable_pin_unlocked(dapm, "Headset Jack");
  57. snd_soc_dapm_disable_pin_unlocked(dapm, "Line Jack");
  58. snd_soc_dapm_enable_pin_unlocked(dapm, "Mic Jack");
  59. gpiod_set_value(gpiod_mute_l, 0);
  60. gpiod_set_value(gpiod_mute_r, 0);
  61. break;
  62. case SPITZ_LINE:
  63. /* enable line jack, disable mic bias and mute hp */
  64. snd_soc_dapm_disable_pin_unlocked(dapm, "Headphone Jack");
  65. snd_soc_dapm_disable_pin_unlocked(dapm, "Headset Jack");
  66. snd_soc_dapm_disable_pin_unlocked(dapm, "Mic Jack");
  67. snd_soc_dapm_enable_pin_unlocked(dapm, "Line Jack");
  68. gpiod_set_value(gpiod_mute_l, 0);
  69. gpiod_set_value(gpiod_mute_r, 0);
  70. break;
  71. case SPITZ_HEADSET:
  72. /* enable and unmute headset jack enable mic bias, mute L hp */
  73. snd_soc_dapm_disable_pin_unlocked(dapm, "Headphone Jack");
  74. snd_soc_dapm_enable_pin_unlocked(dapm, "Mic Jack");
  75. snd_soc_dapm_disable_pin_unlocked(dapm, "Line Jack");
  76. snd_soc_dapm_enable_pin_unlocked(dapm, "Headset Jack");
  77. gpiod_set_value(gpiod_mute_l, 0);
  78. gpiod_set_value(gpiod_mute_r, 1);
  79. break;
  80. case SPITZ_HP_OFF:
  81. /* jack removed, everything off */
  82. snd_soc_dapm_disable_pin_unlocked(dapm, "Headphone Jack");
  83. snd_soc_dapm_disable_pin_unlocked(dapm, "Headset Jack");
  84. snd_soc_dapm_disable_pin_unlocked(dapm, "Mic Jack");
  85. snd_soc_dapm_disable_pin_unlocked(dapm, "Line Jack");
  86. gpiod_set_value(gpiod_mute_l, 0);
  87. gpiod_set_value(gpiod_mute_r, 0);
  88. break;
  89. }
  90. snd_soc_dapm_sync_unlocked(dapm);
  91. snd_soc_dapm_mutex_unlock(dapm);
  92. }
  93. static int spitz_startup(struct snd_pcm_substream *substream)
  94. {
  95. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  96. /* check the jack status at stream startup */
  97. spitz_ext_control(&rtd->card->dapm);
  98. return 0;
  99. }
  100. static int spitz_hw_params(struct snd_pcm_substream *substream,
  101. struct snd_pcm_hw_params *params)
  102. {
  103. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  104. struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
  105. struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
  106. unsigned int clk = 0;
  107. int ret = 0;
  108. switch (params_rate(params)) {
  109. case 8000:
  110. case 16000:
  111. case 48000:
  112. case 96000:
  113. clk = 12288000;
  114. break;
  115. case 11025:
  116. case 22050:
  117. case 44100:
  118. clk = 11289600;
  119. break;
  120. }
  121. /* set the codec system clock for DAC and ADC */
  122. ret = snd_soc_dai_set_sysclk(codec_dai, WM8750_SYSCLK, clk,
  123. SND_SOC_CLOCK_IN);
  124. if (ret < 0)
  125. return ret;
  126. /* set the I2S system clock as input (unused) */
  127. ret = snd_soc_dai_set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
  128. SND_SOC_CLOCK_IN);
  129. if (ret < 0)
  130. return ret;
  131. return 0;
  132. }
  133. static const struct snd_soc_ops spitz_ops = {
  134. .startup = spitz_startup,
  135. .hw_params = spitz_hw_params,
  136. };
  137. static int spitz_get_jack(struct snd_kcontrol *kcontrol,
  138. struct snd_ctl_elem_value *ucontrol)
  139. {
  140. ucontrol->value.enumerated.item[0] = spitz_jack_func;
  141. return 0;
  142. }
  143. static int spitz_set_jack(struct snd_kcontrol *kcontrol,
  144. struct snd_ctl_elem_value *ucontrol)
  145. {
  146. struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
  147. if (spitz_jack_func == ucontrol->value.enumerated.item[0])
  148. return 0;
  149. spitz_jack_func = ucontrol->value.enumerated.item[0];
  150. spitz_ext_control(&card->dapm);
  151. return 1;
  152. }
  153. static int spitz_get_spk(struct snd_kcontrol *kcontrol,
  154. struct snd_ctl_elem_value *ucontrol)
  155. {
  156. ucontrol->value.enumerated.item[0] = spitz_spk_func;
  157. return 0;
  158. }
  159. static int spitz_set_spk(struct snd_kcontrol *kcontrol,
  160. struct snd_ctl_elem_value *ucontrol)
  161. {
  162. struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
  163. if (spitz_spk_func == ucontrol->value.enumerated.item[0])
  164. return 0;
  165. spitz_spk_func = ucontrol->value.enumerated.item[0];
  166. spitz_ext_control(&card->dapm);
  167. return 1;
  168. }
  169. static int spitz_mic_bias(struct snd_soc_dapm_widget *w,
  170. struct snd_kcontrol *k, int event)
  171. {
  172. gpiod_set_value_cansleep(gpiod_mic, SND_SOC_DAPM_EVENT_ON(event));
  173. return 0;
  174. }
  175. /* spitz machine dapm widgets */
  176. static const struct snd_soc_dapm_widget wm8750_dapm_widgets[] = {
  177. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  178. SND_SOC_DAPM_MIC("Mic Jack", spitz_mic_bias),
  179. SND_SOC_DAPM_SPK("Ext Spk", NULL),
  180. SND_SOC_DAPM_LINE("Line Jack", NULL),
  181. /* headset is a mic and mono headphone */
  182. SND_SOC_DAPM_HP("Headset Jack", NULL),
  183. };
  184. /* Spitz machine audio_map */
  185. static const struct snd_soc_dapm_route spitz_audio_map[] = {
  186. /* headphone connected to LOUT1, ROUT1 */
  187. {"Headphone Jack", NULL, "LOUT1"},
  188. {"Headphone Jack", NULL, "ROUT1"},
  189. /* headset connected to ROUT1 and LINPUT1 with bias (def below) */
  190. {"Headset Jack", NULL, "ROUT1"},
  191. /* ext speaker connected to LOUT2, ROUT2 */
  192. {"Ext Spk", NULL, "ROUT2"},
  193. {"Ext Spk", NULL, "LOUT2"},
  194. /* mic is connected to input 1 - with bias */
  195. {"LINPUT1", NULL, "Mic Bias"},
  196. {"Mic Bias", NULL, "Mic Jack"},
  197. /* line is connected to input 1 - no bias */
  198. {"LINPUT1", NULL, "Line Jack"},
  199. };
  200. static const char * const jack_function[] = {"Headphone", "Mic", "Line",
  201. "Headset", "Off"};
  202. static const char * const spk_function[] = {"On", "Off"};
  203. static const struct soc_enum spitz_enum[] = {
  204. SOC_ENUM_SINGLE_EXT(5, jack_function),
  205. SOC_ENUM_SINGLE_EXT(2, spk_function),
  206. };
  207. static const struct snd_kcontrol_new wm8750_spitz_controls[] = {
  208. SOC_ENUM_EXT("Jack Function", spitz_enum[0], spitz_get_jack,
  209. spitz_set_jack),
  210. SOC_ENUM_EXT("Speaker Function", spitz_enum[1], spitz_get_spk,
  211. spitz_set_spk),
  212. };
  213. /* spitz digital audio interface glue - connects codec <--> CPU */
  214. SND_SOC_DAILINK_DEFS(wm8750,
  215. DAILINK_COMP_ARRAY(COMP_CPU("pxa2xx-i2s")),
  216. DAILINK_COMP_ARRAY(COMP_CODEC("wm8750.0-001b", "wm8750-hifi")),
  217. DAILINK_COMP_ARRAY(COMP_PLATFORM("pxa-pcm-audio")));
  218. static struct snd_soc_dai_link spitz_dai = {
  219. .name = "wm8750",
  220. .stream_name = "WM8750",
  221. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  222. SND_SOC_DAIFMT_CBS_CFS,
  223. .ops = &spitz_ops,
  224. SND_SOC_DAILINK_REG(wm8750),
  225. };
  226. /* spitz audio machine driver */
  227. static struct snd_soc_card snd_soc_spitz = {
  228. .name = "Spitz",
  229. .owner = THIS_MODULE,
  230. .dai_link = &spitz_dai,
  231. .num_links = 1,
  232. .controls = wm8750_spitz_controls,
  233. .num_controls = ARRAY_SIZE(wm8750_spitz_controls),
  234. .dapm_widgets = wm8750_dapm_widgets,
  235. .num_dapm_widgets = ARRAY_SIZE(wm8750_dapm_widgets),
  236. .dapm_routes = spitz_audio_map,
  237. .num_dapm_routes = ARRAY_SIZE(spitz_audio_map),
  238. .fully_routed = true,
  239. };
  240. static int spitz_probe(struct platform_device *pdev)
  241. {
  242. struct snd_soc_card *card = &snd_soc_spitz;
  243. int ret;
  244. gpiod_mic = devm_gpiod_get(&pdev->dev, "mic", GPIOD_OUT_LOW);
  245. if (IS_ERR(gpiod_mic))
  246. return PTR_ERR(gpiod_mic);
  247. gpiod_mute_l = devm_gpiod_get(&pdev->dev, "mute-l", GPIOD_OUT_LOW);
  248. if (IS_ERR(gpiod_mute_l))
  249. return PTR_ERR(gpiod_mute_l);
  250. gpiod_mute_r = devm_gpiod_get(&pdev->dev, "mute-r", GPIOD_OUT_LOW);
  251. if (IS_ERR(gpiod_mute_r))
  252. return PTR_ERR(gpiod_mute_r);
  253. card->dev = &pdev->dev;
  254. ret = devm_snd_soc_register_card(&pdev->dev, card);
  255. if (ret)
  256. dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
  257. ret);
  258. return ret;
  259. }
  260. static int spitz_remove(struct platform_device *pdev)
  261. {
  262. return 0;
  263. }
  264. static struct platform_driver spitz_driver = {
  265. .driver = {
  266. .name = "spitz-audio",
  267. .pm = &snd_soc_pm_ops,
  268. },
  269. .probe = spitz_probe,
  270. .remove = spitz_remove,
  271. };
  272. module_platform_driver(spitz_driver);
  273. MODULE_AUTHOR("Richard Purdie");
  274. MODULE_DESCRIPTION("ALSA SoC Spitz");
  275. MODULE_LICENSE("GPL");
  276. MODULE_ALIAS("platform:spitz-audio");