poodle.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * poodle.c -- SoC audio for Poodle
  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/i2c.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/platform_device.h>
  17. #include <sound/core.h>
  18. #include <sound/pcm.h>
  19. #include <sound/soc.h>
  20. #include <asm/mach-types.h>
  21. #include <asm/hardware/locomo.h>
  22. #include <linux/platform_data/asoc-pxa.h>
  23. #include <linux/platform_data/asoc-poodle.h>
  24. #include "../codecs/wm8731.h"
  25. #include "pxa2xx-i2s.h"
  26. #define POODLE_HP 1
  27. #define POODLE_HP_OFF 0
  28. #define POODLE_SPK_ON 1
  29. #define POODLE_SPK_OFF 0
  30. /* audio clock in Hz - rounded from 12.235MHz */
  31. #define POODLE_AUDIO_CLOCK 12288000
  32. static int poodle_jack_func;
  33. static int poodle_spk_func;
  34. static struct poodle_audio_platform_data *poodle_pdata;
  35. static void poodle_ext_control(struct snd_soc_dapm_context *dapm)
  36. {
  37. /* set up jack connection */
  38. if (poodle_jack_func == POODLE_HP) {
  39. /* set = unmute headphone */
  40. locomo_gpio_write(poodle_pdata->locomo_dev,
  41. poodle_pdata->gpio_mute_l, 1);
  42. locomo_gpio_write(poodle_pdata->locomo_dev,
  43. poodle_pdata->gpio_mute_r, 1);
  44. snd_soc_dapm_enable_pin(dapm, "Headphone Jack");
  45. } else {
  46. locomo_gpio_write(poodle_pdata->locomo_dev,
  47. poodle_pdata->gpio_mute_l, 0);
  48. locomo_gpio_write(poodle_pdata->locomo_dev,
  49. poodle_pdata->gpio_mute_r, 0);
  50. snd_soc_dapm_disable_pin(dapm, "Headphone Jack");
  51. }
  52. /* set the endpoints to their new connection states */
  53. if (poodle_spk_func == POODLE_SPK_ON)
  54. snd_soc_dapm_enable_pin(dapm, "Ext Spk");
  55. else
  56. snd_soc_dapm_disable_pin(dapm, "Ext Spk");
  57. /* signal a DAPM event */
  58. snd_soc_dapm_sync(dapm);
  59. }
  60. static int poodle_startup(struct snd_pcm_substream *substream)
  61. {
  62. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  63. /* check the jack status at stream startup */
  64. poodle_ext_control(&rtd->card->dapm);
  65. return 0;
  66. }
  67. /* we need to unmute the HP at shutdown as the mute burns power on poodle */
  68. static void poodle_shutdown(struct snd_pcm_substream *substream)
  69. {
  70. /* set = unmute headphone */
  71. locomo_gpio_write(poodle_pdata->locomo_dev,
  72. poodle_pdata->gpio_mute_l, 1);
  73. locomo_gpio_write(poodle_pdata->locomo_dev,
  74. poodle_pdata->gpio_mute_r, 1);
  75. }
  76. static int poodle_hw_params(struct snd_pcm_substream *substream,
  77. struct snd_pcm_hw_params *params)
  78. {
  79. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  80. struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
  81. struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
  82. unsigned int clk = 0;
  83. int ret = 0;
  84. switch (params_rate(params)) {
  85. case 8000:
  86. case 16000:
  87. case 48000:
  88. case 96000:
  89. clk = 12288000;
  90. break;
  91. case 11025:
  92. case 22050:
  93. case 44100:
  94. clk = 11289600;
  95. break;
  96. }
  97. /* set the codec system clock for DAC and ADC */
  98. ret = snd_soc_dai_set_sysclk(codec_dai, WM8731_SYSCLK_XTAL, clk,
  99. SND_SOC_CLOCK_IN);
  100. if (ret < 0)
  101. return ret;
  102. /* set the I2S system clock as input (unused) */
  103. ret = snd_soc_dai_set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
  104. SND_SOC_CLOCK_IN);
  105. if (ret < 0)
  106. return ret;
  107. return 0;
  108. }
  109. static const struct snd_soc_ops poodle_ops = {
  110. .startup = poodle_startup,
  111. .hw_params = poodle_hw_params,
  112. .shutdown = poodle_shutdown,
  113. };
  114. static int poodle_get_jack(struct snd_kcontrol *kcontrol,
  115. struct snd_ctl_elem_value *ucontrol)
  116. {
  117. ucontrol->value.enumerated.item[0] = poodle_jack_func;
  118. return 0;
  119. }
  120. static int poodle_set_jack(struct snd_kcontrol *kcontrol,
  121. struct snd_ctl_elem_value *ucontrol)
  122. {
  123. struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
  124. if (poodle_jack_func == ucontrol->value.enumerated.item[0])
  125. return 0;
  126. poodle_jack_func = ucontrol->value.enumerated.item[0];
  127. poodle_ext_control(&card->dapm);
  128. return 1;
  129. }
  130. static int poodle_get_spk(struct snd_kcontrol *kcontrol,
  131. struct snd_ctl_elem_value *ucontrol)
  132. {
  133. ucontrol->value.enumerated.item[0] = poodle_spk_func;
  134. return 0;
  135. }
  136. static int poodle_set_spk(struct snd_kcontrol *kcontrol,
  137. struct snd_ctl_elem_value *ucontrol)
  138. {
  139. struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
  140. if (poodle_spk_func == ucontrol->value.enumerated.item[0])
  141. return 0;
  142. poodle_spk_func = ucontrol->value.enumerated.item[0];
  143. poodle_ext_control(&card->dapm);
  144. return 1;
  145. }
  146. static int poodle_amp_event(struct snd_soc_dapm_widget *w,
  147. struct snd_kcontrol *k, int event)
  148. {
  149. if (SND_SOC_DAPM_EVENT_ON(event))
  150. locomo_gpio_write(poodle_pdata->locomo_dev,
  151. poodle_pdata->gpio_amp_on, 0);
  152. else
  153. locomo_gpio_write(poodle_pdata->locomo_dev,
  154. poodle_pdata->gpio_amp_on, 1);
  155. return 0;
  156. }
  157. /* poodle machine dapm widgets */
  158. static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
  159. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  160. SND_SOC_DAPM_SPK("Ext Spk", poodle_amp_event),
  161. SND_SOC_DAPM_MIC("Microphone", NULL),
  162. };
  163. /* Corgi machine connections to the codec pins */
  164. static const struct snd_soc_dapm_route poodle_audio_map[] = {
  165. /* headphone connected to LHPOUT1, RHPOUT1 */
  166. {"Headphone Jack", NULL, "LHPOUT"},
  167. {"Headphone Jack", NULL, "RHPOUT"},
  168. /* speaker connected to LOUT, ROUT */
  169. {"Ext Spk", NULL, "ROUT"},
  170. {"Ext Spk", NULL, "LOUT"},
  171. {"MICIN", NULL, "Microphone"},
  172. };
  173. static const char * const jack_function[] = {"Off", "Headphone"};
  174. static const char * const spk_function[] = {"Off", "On"};
  175. static const struct soc_enum poodle_enum[] = {
  176. SOC_ENUM_SINGLE_EXT(2, jack_function),
  177. SOC_ENUM_SINGLE_EXT(2, spk_function),
  178. };
  179. static const struct snd_kcontrol_new wm8731_poodle_controls[] = {
  180. SOC_ENUM_EXT("Jack Function", poodle_enum[0], poodle_get_jack,
  181. poodle_set_jack),
  182. SOC_ENUM_EXT("Speaker Function", poodle_enum[1], poodle_get_spk,
  183. poodle_set_spk),
  184. };
  185. /* poodle digital audio interface glue - connects codec <--> CPU */
  186. SND_SOC_DAILINK_DEFS(wm8731,
  187. DAILINK_COMP_ARRAY(COMP_CPU("pxa2xx-i2s")),
  188. DAILINK_COMP_ARRAY(COMP_CODEC("wm8731.0-001b", "wm8731-hifi")),
  189. DAILINK_COMP_ARRAY(COMP_PLATFORM("pxa-pcm-audio")));
  190. static struct snd_soc_dai_link poodle_dai = {
  191. .name = "WM8731",
  192. .stream_name = "WM8731",
  193. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  194. SND_SOC_DAIFMT_CBS_CFS,
  195. .ops = &poodle_ops,
  196. SND_SOC_DAILINK_REG(wm8731),
  197. };
  198. /* poodle audio machine driver */
  199. static struct snd_soc_card poodle = {
  200. .name = "Poodle",
  201. .dai_link = &poodle_dai,
  202. .num_links = 1,
  203. .owner = THIS_MODULE,
  204. .controls = wm8731_poodle_controls,
  205. .num_controls = ARRAY_SIZE(wm8731_poodle_controls),
  206. .dapm_widgets = wm8731_dapm_widgets,
  207. .num_dapm_widgets = ARRAY_SIZE(wm8731_dapm_widgets),
  208. .dapm_routes = poodle_audio_map,
  209. .num_dapm_routes = ARRAY_SIZE(poodle_audio_map),
  210. .fully_routed = true,
  211. };
  212. static int poodle_probe(struct platform_device *pdev)
  213. {
  214. struct snd_soc_card *card = &poodle;
  215. int ret;
  216. poodle_pdata = pdev->dev.platform_data;
  217. locomo_gpio_set_dir(poodle_pdata->locomo_dev,
  218. poodle_pdata->gpio_amp_on, 0);
  219. /* should we mute HP at startup - burning power ?*/
  220. locomo_gpio_set_dir(poodle_pdata->locomo_dev,
  221. poodle_pdata->gpio_mute_l, 0);
  222. locomo_gpio_set_dir(poodle_pdata->locomo_dev,
  223. poodle_pdata->gpio_mute_r, 0);
  224. card->dev = &pdev->dev;
  225. ret = devm_snd_soc_register_card(&pdev->dev, card);
  226. if (ret)
  227. dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
  228. ret);
  229. return ret;
  230. }
  231. static struct platform_driver poodle_driver = {
  232. .driver = {
  233. .name = "poodle-audio",
  234. .pm = &snd_soc_pm_ops,
  235. },
  236. .probe = poodle_probe,
  237. };
  238. module_platform_driver(poodle_driver);
  239. /* Module information */
  240. MODULE_AUTHOR("Richard Purdie");
  241. MODULE_DESCRIPTION("ALSA SoC Poodle");
  242. MODULE_LICENSE("GPL");
  243. MODULE_ALIAS("platform:poodle-audio");