cht_bsw_max98090_ti.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. if (ctx->ts3a227e_present) {
  117. /*
  118. * The jack has already been created in the
  119. * cht_max98090_headset_init() function.
  120. */
  121. snd_soc_jack_notifier_register(jack, &cht_jack_nb);
  122. return 0;
  123. }
  124. jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE;
  125. ret = snd_soc_card_jack_new(runtime->card, "Headset Jack",
  126. jack_type, jack, NULL, 0);
  127. if (ret) {
  128. dev_err(runtime->dev, "Headset Jack creation failed %d\n", ret);
  129. return ret;
  130. }
  131. if (ctx->ts3a227e_present)
  132. snd_soc_jack_notifier_register(jack, &cht_jack_nb);
  133. return ret;
  134. }
  135. static int cht_codec_fixup(struct snd_soc_pcm_runtime *rtd,
  136. struct snd_pcm_hw_params *params)
  137. {
  138. struct snd_interval *rate = hw_param_interval(params,
  139. SNDRV_PCM_HW_PARAM_RATE);
  140. struct snd_interval *channels = hw_param_interval(params,
  141. SNDRV_PCM_HW_PARAM_CHANNELS);
  142. int ret = 0;
  143. unsigned int fmt = 0;
  144. ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, 0x3, 0x3, 2, 16);
  145. if (ret < 0) {
  146. dev_err(rtd->dev, "can't set cpu_dai slot fmt: %d\n", ret);
  147. return ret;
  148. }
  149. fmt = SND_SOC_DAIFMT_LEFT_J | SND_SOC_DAIFMT_NB_NF
  150. | SND_SOC_DAIFMT_CBS_CFS;
  151. ret = snd_soc_dai_set_fmt(rtd->cpu_dai, fmt);
  152. if (ret < 0) {
  153. dev_err(rtd->dev, "can't set cpu_dai set fmt: %d\n", ret);
  154. return ret;
  155. }
  156. /* The DSP will covert the FE rate to 48k, stereo, 24bits */
  157. rate->min = rate->max = 48000;
  158. channels->min = channels->max = 2;
  159. /* set SSP2 to 24-bit */
  160. params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
  161. return 0;
  162. }
  163. static int cht_aif1_startup(struct snd_pcm_substream *substream)
  164. {
  165. return snd_pcm_hw_constraint_single(substream->runtime,
  166. SNDRV_PCM_HW_PARAM_RATE, 48000);
  167. }
  168. static int cht_max98090_headset_init(struct snd_soc_component *component)
  169. {
  170. struct snd_soc_card *card = component->card;
  171. struct cht_mc_private *ctx = snd_soc_card_get_drvdata(card);
  172. struct snd_soc_jack *jack = &ctx->jack;
  173. int jack_type;
  174. int ret;
  175. /*
  176. * TI supports 4 butons headset detection
  177. * KEY_MEDIA
  178. * KEY_VOICECOMMAND
  179. * KEY_VOLUMEUP
  180. * KEY_VOLUMEDOWN
  181. */
  182. jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
  183. SND_JACK_BTN_0 | SND_JACK_BTN_1 |
  184. SND_JACK_BTN_2 | SND_JACK_BTN_3;
  185. ret = snd_soc_card_jack_new(card, "Headset Jack", jack_type,
  186. jack, NULL, 0);
  187. if (ret) {
  188. dev_err(card->dev, "Headset Jack creation failed %d\n", ret);
  189. return ret;
  190. }
  191. return ts3a227e_enable_jack_detect(component, &ctx->jack);
  192. }
  193. static struct snd_soc_ops cht_aif1_ops = {
  194. .startup = cht_aif1_startup,
  195. };
  196. static struct snd_soc_ops cht_be_ssp2_ops = {
  197. .hw_params = cht_aif1_hw_params,
  198. };
  199. static struct snd_soc_aux_dev cht_max98090_headset_dev = {
  200. .name = "Headset Chip",
  201. .init = cht_max98090_headset_init,
  202. .codec_name = "i2c-104C227E:00",
  203. };
  204. static struct snd_soc_dai_link cht_dailink[] = {
  205. [MERR_DPCM_AUDIO] = {
  206. .name = "Audio Port",
  207. .stream_name = "Audio",
  208. .cpu_dai_name = "media-cpu-dai",
  209. .codec_dai_name = "snd-soc-dummy-dai",
  210. .codec_name = "snd-soc-dummy",
  211. .platform_name = "sst-mfld-platform",
  212. .nonatomic = true,
  213. .dynamic = 1,
  214. .dpcm_playback = 1,
  215. .dpcm_capture = 1,
  216. .ops = &cht_aif1_ops,
  217. },
  218. [MERR_DPCM_COMPR] = {
  219. .name = "Compressed Port",
  220. .stream_name = "Compress",
  221. .cpu_dai_name = "compress-cpu-dai",
  222. .codec_dai_name = "snd-soc-dummy-dai",
  223. .codec_name = "snd-soc-dummy",
  224. .platform_name = "sst-mfld-platform",
  225. },
  226. /* back ends */
  227. {
  228. .name = "SSP2-Codec",
  229. .be_id = 1,
  230. .cpu_dai_name = "ssp2-port",
  231. .platform_name = "sst-mfld-platform",
  232. .no_pcm = 1,
  233. .codec_dai_name = "HiFi",
  234. .codec_name = "i2c-193C9890:00",
  235. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
  236. | SND_SOC_DAIFMT_CBS_CFS,
  237. .init = cht_codec_init,
  238. .be_hw_params_fixup = cht_codec_fixup,
  239. .dpcm_playback = 1,
  240. .dpcm_capture = 1,
  241. .ops = &cht_be_ssp2_ops,
  242. },
  243. };
  244. /* SoC card */
  245. static struct snd_soc_card snd_soc_card_cht = {
  246. .name = "chtmax98090",
  247. .owner = THIS_MODULE,
  248. .dai_link = cht_dailink,
  249. .num_links = ARRAY_SIZE(cht_dailink),
  250. .aux_dev = &cht_max98090_headset_dev,
  251. .num_aux_devs = 1,
  252. .dapm_widgets = cht_dapm_widgets,
  253. .num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets),
  254. .dapm_routes = cht_audio_map,
  255. .num_dapm_routes = ARRAY_SIZE(cht_audio_map),
  256. .controls = cht_mc_controls,
  257. .num_controls = ARRAY_SIZE(cht_mc_controls),
  258. };
  259. static acpi_status snd_acpi_codec_match(acpi_handle handle, u32 level,
  260. void *context, void **ret)
  261. {
  262. *(bool *)context = true;
  263. return AE_OK;
  264. }
  265. static int snd_cht_mc_probe(struct platform_device *pdev)
  266. {
  267. int ret_val = 0;
  268. bool found = false;
  269. struct cht_mc_private *drv;
  270. drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_ATOMIC);
  271. if (!drv)
  272. return -ENOMEM;
  273. if (ACPI_SUCCESS(acpi_get_devices(
  274. "104C227E",
  275. snd_acpi_codec_match,
  276. &found, NULL)) && found) {
  277. drv->ts3a227e_present = true;
  278. } else {
  279. /* no need probe TI jack detection chip */
  280. snd_soc_card_cht.aux_dev = NULL;
  281. snd_soc_card_cht.num_aux_devs = 0;
  282. drv->ts3a227e_present = false;
  283. }
  284. /* register the soc card */
  285. snd_soc_card_cht.dev = &pdev->dev;
  286. snd_soc_card_set_drvdata(&snd_soc_card_cht, drv);
  287. ret_val = devm_snd_soc_register_card(&pdev->dev, &snd_soc_card_cht);
  288. if (ret_val) {
  289. dev_err(&pdev->dev,
  290. "snd_soc_register_card failed %d\n", ret_val);
  291. return ret_val;
  292. }
  293. platform_set_drvdata(pdev, &snd_soc_card_cht);
  294. return ret_val;
  295. }
  296. static struct platform_driver snd_cht_mc_driver = {
  297. .driver = {
  298. .name = "cht-bsw-max98090",
  299. },
  300. .probe = snd_cht_mc_probe,
  301. };
  302. module_platform_driver(snd_cht_mc_driver)
  303. MODULE_DESCRIPTION("ASoC Intel(R) Braswell Machine driver");
  304. MODULE_AUTHOR("Fang, Yang A <yang.a.fang@intel.com>");
  305. MODULE_LICENSE("GPL v2");
  306. MODULE_ALIAS("platform:cht-bsw-max98090");