cht_bsw_rt5645.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. #include "../common/sst-acpi.h"
  33. #define CHT_PLAT_CLK_3_HZ 19200000
  34. #define CHT_CODEC_DAI "rt5645-aif1"
  35. struct cht_acpi_card {
  36. char *codec_id;
  37. int codec_type;
  38. struct snd_soc_card *soc_card;
  39. };
  40. struct cht_mc_private {
  41. struct snd_soc_jack jack;
  42. struct cht_acpi_card *acpi_card;
  43. char codec_name[16];
  44. };
  45. static inline struct snd_soc_dai *cht_get_codec_dai(struct snd_soc_card *card)
  46. {
  47. struct snd_soc_pcm_runtime *rtd;
  48. list_for_each_entry(rtd, &card->rtd_list, list) {
  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 struct snd_soc_jack_pin cht_bsw_jack_pins[] = {
  137. {
  138. .pin = "Headphone",
  139. .mask = SND_JACK_HEADPHONE,
  140. },
  141. {
  142. .pin = "Headset Mic",
  143. .mask = SND_JACK_MICROPHONE,
  144. },
  145. };
  146. static int cht_aif1_hw_params(struct snd_pcm_substream *substream,
  147. struct snd_pcm_hw_params *params)
  148. {
  149. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  150. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  151. int ret;
  152. /* set codec PLL source to the 19.2MHz platform clock (MCLK) */
  153. ret = snd_soc_dai_set_pll(codec_dai, 0, RT5645_PLL1_S_MCLK,
  154. CHT_PLAT_CLK_3_HZ, params_rate(params) * 512);
  155. if (ret < 0) {
  156. dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
  157. return ret;
  158. }
  159. ret = snd_soc_dai_set_sysclk(codec_dai, RT5645_SCLK_S_PLL1,
  160. params_rate(params) * 512, SND_SOC_CLOCK_IN);
  161. if (ret < 0) {
  162. dev_err(rtd->dev, "can't set codec sysclk: %d\n", ret);
  163. return ret;
  164. }
  165. return 0;
  166. }
  167. static int cht_codec_init(struct snd_soc_pcm_runtime *runtime)
  168. {
  169. int ret;
  170. int jack_type;
  171. struct snd_soc_codec *codec = runtime->codec;
  172. struct snd_soc_dai *codec_dai = runtime->codec_dai;
  173. struct cht_mc_private *ctx = snd_soc_card_get_drvdata(runtime->card);
  174. /* Select clk_i2s1_asrc as ASRC clock source */
  175. rt5645_sel_asrc_clk_src(codec,
  176. RT5645_DA_STEREO_FILTER |
  177. RT5645_DA_MONO_L_FILTER |
  178. RT5645_DA_MONO_R_FILTER |
  179. RT5645_AD_STEREO_FILTER,
  180. RT5645_CLK_SEL_I2S1_ASRC);
  181. /* TDM 4 slots 24 bit, set Rx & Tx bitmask to 4 active slots */
  182. ret = snd_soc_dai_set_tdm_slot(codec_dai, 0xF, 0xF, 4, 24);
  183. if (ret < 0) {
  184. dev_err(runtime->dev, "can't set codec TDM slot %d\n", ret);
  185. return ret;
  186. }
  187. if (ctx->acpi_card->codec_type == CODEC_TYPE_RT5650)
  188. jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
  189. SND_JACK_BTN_0 | SND_JACK_BTN_1 |
  190. SND_JACK_BTN_2 | SND_JACK_BTN_3;
  191. else
  192. jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE;
  193. ret = snd_soc_card_jack_new(runtime->card, "Headset",
  194. jack_type, &ctx->jack,
  195. cht_bsw_jack_pins, ARRAY_SIZE(cht_bsw_jack_pins));
  196. if (ret) {
  197. dev_err(runtime->dev, "Headset jack creation failed %d\n", ret);
  198. return ret;
  199. }
  200. rt5645_set_jack_detect(codec, &ctx->jack, &ctx->jack, &ctx->jack);
  201. return ret;
  202. }
  203. static int cht_codec_fixup(struct snd_soc_pcm_runtime *rtd,
  204. struct snd_pcm_hw_params *params)
  205. {
  206. struct snd_interval *rate = hw_param_interval(params,
  207. SNDRV_PCM_HW_PARAM_RATE);
  208. struct snd_interval *channels = hw_param_interval(params,
  209. SNDRV_PCM_HW_PARAM_CHANNELS);
  210. /* The DSP will covert the FE rate to 48k, stereo, 24bits */
  211. rate->min = rate->max = 48000;
  212. channels->min = channels->max = 2;
  213. /* set SSP2 to 24-bit */
  214. params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
  215. return 0;
  216. }
  217. static int cht_aif1_startup(struct snd_pcm_substream *substream)
  218. {
  219. return snd_pcm_hw_constraint_single(substream->runtime,
  220. SNDRV_PCM_HW_PARAM_RATE, 48000);
  221. }
  222. static const struct snd_soc_ops cht_aif1_ops = {
  223. .startup = cht_aif1_startup,
  224. };
  225. static const struct snd_soc_ops cht_be_ssp2_ops = {
  226. .hw_params = cht_aif1_hw_params,
  227. };
  228. static struct snd_soc_dai_link cht_dailink[] = {
  229. [MERR_DPCM_AUDIO] = {
  230. .name = "Audio Port",
  231. .stream_name = "Audio",
  232. .cpu_dai_name = "media-cpu-dai",
  233. .codec_dai_name = "snd-soc-dummy-dai",
  234. .codec_name = "snd-soc-dummy",
  235. .platform_name = "sst-mfld-platform",
  236. .nonatomic = true,
  237. .dynamic = 1,
  238. .dpcm_playback = 1,
  239. .dpcm_capture = 1,
  240. .ops = &cht_aif1_ops,
  241. },
  242. [MERR_DPCM_DEEP_BUFFER] = {
  243. .name = "Deep-Buffer Audio Port",
  244. .stream_name = "Deep-Buffer Audio",
  245. .cpu_dai_name = "deepbuffer-cpu-dai",
  246. .codec_dai_name = "snd-soc-dummy-dai",
  247. .codec_name = "snd-soc-dummy",
  248. .platform_name = "sst-mfld-platform",
  249. .nonatomic = true,
  250. .dynamic = 1,
  251. .dpcm_playback = 1,
  252. .ops = &cht_aif1_ops,
  253. },
  254. [MERR_DPCM_COMPR] = {
  255. .name = "Compressed Port",
  256. .stream_name = "Compress",
  257. .cpu_dai_name = "compress-cpu-dai",
  258. .codec_dai_name = "snd-soc-dummy-dai",
  259. .codec_name = "snd-soc-dummy",
  260. .platform_name = "sst-mfld-platform",
  261. },
  262. /* CODEC<->CODEC link */
  263. /* back ends */
  264. {
  265. .name = "SSP2-Codec",
  266. .id = 1,
  267. .cpu_dai_name = "ssp2-port",
  268. .platform_name = "sst-mfld-platform",
  269. .no_pcm = 1,
  270. .codec_dai_name = "rt5645-aif1",
  271. .codec_name = "i2c-10EC5645:00",
  272. .dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_IB_NF
  273. | SND_SOC_DAIFMT_CBS_CFS,
  274. .init = cht_codec_init,
  275. .be_hw_params_fixup = cht_codec_fixup,
  276. .nonatomic = true,
  277. .dpcm_playback = 1,
  278. .dpcm_capture = 1,
  279. .ops = &cht_be_ssp2_ops,
  280. },
  281. };
  282. /* SoC card */
  283. static struct snd_soc_card snd_soc_card_chtrt5645 = {
  284. .name = "chtrt5645",
  285. .owner = THIS_MODULE,
  286. .dai_link = cht_dailink,
  287. .num_links = ARRAY_SIZE(cht_dailink),
  288. .dapm_widgets = cht_dapm_widgets,
  289. .num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets),
  290. .dapm_routes = cht_rt5645_audio_map,
  291. .num_dapm_routes = ARRAY_SIZE(cht_rt5645_audio_map),
  292. .controls = cht_mc_controls,
  293. .num_controls = ARRAY_SIZE(cht_mc_controls),
  294. };
  295. static struct snd_soc_card snd_soc_card_chtrt5650 = {
  296. .name = "chtrt5650",
  297. .owner = THIS_MODULE,
  298. .dai_link = cht_dailink,
  299. .num_links = ARRAY_SIZE(cht_dailink),
  300. .dapm_widgets = cht_dapm_widgets,
  301. .num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets),
  302. .dapm_routes = cht_rt5650_audio_map,
  303. .num_dapm_routes = ARRAY_SIZE(cht_rt5650_audio_map),
  304. .controls = cht_mc_controls,
  305. .num_controls = ARRAY_SIZE(cht_mc_controls),
  306. };
  307. static struct cht_acpi_card snd_soc_cards[] = {
  308. {"10EC5640", CODEC_TYPE_RT5645, &snd_soc_card_chtrt5645},
  309. {"10EC5645", CODEC_TYPE_RT5645, &snd_soc_card_chtrt5645},
  310. {"10EC5650", CODEC_TYPE_RT5650, &snd_soc_card_chtrt5650},
  311. };
  312. static char cht_rt5640_codec_name[16]; /* i2c-<HID>:00 with HID being 8 chars */
  313. static int snd_cht_mc_probe(struct platform_device *pdev)
  314. {
  315. int ret_val = 0;
  316. int i;
  317. struct cht_mc_private *drv;
  318. struct snd_soc_card *card = snd_soc_cards[0].soc_card;
  319. struct sst_acpi_mach *mach;
  320. const char *i2c_name = NULL;
  321. int dai_index = 0;
  322. drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_ATOMIC);
  323. if (!drv)
  324. return -ENOMEM;
  325. for (i = 0; i < ARRAY_SIZE(snd_soc_cards); i++) {
  326. if (acpi_dev_found(snd_soc_cards[i].codec_id)) {
  327. dev_dbg(&pdev->dev,
  328. "found codec %s\n", snd_soc_cards[i].codec_id);
  329. card = snd_soc_cards[i].soc_card;
  330. drv->acpi_card = &snd_soc_cards[i];
  331. break;
  332. }
  333. }
  334. card->dev = &pdev->dev;
  335. mach = card->dev->platform_data;
  336. sprintf(drv->codec_name, "i2c-%s:00", drv->acpi_card->codec_id);
  337. /* set correct codec name */
  338. for (i = 0; i < ARRAY_SIZE(cht_dailink); i++)
  339. if (!strcmp(card->dai_link[i].codec_name, "i2c-10EC5645:00")) {
  340. card->dai_link[i].codec_name = drv->codec_name;
  341. dai_index = i;
  342. }
  343. /* fixup codec name based on HID */
  344. i2c_name = sst_acpi_find_name_from_hid(mach->id);
  345. if (i2c_name != NULL) {
  346. snprintf(cht_rt5640_codec_name, sizeof(cht_rt5640_codec_name),
  347. "%s%s", "i2c-", i2c_name);
  348. cht_dailink[dai_index].codec_name = cht_rt5640_codec_name;
  349. }
  350. snd_soc_card_set_drvdata(card, drv);
  351. ret_val = devm_snd_soc_register_card(&pdev->dev, card);
  352. if (ret_val) {
  353. dev_err(&pdev->dev,
  354. "snd_soc_register_card failed %d\n", ret_val);
  355. return ret_val;
  356. }
  357. platform_set_drvdata(pdev, card);
  358. return ret_val;
  359. }
  360. static struct platform_driver snd_cht_mc_driver = {
  361. .driver = {
  362. .name = "cht-bsw-rt5645",
  363. },
  364. .probe = snd_cht_mc_probe,
  365. };
  366. module_platform_driver(snd_cht_mc_driver)
  367. MODULE_DESCRIPTION("ASoC Intel(R) Braswell Machine driver");
  368. MODULE_AUTHOR("Fang, Yang A,N,Harshapriya");
  369. MODULE_LICENSE("GPL v2");
  370. MODULE_ALIAS("platform:cht-bsw-rt5645");