bdw-rt5677.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * ASoC machine driver for Intel Broadwell platforms with RT5677 codec
  3. *
  4. * Copyright (c) 2014, The Chromium OS Authors. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/gpio/consumer.h>
  21. #include <linux/delay.h>
  22. #include <sound/core.h>
  23. #include <sound/pcm.h>
  24. #include <sound/soc.h>
  25. #include <sound/pcm_params.h>
  26. #include <sound/jack.h>
  27. #include "../common/sst-dsp.h"
  28. #include "../haswell/sst-haswell-ipc.h"
  29. #include "../../codecs/rt5677.h"
  30. struct bdw_rt5677_priv {
  31. struct gpio_desc *gpio_hp_en;
  32. struct snd_soc_codec *codec;
  33. };
  34. static int bdw_rt5677_event_hp(struct snd_soc_dapm_widget *w,
  35. struct snd_kcontrol *k, int event)
  36. {
  37. struct snd_soc_dapm_context *dapm = w->dapm;
  38. struct snd_soc_card *card = dapm->card;
  39. struct bdw_rt5677_priv *bdw_rt5677 = snd_soc_card_get_drvdata(card);
  40. if (SND_SOC_DAPM_EVENT_ON(event))
  41. msleep(70);
  42. gpiod_set_value_cansleep(bdw_rt5677->gpio_hp_en,
  43. SND_SOC_DAPM_EVENT_ON(event));
  44. return 0;
  45. }
  46. static const struct snd_soc_dapm_widget bdw_rt5677_widgets[] = {
  47. SND_SOC_DAPM_HP("Headphone", bdw_rt5677_event_hp),
  48. SND_SOC_DAPM_SPK("Speaker", NULL),
  49. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  50. SND_SOC_DAPM_MIC("Local DMICs", NULL),
  51. SND_SOC_DAPM_MIC("Remote DMICs", NULL),
  52. };
  53. static const struct snd_soc_dapm_route bdw_rt5677_map[] = {
  54. /* Speakers */
  55. {"Speaker", NULL, "PDM1L"},
  56. {"Speaker", NULL, "PDM1R"},
  57. /* Headset jack connectors */
  58. {"Headphone", NULL, "LOUT1"},
  59. {"Headphone", NULL, "LOUT2"},
  60. {"IN1P", NULL, "Headset Mic"},
  61. {"IN1N", NULL, "Headset Mic"},
  62. /* Digital MICs
  63. * Local DMICs: the two DMICs on the mainboard
  64. * Remote DMICs: the two DMICs on the camera module
  65. */
  66. {"DMIC L1", NULL, "Remote DMICs"},
  67. {"DMIC R1", NULL, "Remote DMICs"},
  68. {"DMIC L2", NULL, "Local DMICs"},
  69. {"DMIC R2", NULL, "Local DMICs"},
  70. /* CODEC BE connections */
  71. {"SSP0 CODEC IN", NULL, "AIF1 Capture"},
  72. {"AIF1 Playback", NULL, "SSP0 CODEC OUT"},
  73. };
  74. static const struct snd_kcontrol_new bdw_rt5677_controls[] = {
  75. SOC_DAPM_PIN_SWITCH("Speaker"),
  76. SOC_DAPM_PIN_SWITCH("Headphone"),
  77. SOC_DAPM_PIN_SWITCH("Headset Mic"),
  78. SOC_DAPM_PIN_SWITCH("Local DMICs"),
  79. SOC_DAPM_PIN_SWITCH("Remote DMICs"),
  80. };
  81. static struct snd_soc_jack headphone_jack;
  82. static struct snd_soc_jack mic_jack;
  83. static struct snd_soc_jack_pin headphone_jack_pin = {
  84. .pin = "Headphone",
  85. .mask = SND_JACK_HEADPHONE,
  86. };
  87. static struct snd_soc_jack_pin mic_jack_pin = {
  88. .pin = "Headset Mic",
  89. .mask = SND_JACK_MICROPHONE,
  90. };
  91. static struct snd_soc_jack_gpio headphone_jack_gpio = {
  92. .name = "plug-det",
  93. .report = SND_JACK_HEADPHONE,
  94. .debounce_time = 200,
  95. };
  96. static struct snd_soc_jack_gpio mic_jack_gpio = {
  97. .name = "mic-present",
  98. .report = SND_JACK_MICROPHONE,
  99. .debounce_time = 200,
  100. .invert = 1,
  101. };
  102. static int broadwell_ssp0_fixup(struct snd_soc_pcm_runtime *rtd,
  103. struct snd_pcm_hw_params *params)
  104. {
  105. struct snd_interval *rate = hw_param_interval(params,
  106. SNDRV_PCM_HW_PARAM_RATE);
  107. struct snd_interval *channels = hw_param_interval(params,
  108. SNDRV_PCM_HW_PARAM_CHANNELS);
  109. /* The ADSP will covert the FE rate to 48k, stereo */
  110. rate->min = rate->max = 48000;
  111. channels->min = channels->max = 2;
  112. /* set SSP0 to 16 bit */
  113. snd_mask_set(&params->masks[SNDRV_PCM_HW_PARAM_FORMAT -
  114. SNDRV_PCM_HW_PARAM_FIRST_MASK],
  115. SNDRV_PCM_FORMAT_S16_LE);
  116. return 0;
  117. }
  118. static int bdw_rt5677_hw_params(struct snd_pcm_substream *substream,
  119. struct snd_pcm_hw_params *params)
  120. {
  121. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  122. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  123. int ret;
  124. ret = snd_soc_dai_set_sysclk(codec_dai, RT5677_SCLK_S_MCLK, 24576000,
  125. SND_SOC_CLOCK_IN);
  126. if (ret < 0) {
  127. dev_err(rtd->dev, "can't set codec sysclk configuration\n");
  128. return ret;
  129. }
  130. return ret;
  131. }
  132. static struct snd_soc_ops bdw_rt5677_ops = {
  133. .hw_params = bdw_rt5677_hw_params,
  134. };
  135. static int bdw_rt5677_rtd_init(struct snd_soc_pcm_runtime *rtd)
  136. {
  137. struct sst_pdata *pdata = dev_get_platdata(rtd->platform->dev);
  138. struct sst_hsw *broadwell = pdata->dsp;
  139. int ret;
  140. /* Set ADSP SSP port settings */
  141. ret = sst_hsw_device_set_config(broadwell, SST_HSW_DEVICE_SSP_0,
  142. SST_HSW_DEVICE_MCLK_FREQ_24_MHZ,
  143. SST_HSW_DEVICE_CLOCK_MASTER, 9);
  144. if (ret < 0) {
  145. dev_err(rtd->dev, "error: failed to set device config\n");
  146. return ret;
  147. }
  148. return 0;
  149. }
  150. static int bdw_rt5677_init(struct snd_soc_pcm_runtime *rtd)
  151. {
  152. struct bdw_rt5677_priv *bdw_rt5677 =
  153. snd_soc_card_get_drvdata(rtd->card);
  154. struct snd_soc_codec *codec = rtd->codec;
  155. struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec);
  156. /* Enable codec ASRC function for Stereo DAC/Stereo1 ADC/DMIC/I2S1.
  157. * The ASRC clock source is clk_i2s1_asrc.
  158. */
  159. rt5677_sel_asrc_clk_src(codec, RT5677_DA_STEREO_FILTER |
  160. RT5677_AD_STEREO1_FILTER | RT5677_I2S1_SOURCE,
  161. RT5677_CLK_SEL_I2S1_ASRC);
  162. /* Request rt5677 GPIO for headphone amp control */
  163. bdw_rt5677->gpio_hp_en = devm_gpiod_get_index(codec->dev,
  164. "headphone-enable", 0, 0);
  165. if (IS_ERR(bdw_rt5677->gpio_hp_en)) {
  166. dev_err(codec->dev, "Can't find HP_AMP_SHDN_L gpio\n");
  167. return PTR_ERR(bdw_rt5677->gpio_hp_en);
  168. }
  169. gpiod_direction_output(bdw_rt5677->gpio_hp_en, 0);
  170. /* Create and initialize headphone jack */
  171. if (!snd_soc_card_jack_new(rtd->card, "Headphone Jack",
  172. SND_JACK_HEADPHONE, &headphone_jack,
  173. &headphone_jack_pin, 1)) {
  174. headphone_jack_gpio.gpiod_dev = codec->dev;
  175. if (snd_soc_jack_add_gpios(&headphone_jack, 1,
  176. &headphone_jack_gpio))
  177. dev_err(codec->dev, "Can't add headphone jack gpio\n");
  178. } else {
  179. dev_err(codec->dev, "Can't create headphone jack\n");
  180. }
  181. /* Create and initialize mic jack */
  182. if (!snd_soc_card_jack_new(rtd->card, "Mic Jack",
  183. SND_JACK_MICROPHONE, &mic_jack,
  184. &mic_jack_pin, 1)) {
  185. mic_jack_gpio.gpiod_dev = codec->dev;
  186. if (snd_soc_jack_add_gpios(&mic_jack, 1, &mic_jack_gpio))
  187. dev_err(codec->dev, "Can't add mic jack gpio\n");
  188. } else {
  189. dev_err(codec->dev, "Can't create mic jack\n");
  190. }
  191. bdw_rt5677->codec = codec;
  192. snd_soc_dapm_force_enable_pin(dapm, "MICBIAS1");
  193. return 0;
  194. }
  195. /* broadwell digital audio interface glue - connects codec <--> CPU */
  196. static struct snd_soc_dai_link bdw_rt5677_dais[] = {
  197. /* Front End DAI links */
  198. {
  199. .name = "System PCM",
  200. .stream_name = "System Playback/Capture",
  201. .cpu_dai_name = "System Pin",
  202. .platform_name = "haswell-pcm-audio",
  203. .dynamic = 1,
  204. .codec_name = "snd-soc-dummy",
  205. .codec_dai_name = "snd-soc-dummy-dai",
  206. .init = bdw_rt5677_rtd_init,
  207. .trigger = {
  208. SND_SOC_DPCM_TRIGGER_POST,
  209. SND_SOC_DPCM_TRIGGER_POST
  210. },
  211. .dpcm_capture = 1,
  212. .dpcm_playback = 1,
  213. },
  214. /* Back End DAI links */
  215. {
  216. /* SSP0 - Codec */
  217. .name = "Codec",
  218. .id = 0,
  219. .cpu_dai_name = "snd-soc-dummy-dai",
  220. .platform_name = "snd-soc-dummy",
  221. .no_pcm = 1,
  222. .codec_name = "i2c-RT5677CE:00",
  223. .codec_dai_name = "rt5677-aif1",
  224. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  225. SND_SOC_DAIFMT_CBS_CFS,
  226. .ignore_suspend = 1,
  227. .ignore_pmdown_time = 1,
  228. .be_hw_params_fixup = broadwell_ssp0_fixup,
  229. .ops = &bdw_rt5677_ops,
  230. .dpcm_playback = 1,
  231. .dpcm_capture = 1,
  232. .init = bdw_rt5677_init,
  233. },
  234. };
  235. static int bdw_rt5677_suspend_pre(struct snd_soc_card *card)
  236. {
  237. struct bdw_rt5677_priv *bdw_rt5677 = snd_soc_card_get_drvdata(card);
  238. struct snd_soc_dapm_context *dapm;
  239. if (bdw_rt5677->codec) {
  240. dapm = snd_soc_codec_get_dapm(bdw_rt5677->codec);
  241. snd_soc_dapm_disable_pin(dapm, "MICBIAS1");
  242. }
  243. return 0;
  244. }
  245. static int bdw_rt5677_resume_post(struct snd_soc_card *card)
  246. {
  247. struct bdw_rt5677_priv *bdw_rt5677 = snd_soc_card_get_drvdata(card);
  248. struct snd_soc_dapm_context *dapm;
  249. if (bdw_rt5677->codec) {
  250. dapm = snd_soc_codec_get_dapm(bdw_rt5677->codec);
  251. snd_soc_dapm_force_enable_pin(dapm, "MICBIAS1");
  252. }
  253. return 0;
  254. }
  255. /* ASoC machine driver for Broadwell DSP + RT5677 */
  256. static struct snd_soc_card bdw_rt5677_card = {
  257. .name = "bdw-rt5677",
  258. .owner = THIS_MODULE,
  259. .dai_link = bdw_rt5677_dais,
  260. .num_links = ARRAY_SIZE(bdw_rt5677_dais),
  261. .dapm_widgets = bdw_rt5677_widgets,
  262. .num_dapm_widgets = ARRAY_SIZE(bdw_rt5677_widgets),
  263. .dapm_routes = bdw_rt5677_map,
  264. .num_dapm_routes = ARRAY_SIZE(bdw_rt5677_map),
  265. .controls = bdw_rt5677_controls,
  266. .num_controls = ARRAY_SIZE(bdw_rt5677_controls),
  267. .fully_routed = true,
  268. .suspend_pre = bdw_rt5677_suspend_pre,
  269. .resume_post = bdw_rt5677_resume_post,
  270. };
  271. static int bdw_rt5677_probe(struct platform_device *pdev)
  272. {
  273. struct bdw_rt5677_priv *bdw_rt5677;
  274. bdw_rt5677_card.dev = &pdev->dev;
  275. /* Allocate driver private struct */
  276. bdw_rt5677 = devm_kzalloc(&pdev->dev, sizeof(struct bdw_rt5677_priv),
  277. GFP_KERNEL);
  278. if (!bdw_rt5677) {
  279. dev_err(&pdev->dev, "Can't allocate bdw_rt5677\n");
  280. return -ENOMEM;
  281. }
  282. snd_soc_card_set_drvdata(&bdw_rt5677_card, bdw_rt5677);
  283. return devm_snd_soc_register_card(&pdev->dev, &bdw_rt5677_card);
  284. }
  285. static struct platform_driver bdw_rt5677_audio = {
  286. .probe = bdw_rt5677_probe,
  287. .driver = {
  288. .name = "bdw-rt5677",
  289. },
  290. };
  291. module_platform_driver(bdw_rt5677_audio)
  292. /* Module information */
  293. MODULE_AUTHOR("Ben Zhang");
  294. MODULE_DESCRIPTION("Intel Broadwell RT5677 machine driver");
  295. MODULE_LICENSE("GPL v2");
  296. MODULE_ALIAS("platform:bdw-rt5677");