cht_bsw_max98090_ti.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * cht-bsw-max98090.c - ASoc Machine driver for Intel Cherryview-based
  3. * platforms Cherrytrail and Braswell, with max98090 & TI codec.
  4. *
  5. * Copyright (C) 2015 Intel Corp
  6. * Author: Fang, Yang A <yang.a.fang@intel.com>
  7. * This file is modified from cht_bsw_rt5645.c
  8. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; version 2 of the License.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  20. */
  21. #include <linux/module.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/slab.h>
  24. #include <linux/acpi.h>
  25. #include <sound/pcm.h>
  26. #include <sound/pcm_params.h>
  27. #include <sound/soc.h>
  28. #include <sound/jack.h>
  29. #include "../../codecs/max98090.h"
  30. #include "../atom/sst-atom-controls.h"
  31. #include "../../codecs/ts3a227e.h"
  32. #define CHT_PLAT_CLK_3_HZ 19200000
  33. #define CHT_CODEC_DAI "HiFi"
  34. struct cht_mc_private {
  35. struct snd_soc_jack jack;
  36. bool ts3a227e_present;
  37. };
  38. static inline struct snd_soc_dai *cht_get_codec_dai(struct snd_soc_card *card)
  39. {
  40. struct snd_soc_pcm_runtime *rtd;
  41. list_for_each_entry(rtd, &card->rtd_list, list) {
  42. if (!strncmp(rtd->codec_dai->name, CHT_CODEC_DAI,
  43. strlen(CHT_CODEC_DAI)))
  44. return rtd->codec_dai;
  45. }
  46. return NULL;
  47. }
  48. static const struct snd_soc_dapm_widget cht_dapm_widgets[] = {
  49. SND_SOC_DAPM_HP("Headphone", NULL),
  50. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  51. SND_SOC_DAPM_MIC("Int Mic", NULL),
  52. SND_SOC_DAPM_SPK("Ext Spk", NULL),
  53. };
  54. static const struct snd_soc_dapm_route cht_audio_map[] = {
  55. {"IN34", NULL, "Headset Mic"},
  56. {"Headset Mic", NULL, "MICBIAS"},
  57. {"DMICL", NULL, "Int Mic"},
  58. {"Headphone", NULL, "HPL"},
  59. {"Headphone", NULL, "HPR"},
  60. {"Ext Spk", NULL, "SPKL"},
  61. {"Ext Spk", NULL, "SPKR"},
  62. {"HiFi Playback", NULL, "ssp2 Tx"},
  63. {"ssp2 Tx", NULL, "codec_out0"},
  64. {"ssp2 Tx", NULL, "codec_out1"},
  65. {"codec_in0", NULL, "ssp2 Rx" },
  66. {"codec_in1", NULL, "ssp2 Rx" },
  67. {"ssp2 Rx", NULL, "HiFi Capture"},
  68. };
  69. static const struct snd_kcontrol_new cht_mc_controls[] = {
  70. SOC_DAPM_PIN_SWITCH("Headphone"),
  71. SOC_DAPM_PIN_SWITCH("Headset Mic"),
  72. SOC_DAPM_PIN_SWITCH("Int Mic"),
  73. SOC_DAPM_PIN_SWITCH("Ext Spk"),
  74. };
  75. static int cht_aif1_hw_params(struct snd_pcm_substream *substream,
  76. struct snd_pcm_hw_params *params)
  77. {
  78. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  79. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  80. int ret;
  81. ret = snd_soc_dai_set_sysclk(codec_dai, M98090_REG_SYSTEM_CLOCK,
  82. CHT_PLAT_CLK_3_HZ, SND_SOC_CLOCK_IN);
  83. if (ret < 0) {
  84. dev_err(rtd->dev, "can't set codec sysclk: %d\n", ret);
  85. return ret;
  86. }
  87. return 0;
  88. }
  89. static int cht_ti_jack_event(struct notifier_block *nb,
  90. unsigned long event, void *data)
  91. {
  92. struct snd_soc_jack *jack = (struct snd_soc_jack *)data;
  93. struct snd_soc_dapm_context *dapm = &jack->card->dapm;
  94. if (event & SND_JACK_MICROPHONE) {
  95. snd_soc_dapm_force_enable_pin(dapm, "SHDN");
  96. snd_soc_dapm_force_enable_pin(dapm, "MICBIAS");
  97. snd_soc_dapm_sync(dapm);
  98. } else {
  99. snd_soc_dapm_disable_pin(dapm, "MICBIAS");
  100. snd_soc_dapm_disable_pin(dapm, "SHDN");
  101. snd_soc_dapm_sync(dapm);
  102. }
  103. return 0;
  104. }
  105. static struct notifier_block cht_jack_nb = {
  106. .notifier_call = cht_ti_jack_event,
  107. };
  108. static int cht_codec_init(struct snd_soc_pcm_runtime *runtime)
  109. {
  110. int ret;
  111. int jack_type;
  112. struct cht_mc_private *ctx = snd_soc_card_get_drvdata(runtime->card);
  113. struct snd_soc_jack *jack = &ctx->jack;
  114. /**
  115. * TI supports 4 butons headset detection
  116. * KEY_MEDIA
  117. * KEY_VOICECOMMAND
  118. * KEY_VOLUMEUP
  119. * KEY_VOLUMEDOWN
  120. */
  121. if (ctx->ts3a227e_present)
  122. jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
  123. SND_JACK_BTN_0 | SND_JACK_BTN_1 |
  124. SND_JACK_BTN_2 | SND_JACK_BTN_3;
  125. else
  126. jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE;
  127. ret = snd_soc_card_jack_new(runtime->card, "Headset Jack",
  128. jack_type, jack, NULL, 0);
  129. if (ret) {
  130. dev_err(runtime->dev, "Headset Jack creation failed %d\n", ret);
  131. return ret;
  132. }
  133. if (ctx->ts3a227e_present)
  134. snd_soc_jack_notifier_register(jack, &cht_jack_nb);
  135. return ret;
  136. }
  137. static int cht_codec_fixup(struct snd_soc_pcm_runtime *rtd,
  138. struct snd_pcm_hw_params *params)
  139. {
  140. struct snd_interval *rate = hw_param_interval(params,
  141. SNDRV_PCM_HW_PARAM_RATE);
  142. struct snd_interval *channels = hw_param_interval(params,
  143. SNDRV_PCM_HW_PARAM_CHANNELS);
  144. int ret = 0;
  145. unsigned int fmt = 0;
  146. ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, 0x3, 0x3, 2, 16);
  147. if (ret < 0) {
  148. dev_err(rtd->dev, "can't set cpu_dai slot fmt: %d\n", ret);
  149. return ret;
  150. }
  151. fmt = SND_SOC_DAIFMT_LEFT_J | SND_SOC_DAIFMT_NB_NF
  152. | SND_SOC_DAIFMT_CBS_CFS;
  153. ret = snd_soc_dai_set_fmt(rtd->cpu_dai, fmt);
  154. if (ret < 0) {
  155. dev_err(rtd->dev, "can't set cpu_dai set fmt: %d\n", ret);
  156. return ret;
  157. }
  158. /* The DSP will covert the FE rate to 48k, stereo, 24bits */
  159. rate->min = rate->max = 48000;
  160. channels->min = channels->max = 2;
  161. /* set SSP2 to 24-bit */
  162. params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
  163. return 0;
  164. }
  165. static int cht_aif1_startup(struct snd_pcm_substream *substream)
  166. {
  167. return snd_pcm_hw_constraint_single(substream->runtime,
  168. SNDRV_PCM_HW_PARAM_RATE, 48000);
  169. }
  170. static int cht_max98090_headset_init(struct snd_soc_component *component)
  171. {
  172. struct snd_soc_card *card = component->card;
  173. struct cht_mc_private *ctx = snd_soc_card_get_drvdata(card);
  174. return ts3a227e_enable_jack_detect(component, &ctx->jack);
  175. }
  176. static const struct snd_soc_ops cht_aif1_ops = {
  177. .startup = cht_aif1_startup,
  178. };
  179. static const struct snd_soc_ops cht_be_ssp2_ops = {
  180. .hw_params = cht_aif1_hw_params,
  181. };
  182. static struct snd_soc_aux_dev cht_max98090_headset_dev = {
  183. .name = "Headset Chip",
  184. .init = cht_max98090_headset_init,
  185. .codec_name = "i2c-104C227E:00",
  186. };
  187. static struct snd_soc_dai_link cht_dailink[] = {
  188. [MERR_DPCM_AUDIO] = {
  189. .name = "Audio Port",
  190. .stream_name = "Audio",
  191. .cpu_dai_name = "media-cpu-dai",
  192. .codec_dai_name = "snd-soc-dummy-dai",
  193. .codec_name = "snd-soc-dummy",
  194. .platform_name = "sst-mfld-platform",
  195. .nonatomic = true,
  196. .dynamic = 1,
  197. .dpcm_playback = 1,
  198. .dpcm_capture = 1,
  199. .ops = &cht_aif1_ops,
  200. },
  201. [MERR_DPCM_DEEP_BUFFER] = {
  202. .name = "Deep-Buffer Audio Port",
  203. .stream_name = "Deep-Buffer Audio",
  204. .cpu_dai_name = "deepbuffer-cpu-dai",
  205. .codec_dai_name = "snd-soc-dummy-dai",
  206. .codec_name = "snd-soc-dummy",
  207. .platform_name = "sst-mfld-platform",
  208. .nonatomic = true,
  209. .dynamic = 1,
  210. .dpcm_playback = 1,
  211. .ops = &cht_aif1_ops,
  212. },
  213. [MERR_DPCM_COMPR] = {
  214. .name = "Compressed Port",
  215. .stream_name = "Compress",
  216. .cpu_dai_name = "compress-cpu-dai",
  217. .codec_dai_name = "snd-soc-dummy-dai",
  218. .codec_name = "snd-soc-dummy",
  219. .platform_name = "sst-mfld-platform",
  220. },
  221. /* back ends */
  222. {
  223. .name = "SSP2-Codec",
  224. .id = 1,
  225. .cpu_dai_name = "ssp2-port",
  226. .platform_name = "sst-mfld-platform",
  227. .no_pcm = 1,
  228. .codec_dai_name = "HiFi",
  229. .codec_name = "i2c-193C9890:00",
  230. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
  231. | SND_SOC_DAIFMT_CBS_CFS,
  232. .init = cht_codec_init,
  233. .be_hw_params_fixup = cht_codec_fixup,
  234. .dpcm_playback = 1,
  235. .dpcm_capture = 1,
  236. .ops = &cht_be_ssp2_ops,
  237. },
  238. };
  239. /* SoC card */
  240. static struct snd_soc_card snd_soc_card_cht = {
  241. .name = "chtmax98090",
  242. .owner = THIS_MODULE,
  243. .dai_link = cht_dailink,
  244. .num_links = ARRAY_SIZE(cht_dailink),
  245. .aux_dev = &cht_max98090_headset_dev,
  246. .num_aux_devs = 1,
  247. .dapm_widgets = cht_dapm_widgets,
  248. .num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets),
  249. .dapm_routes = cht_audio_map,
  250. .num_dapm_routes = ARRAY_SIZE(cht_audio_map),
  251. .controls = cht_mc_controls,
  252. .num_controls = ARRAY_SIZE(cht_mc_controls),
  253. };
  254. static int snd_cht_mc_probe(struct platform_device *pdev)
  255. {
  256. int ret_val = 0;
  257. struct cht_mc_private *drv;
  258. drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_ATOMIC);
  259. if (!drv)
  260. return -ENOMEM;
  261. drv->ts3a227e_present = acpi_dev_found("104C227E");
  262. if (!drv->ts3a227e_present) {
  263. /* no need probe TI jack detection chip */
  264. snd_soc_card_cht.aux_dev = NULL;
  265. snd_soc_card_cht.num_aux_devs = 0;
  266. }
  267. /* register the soc card */
  268. snd_soc_card_cht.dev = &pdev->dev;
  269. snd_soc_card_set_drvdata(&snd_soc_card_cht, drv);
  270. ret_val = devm_snd_soc_register_card(&pdev->dev, &snd_soc_card_cht);
  271. if (ret_val) {
  272. dev_err(&pdev->dev,
  273. "snd_soc_register_card failed %d\n", ret_val);
  274. return ret_val;
  275. }
  276. platform_set_drvdata(pdev, &snd_soc_card_cht);
  277. return ret_val;
  278. }
  279. static struct platform_driver snd_cht_mc_driver = {
  280. .driver = {
  281. .name = "cht-bsw-max98090",
  282. },
  283. .probe = snd_cht_mc_probe,
  284. };
  285. module_platform_driver(snd_cht_mc_driver)
  286. MODULE_DESCRIPTION("ASoC Intel(R) Braswell Machine driver");
  287. MODULE_AUTHOR("Fang, Yang A <yang.a.fang@intel.com>");
  288. MODULE_LICENSE("GPL v2");
  289. MODULE_ALIAS("platform:cht-bsw-max98090");