cht_bsw_nau8824.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * cht-bsw-nau8824.c - ASoc Machine driver for Intel Cherryview-based
  3. * platforms Cherrytrail and Braswell, with nau8824 codec.
  4. *
  5. * Copyright (C) 2018 Intel Corp
  6. * Copyright (C) 2018 Nuvoton Technology Corp
  7. *
  8. * Author: Wang, Joseph C <joequant@gmail.com>
  9. * Co-author: John Hsu <KCHSU0@nuvoton.com>
  10. * This file is based on cht_bsw_rt5672.c and cht-bsw-max98090.c
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; version 2 of the License.
  15. *
  16. * This program is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. */
  21. #include <linux/module.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/slab.h>
  24. #include <sound/pcm.h>
  25. #include <sound/pcm_params.h>
  26. #include <sound/soc.h>
  27. #include <sound/jack.h>
  28. #include <linux/input.h>
  29. #include "../atom/sst-atom-controls.h"
  30. #include "../../codecs/nau8824.h"
  31. struct cht_mc_private {
  32. struct snd_soc_jack jack;
  33. };
  34. static struct snd_soc_jack_pin cht_bsw_jack_pins[] = {
  35. {
  36. .pin = "Headphone",
  37. .mask = SND_JACK_HEADPHONE,
  38. },
  39. {
  40. .pin = "Headset Mic",
  41. .mask = SND_JACK_MICROPHONE,
  42. },
  43. };
  44. static const struct snd_soc_dapm_widget cht_dapm_widgets[] = {
  45. SND_SOC_DAPM_HP("Headphone", NULL),
  46. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  47. SND_SOC_DAPM_MIC("Int Mic", NULL),
  48. SND_SOC_DAPM_SPK("Ext Spk", NULL),
  49. };
  50. static const struct snd_soc_dapm_route cht_audio_map[] = {
  51. {"Ext Spk", NULL, "SPKOUTL"},
  52. {"Ext Spk", NULL, "SPKOUTR"},
  53. {"Headphone", NULL, "HPOL"},
  54. {"Headphone", NULL, "HPOR"},
  55. {"MIC1", NULL, "Int Mic"},
  56. {"MIC2", NULL, "Int Mic"},
  57. {"HSMIC1", NULL, "Headset Mic"},
  58. {"HSMIC2", NULL, "Headset Mic"},
  59. {"Playback", NULL, "ssp2 Tx"},
  60. {"ssp2 Tx", NULL, "codec_out0"},
  61. {"ssp2 Tx", NULL, "codec_out1"},
  62. {"codec_in0", NULL, "ssp2 Rx" },
  63. {"codec_in1", NULL, "ssp2 Rx" },
  64. {"ssp2 Rx", NULL, "Capture"},
  65. };
  66. static const struct snd_kcontrol_new cht_mc_controls[] = {
  67. SOC_DAPM_PIN_SWITCH("Headphone"),
  68. SOC_DAPM_PIN_SWITCH("Headset Mic"),
  69. SOC_DAPM_PIN_SWITCH("Int Mic"),
  70. SOC_DAPM_PIN_SWITCH("Ext Spk"),
  71. };
  72. static int cht_aif1_hw_params(struct snd_pcm_substream *substream,
  73. struct snd_pcm_hw_params *params)
  74. {
  75. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  76. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  77. int ret;
  78. ret = snd_soc_dai_set_sysclk(codec_dai, NAU8824_CLK_FLL_FS, 0,
  79. SND_SOC_CLOCK_IN);
  80. if (ret < 0) {
  81. dev_err(codec_dai->dev, "can't set FS clock %d\n", ret);
  82. return ret;
  83. }
  84. ret = snd_soc_dai_set_pll(codec_dai, 0, 0, params_rate(params),
  85. params_rate(params) * 256);
  86. if (ret < 0) {
  87. dev_err(codec_dai->dev, "can't set FLL: %d\n", ret);
  88. return ret;
  89. }
  90. return 0;
  91. }
  92. static int cht_codec_init(struct snd_soc_pcm_runtime *runtime)
  93. {
  94. struct cht_mc_private *ctx = snd_soc_card_get_drvdata(runtime->card);
  95. struct snd_soc_jack *jack = &ctx->jack;
  96. struct snd_soc_dai *codec_dai = runtime->codec_dai;
  97. struct snd_soc_component *component = codec_dai->component;
  98. int ret, jack_type;
  99. /* TDM 4 slots 24 bit, set Rx & Tx bitmask to 4 active slots */
  100. ret = snd_soc_dai_set_tdm_slot(codec_dai, 0xf, 0x1, 4, 24);
  101. if (ret < 0) {
  102. dev_err(runtime->dev, "can't set codec TDM slot %d\n", ret);
  103. return ret;
  104. }
  105. /* NAU88L24 supports 4 butons headset detection
  106. * KEY_MEDIA
  107. * KEY_VOICECOMMAND
  108. * KEY_VOLUMEUP
  109. * KEY_VOLUMEDOWN
  110. */
  111. jack_type = SND_JACK_HEADSET | SND_JACK_BTN_0 | SND_JACK_BTN_1 |
  112. SND_JACK_BTN_2 | SND_JACK_BTN_3;
  113. ret = snd_soc_card_jack_new(runtime->card, "Headset", jack_type, jack,
  114. cht_bsw_jack_pins, ARRAY_SIZE(cht_bsw_jack_pins));
  115. if (ret) {
  116. dev_err(runtime->dev,
  117. "Headset Jack creation failed %d\n", ret);
  118. return ret;
  119. }
  120. snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_MEDIA);
  121. snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
  122. snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
  123. snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
  124. nau8824_enable_jack_detect(component, jack);
  125. return ret;
  126. }
  127. static int cht_codec_fixup(struct snd_soc_pcm_runtime *rtd,
  128. struct snd_pcm_hw_params *params)
  129. {
  130. struct snd_interval *rate = hw_param_interval(params,
  131. SNDRV_PCM_HW_PARAM_RATE);
  132. struct snd_interval *channels = hw_param_interval(params,
  133. SNDRV_PCM_HW_PARAM_CHANNELS);
  134. struct snd_mask *fmt =
  135. hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  136. /* The DSP will covert the FE rate to 48k, stereo, 24bits */
  137. rate->min = rate->max = 48000;
  138. channels->min = channels->max = 2;
  139. /* set SSP2 to 24-bit */
  140. snd_mask_none(fmt);
  141. params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
  142. return 0;
  143. }
  144. static int cht_aif1_startup(struct snd_pcm_substream *substream)
  145. {
  146. return snd_pcm_hw_constraint_single(substream->runtime,
  147. SNDRV_PCM_HW_PARAM_RATE, 48000);
  148. }
  149. static const struct snd_soc_ops cht_aif1_ops = {
  150. .startup = cht_aif1_startup,
  151. };
  152. static const struct snd_soc_ops cht_be_ssp2_ops = {
  153. .hw_params = cht_aif1_hw_params,
  154. };
  155. static struct snd_soc_dai_link cht_dailink[] = {
  156. /* Front End DAI links */
  157. [MERR_DPCM_AUDIO] = {
  158. .name = "Audio Port",
  159. .stream_name = "Audio",
  160. .cpu_dai_name = "media-cpu-dai",
  161. .codec_dai_name = "snd-soc-dummy-dai",
  162. .codec_name = "snd-soc-dummy",
  163. .platform_name = "sst-mfld-platform",
  164. .nonatomic = true,
  165. .dynamic = 1,
  166. .dpcm_playback = 1,
  167. .dpcm_capture = 1,
  168. .ops = &cht_aif1_ops,
  169. },
  170. [MERR_DPCM_DEEP_BUFFER] = {
  171. .name = "Deep-Buffer Audio Port",
  172. .stream_name = "Deep-Buffer Audio",
  173. .cpu_dai_name = "deepbuffer-cpu-dai",
  174. .codec_dai_name = "snd-soc-dummy-dai",
  175. .codec_name = "snd-soc-dummy",
  176. .platform_name = "sst-mfld-platform",
  177. .nonatomic = true,
  178. .dynamic = 1,
  179. .dpcm_playback = 1,
  180. .ops = &cht_aif1_ops,
  181. },
  182. [MERR_DPCM_COMPR] = {
  183. .name = "Compressed Port",
  184. .stream_name = "Compress",
  185. .cpu_dai_name = "compress-cpu-dai",
  186. .codec_dai_name = "snd-soc-dummy-dai",
  187. .codec_name = "snd-soc-dummy",
  188. .platform_name = "sst-mfld-platform",
  189. },
  190. /* Back End DAI links */
  191. {
  192. /* SSP2 - Codec */
  193. .name = "SSP2-Codec",
  194. .id = 1,
  195. .cpu_dai_name = "ssp2-port",
  196. .platform_name = "sst-mfld-platform",
  197. .no_pcm = 1,
  198. .codec_dai_name = NAU8824_CODEC_DAI,
  199. .codec_name = "i2c-10508824:00",
  200. .dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_IB_NF
  201. | SND_SOC_DAIFMT_CBS_CFS,
  202. .init = cht_codec_init,
  203. .be_hw_params_fixup = cht_codec_fixup,
  204. .dpcm_playback = 1,
  205. .dpcm_capture = 1,
  206. .ops = &cht_be_ssp2_ops,
  207. },
  208. };
  209. /* SoC card */
  210. static struct snd_soc_card snd_soc_card_cht = {
  211. .name = "chtnau8824",
  212. .owner = THIS_MODULE,
  213. .dai_link = cht_dailink,
  214. .num_links = ARRAY_SIZE(cht_dailink),
  215. .dapm_widgets = cht_dapm_widgets,
  216. .num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets),
  217. .dapm_routes = cht_audio_map,
  218. .num_dapm_routes = ARRAY_SIZE(cht_audio_map),
  219. .controls = cht_mc_controls,
  220. .num_controls = ARRAY_SIZE(cht_mc_controls),
  221. };
  222. static int snd_cht_mc_probe(struct platform_device *pdev)
  223. {
  224. struct cht_mc_private *drv;
  225. int ret_val;
  226. drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_KERNEL);
  227. if (!drv)
  228. return -ENOMEM;
  229. snd_soc_card_set_drvdata(&snd_soc_card_cht, drv);
  230. /* register the soc card */
  231. snd_soc_card_cht.dev = &pdev->dev;
  232. ret_val = devm_snd_soc_register_card(&pdev->dev, &snd_soc_card_cht);
  233. if (ret_val) {
  234. dev_err(&pdev->dev,
  235. "snd_soc_register_card failed %d\n", ret_val);
  236. return ret_val;
  237. }
  238. platform_set_drvdata(pdev, &snd_soc_card_cht);
  239. return ret_val;
  240. }
  241. static struct platform_driver snd_cht_mc_driver = {
  242. .driver = {
  243. .name = "cht-bsw-nau8824",
  244. },
  245. .probe = snd_cht_mc_probe,
  246. };
  247. module_platform_driver(snd_cht_mc_driver);
  248. MODULE_DESCRIPTION("ASoC Intel(R) Baytrail CR Machine driver");
  249. MODULE_AUTHOR("Wang, Joseph C <joequant@gmail.com>");
  250. MODULE_AUTHOR("John Hsu <KCHSU0@nuvoton.com>");
  251. MODULE_LICENSE("GPL v2");
  252. MODULE_ALIAS("platform:cht-bsw-nau8824");