cht_bsw_rt5672.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * cht_bsw_rt5672.c - ASoc Machine driver for Intel Cherryview-based platforms
  3. * Cherrytrail and Braswell, with RT5672 codec.
  4. *
  5. * Copyright (C) 2014 Intel Corp
  6. * Author: Subhransu S. Prusty <subhransu.s.prusty@intel.com>
  7. * Mengdong Lin <mengdong.lin@intel.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; version 2 of the License.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/slab.h>
  21. #include <sound/pcm.h>
  22. #include <sound/pcm_params.h>
  23. #include <sound/soc.h>
  24. #include <sound/jack.h>
  25. #include "../../codecs/rt5670.h"
  26. #include "../atom/sst-atom-controls.h"
  27. #include "../common/sst-acpi.h"
  28. /* The platform clock #3 outputs 19.2Mhz clock to codec as I2S MCLK */
  29. #define CHT_PLAT_CLK_3_HZ 19200000
  30. #define CHT_CODEC_DAI "rt5670-aif1"
  31. static struct snd_soc_jack cht_bsw_headset;
  32. static char cht_bsw_codec_name[16];
  33. /* Headset jack detection DAPM pins */
  34. static struct snd_soc_jack_pin cht_bsw_headset_pins[] = {
  35. {
  36. .pin = "Headset Mic",
  37. .mask = SND_JACK_MICROPHONE,
  38. },
  39. {
  40. .pin = "Headphone",
  41. .mask = SND_JACK_HEADPHONE,
  42. },
  43. };
  44. static inline struct snd_soc_dai *cht_get_codec_dai(struct snd_soc_card *card)
  45. {
  46. struct snd_soc_pcm_runtime *rtd;
  47. list_for_each_entry(rtd, &card->rtd_list, list) {
  48. if (!strncmp(rtd->codec_dai->name, CHT_CODEC_DAI,
  49. strlen(CHT_CODEC_DAI)))
  50. return rtd->codec_dai;
  51. }
  52. return NULL;
  53. }
  54. static int platform_clock_control(struct snd_soc_dapm_widget *w,
  55. struct snd_kcontrol *k, int event)
  56. {
  57. struct snd_soc_dapm_context *dapm = w->dapm;
  58. struct snd_soc_card *card = dapm->card;
  59. struct snd_soc_dai *codec_dai;
  60. int ret;
  61. codec_dai = cht_get_codec_dai(card);
  62. if (!codec_dai) {
  63. dev_err(card->dev, "Codec dai not found; Unable to set platform clock\n");
  64. return -EIO;
  65. }
  66. if (SND_SOC_DAPM_EVENT_ON(event)) {
  67. /* set codec PLL source to the 19.2MHz platform clock (MCLK) */
  68. ret = snd_soc_dai_set_pll(codec_dai, 0, RT5670_PLL1_S_MCLK,
  69. CHT_PLAT_CLK_3_HZ, 48000 * 512);
  70. if (ret < 0) {
  71. dev_err(card->dev, "can't set codec pll: %d\n", ret);
  72. return ret;
  73. }
  74. /* set codec sysclk source to PLL */
  75. ret = snd_soc_dai_set_sysclk(codec_dai, RT5670_SCLK_S_PLL1,
  76. 48000 * 512, SND_SOC_CLOCK_IN);
  77. if (ret < 0) {
  78. dev_err(card->dev, "can't set codec sysclk: %d\n", ret);
  79. return ret;
  80. }
  81. } else {
  82. /* Set codec sysclk source to its internal clock because codec
  83. * PLL will be off when idle and MCLK will also be off by ACPI
  84. * when codec is runtime suspended. Codec needs clock for jack
  85. * detection and button press.
  86. */
  87. snd_soc_dai_set_sysclk(codec_dai, RT5670_SCLK_S_RCCLK,
  88. 48000 * 512, SND_SOC_CLOCK_IN);
  89. }
  90. return 0;
  91. }
  92. static const struct snd_soc_dapm_widget cht_dapm_widgets[] = {
  93. SND_SOC_DAPM_HP("Headphone", NULL),
  94. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  95. SND_SOC_DAPM_MIC("Int Mic", NULL),
  96. SND_SOC_DAPM_SPK("Ext Spk", NULL),
  97. SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
  98. platform_clock_control, SND_SOC_DAPM_PRE_PMU |
  99. SND_SOC_DAPM_POST_PMD),
  100. };
  101. static const struct snd_soc_dapm_route cht_audio_map[] = {
  102. {"IN1P", NULL, "Headset Mic"},
  103. {"IN1N", NULL, "Headset Mic"},
  104. {"DMIC L1", NULL, "Int Mic"},
  105. {"DMIC R1", NULL, "Int Mic"},
  106. {"Headphone", NULL, "HPOL"},
  107. {"Headphone", NULL, "HPOR"},
  108. {"Ext Spk", NULL, "SPOLP"},
  109. {"Ext Spk", NULL, "SPOLN"},
  110. {"Ext Spk", NULL, "SPORP"},
  111. {"Ext Spk", NULL, "SPORN"},
  112. {"AIF1 Playback", NULL, "ssp2 Tx"},
  113. {"ssp2 Tx", NULL, "codec_out0"},
  114. {"ssp2 Tx", NULL, "codec_out1"},
  115. {"codec_in0", NULL, "ssp2 Rx"},
  116. {"codec_in1", NULL, "ssp2 Rx"},
  117. {"ssp2 Rx", NULL, "AIF1 Capture"},
  118. {"Headphone", NULL, "Platform Clock"},
  119. {"Headset Mic", NULL, "Platform Clock"},
  120. {"Int Mic", NULL, "Platform Clock"},
  121. {"Ext Spk", NULL, "Platform Clock"},
  122. };
  123. static const struct snd_kcontrol_new cht_mc_controls[] = {
  124. SOC_DAPM_PIN_SWITCH("Headphone"),
  125. SOC_DAPM_PIN_SWITCH("Headset Mic"),
  126. SOC_DAPM_PIN_SWITCH("Int Mic"),
  127. SOC_DAPM_PIN_SWITCH("Ext Spk"),
  128. };
  129. static int cht_aif1_hw_params(struct snd_pcm_substream *substream,
  130. struct snd_pcm_hw_params *params)
  131. {
  132. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  133. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  134. int ret;
  135. /* set codec PLL source to the 19.2MHz platform clock (MCLK) */
  136. ret = snd_soc_dai_set_pll(codec_dai, 0, RT5670_PLL1_S_MCLK,
  137. CHT_PLAT_CLK_3_HZ, params_rate(params) * 512);
  138. if (ret < 0) {
  139. dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
  140. return ret;
  141. }
  142. /* set codec sysclk source to PLL */
  143. ret = snd_soc_dai_set_sysclk(codec_dai, RT5670_SCLK_S_PLL1,
  144. params_rate(params) * 512,
  145. SND_SOC_CLOCK_IN);
  146. if (ret < 0) {
  147. dev_err(rtd->dev, "can't set codec sysclk: %d\n", ret);
  148. return ret;
  149. }
  150. return 0;
  151. }
  152. static int cht_codec_init(struct snd_soc_pcm_runtime *runtime)
  153. {
  154. int ret;
  155. struct snd_soc_dai *codec_dai = runtime->codec_dai;
  156. struct snd_soc_codec *codec = codec_dai->codec;
  157. /* TDM 4 slots 24 bit, set Rx & Tx bitmask to 4 active slots */
  158. ret = snd_soc_dai_set_tdm_slot(codec_dai, 0xF, 0xF, 4, 24);
  159. if (ret < 0) {
  160. dev_err(runtime->dev, "can't set codec TDM slot %d\n", ret);
  161. return ret;
  162. }
  163. /* Select codec ASRC clock source to track I2S1 clock, because codec
  164. * is in slave mode and 100fs I2S format (BCLK = 100 * LRCLK) cannot
  165. * be supported by RT5672. Otherwise, ASRC will be disabled and cause
  166. * noise.
  167. */
  168. rt5670_sel_asrc_clk_src(codec,
  169. RT5670_DA_STEREO_FILTER
  170. | RT5670_DA_MONO_L_FILTER
  171. | RT5670_DA_MONO_R_FILTER
  172. | RT5670_AD_STEREO_FILTER
  173. | RT5670_AD_MONO_L_FILTER
  174. | RT5670_AD_MONO_R_FILTER,
  175. RT5670_CLK_SEL_I2S1_ASRC);
  176. ret = snd_soc_card_jack_new(runtime->card, "Headset",
  177. SND_JACK_HEADSET | SND_JACK_BTN_0 |
  178. SND_JACK_BTN_1 | SND_JACK_BTN_2, &cht_bsw_headset,
  179. cht_bsw_headset_pins, ARRAY_SIZE(cht_bsw_headset_pins));
  180. if (ret)
  181. return ret;
  182. rt5670_set_jack_detect(codec, &cht_bsw_headset);
  183. return 0;
  184. }
  185. static int cht_codec_fixup(struct snd_soc_pcm_runtime *rtd,
  186. struct snd_pcm_hw_params *params)
  187. {
  188. struct snd_interval *rate = hw_param_interval(params,
  189. SNDRV_PCM_HW_PARAM_RATE);
  190. struct snd_interval *channels = hw_param_interval(params,
  191. SNDRV_PCM_HW_PARAM_CHANNELS);
  192. /* The DSP will covert the FE rate to 48k, stereo, 24bits */
  193. rate->min = rate->max = 48000;
  194. channels->min = channels->max = 2;
  195. /* set SSP2 to 24-bit */
  196. params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
  197. return 0;
  198. }
  199. static int cht_aif1_startup(struct snd_pcm_substream *substream)
  200. {
  201. return snd_pcm_hw_constraint_single(substream->runtime,
  202. SNDRV_PCM_HW_PARAM_RATE, 48000);
  203. }
  204. static const struct snd_soc_ops cht_aif1_ops = {
  205. .startup = cht_aif1_startup,
  206. };
  207. static const struct snd_soc_ops cht_be_ssp2_ops = {
  208. .hw_params = cht_aif1_hw_params,
  209. };
  210. static struct snd_soc_dai_link cht_dailink[] = {
  211. /* Front End DAI links */
  212. [MERR_DPCM_AUDIO] = {
  213. .name = "Audio Port",
  214. .stream_name = "Audio",
  215. .cpu_dai_name = "media-cpu-dai",
  216. .codec_dai_name = "snd-soc-dummy-dai",
  217. .codec_name = "snd-soc-dummy",
  218. .platform_name = "sst-mfld-platform",
  219. .nonatomic = true,
  220. .dynamic = 1,
  221. .dpcm_playback = 1,
  222. .dpcm_capture = 1,
  223. .ops = &cht_aif1_ops,
  224. },
  225. [MERR_DPCM_DEEP_BUFFER] = {
  226. .name = "Deep-Buffer Audio Port",
  227. .stream_name = "Deep-Buffer Audio",
  228. .cpu_dai_name = "deepbuffer-cpu-dai",
  229. .codec_dai_name = "snd-soc-dummy-dai",
  230. .codec_name = "snd-soc-dummy",
  231. .platform_name = "sst-mfld-platform",
  232. .nonatomic = true,
  233. .dynamic = 1,
  234. .dpcm_playback = 1,
  235. .ops = &cht_aif1_ops,
  236. },
  237. [MERR_DPCM_COMPR] = {
  238. .name = "Compressed Port",
  239. .stream_name = "Compress",
  240. .cpu_dai_name = "compress-cpu-dai",
  241. .codec_dai_name = "snd-soc-dummy-dai",
  242. .codec_name = "snd-soc-dummy",
  243. .platform_name = "sst-mfld-platform",
  244. },
  245. /* Back End DAI links */
  246. {
  247. /* SSP2 - Codec */
  248. .name = "SSP2-Codec",
  249. .id = 1,
  250. .cpu_dai_name = "ssp2-port",
  251. .platform_name = "sst-mfld-platform",
  252. .no_pcm = 1,
  253. .nonatomic = true,
  254. .codec_dai_name = "rt5670-aif1",
  255. .codec_name = "i2c-10EC5670:00",
  256. .dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_IB_NF
  257. | SND_SOC_DAIFMT_CBS_CFS,
  258. .init = cht_codec_init,
  259. .be_hw_params_fixup = cht_codec_fixup,
  260. .dpcm_playback = 1,
  261. .dpcm_capture = 1,
  262. .ops = &cht_be_ssp2_ops,
  263. },
  264. };
  265. static int cht_suspend_pre(struct snd_soc_card *card)
  266. {
  267. struct snd_soc_component *component;
  268. list_for_each_entry(component, &card->component_dev_list, card_list) {
  269. if (!strcmp(component->name, "i2c-10EC5670:00")) {
  270. struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
  271. dev_dbg(codec->dev, "disabling jack detect before going to suspend.\n");
  272. rt5670_jack_suspend(codec);
  273. break;
  274. }
  275. }
  276. return 0;
  277. }
  278. static int cht_resume_post(struct snd_soc_card *card)
  279. {
  280. struct snd_soc_component *component;
  281. list_for_each_entry(component, &card->component_dev_list, card_list) {
  282. if (!strcmp(component->name, "i2c-10EC5670:00")) {
  283. struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
  284. dev_dbg(codec->dev, "enabling jack detect for resume.\n");
  285. rt5670_jack_resume(codec);
  286. break;
  287. }
  288. }
  289. return 0;
  290. }
  291. /* SoC card */
  292. static struct snd_soc_card snd_soc_card_cht = {
  293. .name = "cherrytrailcraudio",
  294. .owner = THIS_MODULE,
  295. .dai_link = cht_dailink,
  296. .num_links = ARRAY_SIZE(cht_dailink),
  297. .dapm_widgets = cht_dapm_widgets,
  298. .num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets),
  299. .dapm_routes = cht_audio_map,
  300. .num_dapm_routes = ARRAY_SIZE(cht_audio_map),
  301. .controls = cht_mc_controls,
  302. .num_controls = ARRAY_SIZE(cht_mc_controls),
  303. .suspend_pre = cht_suspend_pre,
  304. .resume_post = cht_resume_post,
  305. };
  306. #define RT5672_I2C_DEFAULT "i2c-10EC5670:00"
  307. static int snd_cht_mc_probe(struct platform_device *pdev)
  308. {
  309. int ret_val = 0;
  310. struct sst_acpi_mach *mach = pdev->dev.platform_data;
  311. const char *i2c_name;
  312. int i;
  313. strcpy(cht_bsw_codec_name, RT5672_I2C_DEFAULT);
  314. /* fixup codec name based on HID */
  315. if (mach) {
  316. i2c_name = sst_acpi_find_name_from_hid(mach->id);
  317. if (i2c_name) {
  318. snprintf(cht_bsw_codec_name, sizeof(cht_bsw_codec_name),
  319. "i2c-%s", i2c_name);
  320. for (i = 0; i < ARRAY_SIZE(cht_dailink); i++) {
  321. if (!strcmp(cht_dailink[i].codec_name,
  322. RT5672_I2C_DEFAULT)) {
  323. cht_dailink[i].codec_name =
  324. cht_bsw_codec_name;
  325. break;
  326. }
  327. }
  328. }
  329. }
  330. /* register the soc card */
  331. snd_soc_card_cht.dev = &pdev->dev;
  332. ret_val = devm_snd_soc_register_card(&pdev->dev, &snd_soc_card_cht);
  333. if (ret_val) {
  334. dev_err(&pdev->dev,
  335. "snd_soc_register_card failed %d\n", ret_val);
  336. return ret_val;
  337. }
  338. platform_set_drvdata(pdev, &snd_soc_card_cht);
  339. return ret_val;
  340. }
  341. static struct platform_driver snd_cht_mc_driver = {
  342. .driver = {
  343. .name = "cht-bsw-rt5672",
  344. },
  345. .probe = snd_cht_mc_probe,
  346. };
  347. module_platform_driver(snd_cht_mc_driver);
  348. MODULE_DESCRIPTION("ASoC Intel(R) Baytrail CR Machine driver");
  349. MODULE_AUTHOR("Subhransu S. Prusty, Mengdong Lin");
  350. MODULE_LICENSE("GPL v2");
  351. MODULE_ALIAS("platform:cht-bsw-rt5672");