cht_bsw_rt5645.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * cht-bsw-rt5645.c - ASoc Machine driver for Intel Cherryview-based platforms
  3. * Cherrytrail and Braswell, with RT5645 codec.
  4. *
  5. * Copyright (C) 2015 Intel Corp
  6. * Author: Fang, Yang A <yang.a.fang@intel.com>
  7. * N,Harshapriya <harshapriya.n@intel.com>
  8. * This file is modified from cht_bsw_rt5672.c
  9. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; version 2 of the License.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  21. */
  22. #include <linux/module.h>
  23. #include <linux/acpi.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/slab.h>
  26. #include <sound/pcm.h>
  27. #include <sound/pcm_params.h>
  28. #include <sound/soc.h>
  29. #include <sound/jack.h>
  30. #include "../../codecs/rt5645.h"
  31. #include "../atom/sst-atom-controls.h"
  32. #define CHT_PLAT_CLK_3_HZ 19200000
  33. #define CHT_CODEC_DAI "rt5645-aif1"
  34. struct cht_acpi_card {
  35. char *codec_id;
  36. int codec_type;
  37. struct snd_soc_card *soc_card;
  38. };
  39. struct cht_mc_private {
  40. struct snd_soc_jack jack;
  41. struct cht_acpi_card *acpi_card;
  42. };
  43. static inline struct snd_soc_dai *cht_get_codec_dai(struct snd_soc_card *card)
  44. {
  45. int i;
  46. for (i = 0; i < card->num_rtd; i++) {
  47. struct snd_soc_pcm_runtime *rtd;
  48. rtd = card->rtd + i;
  49. if (!strncmp(rtd->codec_dai->name, CHT_CODEC_DAI,
  50. strlen(CHT_CODEC_DAI)))
  51. return rtd->codec_dai;
  52. }
  53. return NULL;
  54. }
  55. static int platform_clock_control(struct snd_soc_dapm_widget *w,
  56. struct snd_kcontrol *k, int event)
  57. {
  58. struct snd_soc_dapm_context *dapm = w->dapm;
  59. struct snd_soc_card *card = dapm->card;
  60. struct snd_soc_dai *codec_dai;
  61. int ret;
  62. codec_dai = cht_get_codec_dai(card);
  63. if (!codec_dai) {
  64. dev_err(card->dev, "Codec dai not found; Unable to set platform clock\n");
  65. return -EIO;
  66. }
  67. if (!SND_SOC_DAPM_EVENT_OFF(event))
  68. return 0;
  69. /* Set codec sysclk source to its internal clock because codec PLL will
  70. * be off when idle and MCLK will also be off by ACPI when codec is
  71. * runtime suspended. Codec needs clock for jack detection and button
  72. * press.
  73. */
  74. ret = snd_soc_dai_set_sysclk(codec_dai, RT5645_SCLK_S_RCCLK,
  75. 0, SND_SOC_CLOCK_IN);
  76. if (ret < 0) {
  77. dev_err(card->dev, "can't set codec sysclk: %d\n", ret);
  78. return ret;
  79. }
  80. return 0;
  81. }
  82. static const struct snd_soc_dapm_widget cht_dapm_widgets[] = {
  83. SND_SOC_DAPM_HP("Headphone", NULL),
  84. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  85. SND_SOC_DAPM_MIC("Int Mic", NULL),
  86. SND_SOC_DAPM_SPK("Ext Spk", NULL),
  87. SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
  88. platform_clock_control, SND_SOC_DAPM_POST_PMD),
  89. };
  90. static const struct snd_soc_dapm_route cht_rt5645_audio_map[] = {
  91. {"IN1P", NULL, "Headset Mic"},
  92. {"IN1N", NULL, "Headset Mic"},
  93. {"DMIC L1", NULL, "Int Mic"},
  94. {"DMIC R1", NULL, "Int Mic"},
  95. {"Headphone", NULL, "HPOL"},
  96. {"Headphone", NULL, "HPOR"},
  97. {"Ext Spk", NULL, "SPOL"},
  98. {"Ext Spk", NULL, "SPOR"},
  99. {"AIF1 Playback", NULL, "ssp2 Tx"},
  100. {"ssp2 Tx", NULL, "codec_out0"},
  101. {"ssp2 Tx", NULL, "codec_out1"},
  102. {"codec_in0", NULL, "ssp2 Rx" },
  103. {"codec_in1", NULL, "ssp2 Rx" },
  104. {"ssp2 Rx", NULL, "AIF1 Capture"},
  105. {"Headphone", NULL, "Platform Clock"},
  106. {"Headset Mic", NULL, "Platform Clock"},
  107. {"Int Mic", NULL, "Platform Clock"},
  108. {"Ext Spk", NULL, "Platform Clock"},
  109. };
  110. static const struct snd_soc_dapm_route cht_rt5650_audio_map[] = {
  111. {"IN1P", NULL, "Headset Mic"},
  112. {"IN1N", NULL, "Headset Mic"},
  113. {"DMIC L2", NULL, "Int Mic"},
  114. {"DMIC R2", NULL, "Int Mic"},
  115. {"Headphone", NULL, "HPOL"},
  116. {"Headphone", NULL, "HPOR"},
  117. {"Ext Spk", NULL, "SPOL"},
  118. {"Ext Spk", NULL, "SPOR"},
  119. {"AIF1 Playback", NULL, "ssp2 Tx"},
  120. {"ssp2 Tx", NULL, "codec_out0"},
  121. {"ssp2 Tx", NULL, "codec_out1"},
  122. {"codec_in0", NULL, "ssp2 Rx" },
  123. {"codec_in1", NULL, "ssp2 Rx" },
  124. {"ssp2 Rx", NULL, "AIF1 Capture"},
  125. {"Headphone", NULL, "Platform Clock"},
  126. {"Headset Mic", NULL, "Platform Clock"},
  127. {"Int Mic", NULL, "Platform Clock"},
  128. {"Ext Spk", NULL, "Platform Clock"},
  129. };
  130. static const struct snd_kcontrol_new cht_mc_controls[] = {
  131. SOC_DAPM_PIN_SWITCH("Headphone"),
  132. SOC_DAPM_PIN_SWITCH("Headset Mic"),
  133. SOC_DAPM_PIN_SWITCH("Int Mic"),
  134. SOC_DAPM_PIN_SWITCH("Ext Spk"),
  135. };
  136. static int cht_aif1_hw_params(struct snd_pcm_substream *substream,
  137. struct snd_pcm_hw_params *params)
  138. {
  139. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  140. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  141. int ret;
  142. /* set codec PLL source to the 19.2MHz platform clock (MCLK) */
  143. ret = snd_soc_dai_set_pll(codec_dai, 0, RT5645_PLL1_S_MCLK,
  144. CHT_PLAT_CLK_3_HZ, params_rate(params) * 512);
  145. if (ret < 0) {
  146. dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
  147. return ret;
  148. }
  149. ret = snd_soc_dai_set_sysclk(codec_dai, RT5645_SCLK_S_PLL1,
  150. params_rate(params) * 512, SND_SOC_CLOCK_IN);
  151. if (ret < 0) {
  152. dev_err(rtd->dev, "can't set codec sysclk: %d\n", ret);
  153. return ret;
  154. }
  155. return 0;
  156. }
  157. static int cht_codec_init(struct snd_soc_pcm_runtime *runtime)
  158. {
  159. int ret;
  160. int jack_type;
  161. struct snd_soc_codec *codec = runtime->codec;
  162. struct snd_soc_dai *codec_dai = runtime->codec_dai;
  163. struct cht_mc_private *ctx = snd_soc_card_get_drvdata(runtime->card);
  164. /* Select clk_i2s1_asrc as ASRC clock source */
  165. rt5645_sel_asrc_clk_src(codec,
  166. RT5645_DA_STEREO_FILTER |
  167. RT5645_DA_MONO_L_FILTER |
  168. RT5645_DA_MONO_R_FILTER |
  169. RT5645_AD_STEREO_FILTER,
  170. RT5645_CLK_SEL_I2S1_ASRC);
  171. /* TDM 4 slots 24 bit, set Rx & Tx bitmask to 4 active slots */
  172. ret = snd_soc_dai_set_tdm_slot(codec_dai, 0xF, 0xF, 4, 24);
  173. if (ret < 0) {
  174. dev_err(runtime->dev, "can't set codec TDM slot %d\n", ret);
  175. return ret;
  176. }
  177. if (ctx->acpi_card->codec_type == CODEC_TYPE_RT5650)
  178. jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
  179. SND_JACK_BTN_0 | SND_JACK_BTN_1 |
  180. SND_JACK_BTN_2 | SND_JACK_BTN_3;
  181. else
  182. jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE;
  183. ret = snd_soc_card_jack_new(runtime->card, "Headset Jack",
  184. jack_type, &ctx->jack,
  185. NULL, 0);
  186. if (ret) {
  187. dev_err(runtime->dev, "Headset jack creation failed %d\n", ret);
  188. return ret;
  189. }
  190. rt5645_set_jack_detect(codec, &ctx->jack, &ctx->jack, &ctx->jack);
  191. return ret;
  192. }
  193. static int cht_codec_fixup(struct snd_soc_pcm_runtime *rtd,
  194. struct snd_pcm_hw_params *params)
  195. {
  196. struct snd_interval *rate = hw_param_interval(params,
  197. SNDRV_PCM_HW_PARAM_RATE);
  198. struct snd_interval *channels = hw_param_interval(params,
  199. SNDRV_PCM_HW_PARAM_CHANNELS);
  200. /* The DSP will covert the FE rate to 48k, stereo, 24bits */
  201. rate->min = rate->max = 48000;
  202. channels->min = channels->max = 2;
  203. /* set SSP2 to 24-bit */
  204. params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
  205. return 0;
  206. }
  207. static unsigned int rates_48000[] = {
  208. 48000,
  209. };
  210. static struct snd_pcm_hw_constraint_list constraints_48000 = {
  211. .count = ARRAY_SIZE(rates_48000),
  212. .list = rates_48000,
  213. };
  214. static int cht_aif1_startup(struct snd_pcm_substream *substream)
  215. {
  216. return snd_pcm_hw_constraint_list(substream->runtime, 0,
  217. SNDRV_PCM_HW_PARAM_RATE,
  218. &constraints_48000);
  219. }
  220. static struct snd_soc_ops cht_aif1_ops = {
  221. .startup = cht_aif1_startup,
  222. };
  223. static struct snd_soc_ops cht_be_ssp2_ops = {
  224. .hw_params = cht_aif1_hw_params,
  225. };
  226. static struct snd_soc_dai_link cht_dailink[] = {
  227. [MERR_DPCM_AUDIO] = {
  228. .name = "Audio Port",
  229. .stream_name = "Audio",
  230. .cpu_dai_name = "media-cpu-dai",
  231. .codec_dai_name = "snd-soc-dummy-dai",
  232. .codec_name = "snd-soc-dummy",
  233. .platform_name = "sst-mfld-platform",
  234. .nonatomic = true,
  235. .dynamic = 1,
  236. .dpcm_playback = 1,
  237. .dpcm_capture = 1,
  238. .ops = &cht_aif1_ops,
  239. },
  240. [MERR_DPCM_COMPR] = {
  241. .name = "Compressed Port",
  242. .stream_name = "Compress",
  243. .cpu_dai_name = "compress-cpu-dai",
  244. .codec_dai_name = "snd-soc-dummy-dai",
  245. .codec_name = "snd-soc-dummy",
  246. .platform_name = "sst-mfld-platform",
  247. },
  248. /* CODEC<->CODEC link */
  249. /* back ends */
  250. {
  251. .name = "SSP2-Codec",
  252. .be_id = 1,
  253. .cpu_dai_name = "ssp2-port",
  254. .platform_name = "sst-mfld-platform",
  255. .no_pcm = 1,
  256. .codec_dai_name = "rt5645-aif1",
  257. .codec_name = "i2c-10EC5645:00",
  258. .dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_IB_NF
  259. | SND_SOC_DAIFMT_CBS_CFS,
  260. .init = cht_codec_init,
  261. .be_hw_params_fixup = cht_codec_fixup,
  262. .nonatomic = true,
  263. .dpcm_playback = 1,
  264. .dpcm_capture = 1,
  265. .ops = &cht_be_ssp2_ops,
  266. },
  267. };
  268. /* SoC card */
  269. static struct snd_soc_card snd_soc_card_chtrt5645 = {
  270. .name = "chtrt5645",
  271. .owner = THIS_MODULE,
  272. .dai_link = cht_dailink,
  273. .num_links = ARRAY_SIZE(cht_dailink),
  274. .dapm_widgets = cht_dapm_widgets,
  275. .num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets),
  276. .dapm_routes = cht_rt5645_audio_map,
  277. .num_dapm_routes = ARRAY_SIZE(cht_rt5645_audio_map),
  278. .controls = cht_mc_controls,
  279. .num_controls = ARRAY_SIZE(cht_mc_controls),
  280. };
  281. static struct snd_soc_card snd_soc_card_chtrt5650 = {
  282. .name = "chtrt5650",
  283. .owner = THIS_MODULE,
  284. .dai_link = cht_dailink,
  285. .num_links = ARRAY_SIZE(cht_dailink),
  286. .dapm_widgets = cht_dapm_widgets,
  287. .num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets),
  288. .dapm_routes = cht_rt5650_audio_map,
  289. .num_dapm_routes = ARRAY_SIZE(cht_rt5650_audio_map),
  290. .controls = cht_mc_controls,
  291. .num_controls = ARRAY_SIZE(cht_mc_controls),
  292. };
  293. static struct cht_acpi_card snd_soc_cards[] = {
  294. {"10EC5645", CODEC_TYPE_RT5645, &snd_soc_card_chtrt5645},
  295. {"10EC5650", CODEC_TYPE_RT5650, &snd_soc_card_chtrt5650},
  296. };
  297. static acpi_status snd_acpi_codec_match(acpi_handle handle, u32 level,
  298. void *context, void **ret)
  299. {
  300. *(bool *)context = true;
  301. return AE_OK;
  302. }
  303. static int snd_cht_mc_probe(struct platform_device *pdev)
  304. {
  305. int ret_val = 0;
  306. int i;
  307. struct cht_mc_private *drv;
  308. struct snd_soc_card *card = snd_soc_cards[0].soc_card;
  309. bool found = false;
  310. char codec_name[16];
  311. drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_ATOMIC);
  312. if (!drv)
  313. return -ENOMEM;
  314. for (i = 0; i < ARRAY_SIZE(snd_soc_cards); i++) {
  315. if (ACPI_SUCCESS(acpi_get_devices(
  316. snd_soc_cards[i].codec_id,
  317. snd_acpi_codec_match,
  318. &found, NULL)) && found) {
  319. dev_dbg(&pdev->dev,
  320. "found codec %s\n", snd_soc_cards[i].codec_id);
  321. card = snd_soc_cards[i].soc_card;
  322. drv->acpi_card = &snd_soc_cards[i];
  323. break;
  324. }
  325. }
  326. card->dev = &pdev->dev;
  327. sprintf(codec_name, "i2c-%s:00", drv->acpi_card->codec_id);
  328. /* set correct codec name */
  329. strcpy((char *)card->dai_link[2].codec_name, codec_name);
  330. snd_soc_card_set_drvdata(card, drv);
  331. ret_val = devm_snd_soc_register_card(&pdev->dev, card);
  332. if (ret_val) {
  333. dev_err(&pdev->dev,
  334. "snd_soc_register_card failed %d\n", ret_val);
  335. return ret_val;
  336. }
  337. platform_set_drvdata(pdev, card);
  338. return ret_val;
  339. }
  340. static struct platform_driver snd_cht_mc_driver = {
  341. .driver = {
  342. .name = "cht-bsw-rt5645",
  343. },
  344. .probe = snd_cht_mc_probe,
  345. };
  346. module_platform_driver(snd_cht_mc_driver)
  347. MODULE_DESCRIPTION("ASoC Intel(R) Braswell Machine driver");
  348. MODULE_AUTHOR("Fang, Yang A,N,Harshapriya");
  349. MODULE_LICENSE("GPL v2");
  350. MODULE_ALIAS("platform:cht-bsw-rt5645");