bdw-rt5677.c 10 KB

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