cht_bsw_max98090_ti.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. int i;
  41. for (i = 0; i < card->num_rtd; i++) {
  42. struct snd_soc_pcm_runtime *rtd;
  43. rtd = card->rtd + i;
  44. if (!strncmp(rtd->codec_dai->name, CHT_CODEC_DAI,
  45. strlen(CHT_CODEC_DAI)))
  46. return rtd->codec_dai;
  47. }
  48. return NULL;
  49. }
  50. static const struct snd_soc_dapm_widget cht_dapm_widgets[] = {
  51. SND_SOC_DAPM_HP("Headphone", NULL),
  52. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  53. SND_SOC_DAPM_MIC("Int Mic", NULL),
  54. SND_SOC_DAPM_SPK("Ext Spk", NULL),
  55. };
  56. static const struct snd_soc_dapm_route cht_audio_map[] = {
  57. {"IN34", NULL, "Headset Mic"},
  58. {"Headset Mic", NULL, "MICBIAS"},
  59. {"DMICL", NULL, "Int Mic"},
  60. {"Headphone", NULL, "HPL"},
  61. {"Headphone", NULL, "HPR"},
  62. {"Ext Spk", NULL, "SPKL"},
  63. {"Ext Spk", NULL, "SPKR"},
  64. {"HiFi Playback", NULL, "ssp2 Tx"},
  65. {"ssp2 Tx", NULL, "codec_out0"},
  66. {"ssp2 Tx", NULL, "codec_out1"},
  67. {"codec_in0", NULL, "ssp2 Rx" },
  68. {"codec_in1", NULL, "ssp2 Rx" },
  69. {"ssp2 Rx", NULL, "HiFi Capture"},
  70. };
  71. static const struct snd_kcontrol_new cht_mc_controls[] = {
  72. SOC_DAPM_PIN_SWITCH("Headphone"),
  73. SOC_DAPM_PIN_SWITCH("Headset Mic"),
  74. SOC_DAPM_PIN_SWITCH("Int Mic"),
  75. SOC_DAPM_PIN_SWITCH("Ext Spk"),
  76. };
  77. static int cht_aif1_hw_params(struct snd_pcm_substream *substream,
  78. struct snd_pcm_hw_params *params)
  79. {
  80. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  81. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  82. int ret;
  83. ret = snd_soc_dai_set_sysclk(codec_dai, M98090_REG_SYSTEM_CLOCK,
  84. CHT_PLAT_CLK_3_HZ, SND_SOC_CLOCK_IN);
  85. if (ret < 0) {
  86. dev_err(rtd->dev, "can't set codec sysclk: %d\n", ret);
  87. return ret;
  88. }
  89. return 0;
  90. }
  91. static int cht_ti_jack_event(struct notifier_block *nb,
  92. unsigned long event, void *data)
  93. {
  94. struct snd_soc_jack *jack = (struct snd_soc_jack *)data;
  95. struct snd_soc_dapm_context *dapm = &jack->card->dapm;
  96. if (event & SND_JACK_MICROPHONE) {
  97. snd_soc_dapm_force_enable_pin(dapm, "SHDN");
  98. snd_soc_dapm_force_enable_pin(dapm, "MICBIAS");
  99. snd_soc_dapm_sync(dapm);
  100. } else {
  101. snd_soc_dapm_disable_pin(dapm, "MICBIAS");
  102. snd_soc_dapm_disable_pin(dapm, "SHDN");
  103. snd_soc_dapm_sync(dapm);
  104. }
  105. return 0;
  106. }
  107. static struct notifier_block cht_jack_nb = {
  108. .notifier_call = cht_ti_jack_event,
  109. };
  110. static int cht_codec_init(struct snd_soc_pcm_runtime *runtime)
  111. {
  112. int ret;
  113. int jack_type;
  114. struct cht_mc_private *ctx = snd_soc_card_get_drvdata(runtime->card);
  115. struct snd_soc_jack *jack = &ctx->jack;
  116. /**
  117. * TI supports 4 butons headset detection
  118. * KEY_MEDIA
  119. * KEY_VOICECOMMAND
  120. * KEY_VOLUMEUP
  121. * KEY_VOLUMEDOWN
  122. */
  123. if (ctx->ts3a227e_present)
  124. jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
  125. SND_JACK_BTN_0 | SND_JACK_BTN_1 |
  126. SND_JACK_BTN_2 | SND_JACK_BTN_3;
  127. else
  128. jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE;
  129. ret = snd_soc_card_jack_new(runtime->card, "Headset Jack",
  130. jack_type, jack, NULL, 0);
  131. if (ret) {
  132. dev_err(runtime->dev, "Headset Jack creation failed %d\n", ret);
  133. return ret;
  134. }
  135. if (ctx->ts3a227e_present)
  136. snd_soc_jack_notifier_register(jack, &cht_jack_nb);
  137. return ret;
  138. }
  139. static int cht_codec_fixup(struct snd_soc_pcm_runtime *rtd,
  140. struct snd_pcm_hw_params *params)
  141. {
  142. struct snd_interval *rate = hw_param_interval(params,
  143. SNDRV_PCM_HW_PARAM_RATE);
  144. struct snd_interval *channels = hw_param_interval(params,
  145. SNDRV_PCM_HW_PARAM_CHANNELS);
  146. int ret = 0;
  147. unsigned int fmt = 0;
  148. ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, 0x3, 0x3, 2, 16);
  149. if (ret < 0) {
  150. dev_err(rtd->dev, "can't set cpu_dai slot fmt: %d\n", ret);
  151. return ret;
  152. }
  153. fmt = SND_SOC_DAIFMT_LEFT_J | SND_SOC_DAIFMT_NB_NF
  154. | SND_SOC_DAIFMT_CBS_CFS;
  155. ret = snd_soc_dai_set_fmt(rtd->cpu_dai, fmt);
  156. if (ret < 0) {
  157. dev_err(rtd->dev, "can't set cpu_dai set fmt: %d\n", ret);
  158. return ret;
  159. }
  160. /* The DSP will covert the FE rate to 48k, stereo, 24bits */
  161. rate->min = rate->max = 48000;
  162. channels->min = channels->max = 2;
  163. /* set SSP2 to 24-bit */
  164. params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
  165. return 0;
  166. }
  167. static unsigned int rates_48000[] = {
  168. 48000,
  169. };
  170. static struct snd_pcm_hw_constraint_list constraints_48000 = {
  171. .count = ARRAY_SIZE(rates_48000),
  172. .list = rates_48000,
  173. };
  174. static int cht_aif1_startup(struct snd_pcm_substream *substream)
  175. {
  176. return snd_pcm_hw_constraint_list(substream->runtime, 0,
  177. SNDRV_PCM_HW_PARAM_RATE,
  178. &constraints_48000);
  179. }
  180. static int cht_max98090_headset_init(struct snd_soc_component *component)
  181. {
  182. struct snd_soc_card *card = component->card;
  183. struct cht_mc_private *ctx = snd_soc_card_get_drvdata(card);
  184. return ts3a227e_enable_jack_detect(component, &ctx->jack);
  185. }
  186. static struct snd_soc_ops cht_aif1_ops = {
  187. .startup = cht_aif1_startup,
  188. };
  189. static struct snd_soc_ops cht_be_ssp2_ops = {
  190. .hw_params = cht_aif1_hw_params,
  191. };
  192. static struct snd_soc_aux_dev cht_max98090_headset_dev = {
  193. .name = "Headset Chip",
  194. .init = cht_max98090_headset_init,
  195. .codec_name = "i2c-104C227E:00",
  196. };
  197. static struct snd_soc_dai_link cht_dailink[] = {
  198. [MERR_DPCM_AUDIO] = {
  199. .name = "Audio Port",
  200. .stream_name = "Audio",
  201. .cpu_dai_name = "media-cpu-dai",
  202. .codec_dai_name = "snd-soc-dummy-dai",
  203. .codec_name = "snd-soc-dummy",
  204. .platform_name = "sst-mfld-platform",
  205. .nonatomic = true,
  206. .dynamic = 1,
  207. .dpcm_playback = 1,
  208. .dpcm_capture = 1,
  209. .ops = &cht_aif1_ops,
  210. },
  211. [MERR_DPCM_COMPR] = {
  212. .name = "Compressed Port",
  213. .stream_name = "Compress",
  214. .cpu_dai_name = "compress-cpu-dai",
  215. .codec_dai_name = "snd-soc-dummy-dai",
  216. .codec_name = "snd-soc-dummy",
  217. .platform_name = "sst-mfld-platform",
  218. },
  219. /* back ends */
  220. {
  221. .name = "SSP2-Codec",
  222. .be_id = 1,
  223. .cpu_dai_name = "ssp2-port",
  224. .platform_name = "sst-mfld-platform",
  225. .no_pcm = 1,
  226. .codec_dai_name = "HiFi",
  227. .codec_name = "i2c-193C9890:00",
  228. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
  229. | SND_SOC_DAIFMT_CBS_CFS,
  230. .init = cht_codec_init,
  231. .be_hw_params_fixup = cht_codec_fixup,
  232. .dpcm_playback = 1,
  233. .dpcm_capture = 1,
  234. .ops = &cht_be_ssp2_ops,
  235. },
  236. };
  237. /* SoC card */
  238. static struct snd_soc_card snd_soc_card_cht = {
  239. .name = "chtmax98090",
  240. .owner = THIS_MODULE,
  241. .dai_link = cht_dailink,
  242. .num_links = ARRAY_SIZE(cht_dailink),
  243. .aux_dev = &cht_max98090_headset_dev,
  244. .num_aux_devs = 1,
  245. .dapm_widgets = cht_dapm_widgets,
  246. .num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets),
  247. .dapm_routes = cht_audio_map,
  248. .num_dapm_routes = ARRAY_SIZE(cht_audio_map),
  249. .controls = cht_mc_controls,
  250. .num_controls = ARRAY_SIZE(cht_mc_controls),
  251. };
  252. static acpi_status snd_acpi_codec_match(acpi_handle handle, u32 level,
  253. void *context, void **ret)
  254. {
  255. *(bool *)context = true;
  256. return AE_OK;
  257. }
  258. static int snd_cht_mc_probe(struct platform_device *pdev)
  259. {
  260. int ret_val = 0;
  261. bool found = false;
  262. struct cht_mc_private *drv;
  263. drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_ATOMIC);
  264. if (!drv)
  265. return -ENOMEM;
  266. if (ACPI_SUCCESS(acpi_get_devices(
  267. "104C227E",
  268. snd_acpi_codec_match,
  269. &found, NULL)) && found) {
  270. drv->ts3a227e_present = true;
  271. } else {
  272. /* no need probe TI jack detection chip */
  273. snd_soc_card_cht.aux_dev = NULL;
  274. snd_soc_card_cht.num_aux_devs = 0;
  275. drv->ts3a227e_present = false;
  276. }
  277. /* register the soc card */
  278. snd_soc_card_cht.dev = &pdev->dev;
  279. snd_soc_card_set_drvdata(&snd_soc_card_cht, drv);
  280. ret_val = devm_snd_soc_register_card(&pdev->dev, &snd_soc_card_cht);
  281. if (ret_val) {
  282. dev_err(&pdev->dev,
  283. "snd_soc_register_card failed %d\n", ret_val);
  284. return ret_val;
  285. }
  286. platform_set_drvdata(pdev, &snd_soc_card_cht);
  287. return ret_val;
  288. }
  289. static struct platform_driver snd_cht_mc_driver = {
  290. .driver = {
  291. .name = "cht-bsw-max98090",
  292. },
  293. .probe = snd_cht_mc_probe,
  294. };
  295. module_platform_driver(snd_cht_mc_driver)
  296. MODULE_DESCRIPTION("ASoC Intel(R) Braswell Machine driver");
  297. MODULE_AUTHOR("Fang, Yang A <yang.a.fang@intel.com>");
  298. MODULE_LICENSE("GPL v2");
  299. MODULE_ALIAS("platform:cht-bsw-max98090");