sof_cs42l42.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. // Copyright(c) 2021 Intel Corporation.
  3. /*
  4. * Intel SOF Machine Driver with Cirrus Logic CS42L42 Codec
  5. * and speaker codec MAX98357A
  6. */
  7. #include <linux/i2c.h>
  8. #include <linux/input.h>
  9. #include <linux/module.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/regulator/consumer.h>
  12. #include <linux/dmi.h>
  13. #include <sound/core.h>
  14. #include <sound/jack.h>
  15. #include <sound/pcm.h>
  16. #include <sound/pcm_params.h>
  17. #include <sound/soc.h>
  18. #include <sound/sof.h>
  19. #include <sound/soc-acpi.h>
  20. #include <dt-bindings/sound/cs42l42.h>
  21. #include "../../codecs/hdac_hdmi.h"
  22. #include "../common/soc-intel-quirks.h"
  23. #include "hda_dsp_common.h"
  24. #include "sof_maxim_common.h"
  25. #define NAME_SIZE 32
  26. #define SOF_CS42L42_SSP_CODEC(quirk) ((quirk) & GENMASK(2, 0))
  27. #define SOF_CS42L42_SSP_CODEC_MASK (GENMASK(2, 0))
  28. #define SOF_SPEAKER_AMP_PRESENT BIT(3)
  29. #define SOF_CS42L42_SSP_AMP_SHIFT 4
  30. #define SOF_CS42L42_SSP_AMP_MASK (GENMASK(6, 4))
  31. #define SOF_CS42L42_SSP_AMP(quirk) \
  32. (((quirk) << SOF_CS42L42_SSP_AMP_SHIFT) & SOF_CS42L42_SSP_AMP_MASK)
  33. #define SOF_CS42L42_NUM_HDMIDEV_SHIFT 7
  34. #define SOF_CS42L42_NUM_HDMIDEV_MASK (GENMASK(9, 7))
  35. #define SOF_CS42L42_NUM_HDMIDEV(quirk) \
  36. (((quirk) << SOF_CS42L42_NUM_HDMIDEV_SHIFT) & SOF_CS42L42_NUM_HDMIDEV_MASK)
  37. #define SOF_CS42L42_DAILINK_SHIFT 10
  38. #define SOF_CS42L42_DAILINK_MASK (GENMASK(24, 10))
  39. #define SOF_CS42L42_DAILINK(link1, link2, link3, link4, link5) \
  40. ((((link1) | ((link2) << 3) | ((link3) << 6) | ((link4) << 9) | ((link5) << 12)) << SOF_CS42L42_DAILINK_SHIFT) & SOF_CS42L42_DAILINK_MASK)
  41. #define SOF_BT_OFFLOAD_PRESENT BIT(25)
  42. #define SOF_CS42L42_SSP_BT_SHIFT 26
  43. #define SOF_CS42L42_SSP_BT_MASK (GENMASK(28, 26))
  44. #define SOF_CS42L42_SSP_BT(quirk) \
  45. (((quirk) << SOF_CS42L42_SSP_BT_SHIFT) & SOF_CS42L42_SSP_BT_MASK)
  46. #define SOF_MAX98357A_SPEAKER_AMP_PRESENT BIT(29)
  47. #define SOF_MAX98360A_SPEAKER_AMP_PRESENT BIT(30)
  48. enum {
  49. LINK_NONE = 0,
  50. LINK_HP = 1,
  51. LINK_SPK = 2,
  52. LINK_DMIC = 3,
  53. LINK_HDMI = 4,
  54. LINK_BT = 5,
  55. };
  56. static struct snd_soc_jack_pin jack_pins[] = {
  57. {
  58. .pin = "Headphone Jack",
  59. .mask = SND_JACK_HEADPHONE,
  60. },
  61. {
  62. .pin = "Headset Mic",
  63. .mask = SND_JACK_MICROPHONE,
  64. },
  65. };
  66. /* Default: SSP2 */
  67. static unsigned long sof_cs42l42_quirk = SOF_CS42L42_SSP_CODEC(2);
  68. struct sof_hdmi_pcm {
  69. struct list_head head;
  70. struct snd_soc_dai *codec_dai;
  71. struct snd_soc_jack hdmi_jack;
  72. int device;
  73. };
  74. struct sof_card_private {
  75. struct snd_soc_jack headset_jack;
  76. struct list_head hdmi_pcm_list;
  77. bool common_hdmi_codec_drv;
  78. };
  79. static int sof_hdmi_init(struct snd_soc_pcm_runtime *rtd)
  80. {
  81. struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
  82. struct snd_soc_dai *dai = asoc_rtd_to_codec(rtd, 0);
  83. struct sof_hdmi_pcm *pcm;
  84. pcm = devm_kzalloc(rtd->card->dev, sizeof(*pcm), GFP_KERNEL);
  85. if (!pcm)
  86. return -ENOMEM;
  87. /* dai_link id is 1:1 mapped to the PCM device */
  88. pcm->device = rtd->dai_link->id;
  89. pcm->codec_dai = dai;
  90. list_add_tail(&pcm->head, &ctx->hdmi_pcm_list);
  91. return 0;
  92. }
  93. static int sof_cs42l42_init(struct snd_soc_pcm_runtime *rtd)
  94. {
  95. struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
  96. struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
  97. struct snd_soc_jack *jack = &ctx->headset_jack;
  98. int ret;
  99. /*
  100. * Headset buttons map to the google Reference headset.
  101. * These can be configured by userspace.
  102. */
  103. ret = snd_soc_card_jack_new_pins(rtd->card, "Headset Jack",
  104. SND_JACK_HEADSET | SND_JACK_BTN_0 |
  105. SND_JACK_BTN_1 | SND_JACK_BTN_2 |
  106. SND_JACK_BTN_3,
  107. jack,
  108. jack_pins,
  109. ARRAY_SIZE(jack_pins));
  110. if (ret) {
  111. dev_err(rtd->dev, "Headset Jack creation failed: %d\n", ret);
  112. return ret;
  113. }
  114. snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
  115. snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOLUMEUP);
  116. snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEDOWN);
  117. snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOICECOMMAND);
  118. ret = snd_soc_component_set_jack(component, jack, NULL);
  119. if (ret) {
  120. dev_err(rtd->dev, "Headset Jack call-back failed: %d\n", ret);
  121. return ret;
  122. }
  123. return ret;
  124. };
  125. static void sof_cs42l42_exit(struct snd_soc_pcm_runtime *rtd)
  126. {
  127. struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
  128. snd_soc_component_set_jack(component, NULL, NULL);
  129. }
  130. static int sof_cs42l42_hw_params(struct snd_pcm_substream *substream,
  131. struct snd_pcm_hw_params *params)
  132. {
  133. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  134. struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
  135. int clk_freq, ret;
  136. clk_freq = sof_dai_get_bclk(rtd); /* BCLK freq */
  137. if (clk_freq <= 0) {
  138. dev_err(rtd->dev, "get bclk freq failed: %d\n", clk_freq);
  139. return -EINVAL;
  140. }
  141. /* Configure sysclk for codec */
  142. ret = snd_soc_dai_set_sysclk(codec_dai, 0,
  143. clk_freq, SND_SOC_CLOCK_IN);
  144. if (ret < 0)
  145. dev_err(rtd->dev, "snd_soc_dai_set_sysclk err = %d\n", ret);
  146. return ret;
  147. }
  148. static const struct snd_soc_ops sof_cs42l42_ops = {
  149. .hw_params = sof_cs42l42_hw_params,
  150. };
  151. static struct snd_soc_dai_link_component platform_component[] = {
  152. {
  153. /* name might be overridden during probe */
  154. .name = "0000:00:1f.3"
  155. }
  156. };
  157. static int sof_card_late_probe(struct snd_soc_card *card)
  158. {
  159. struct sof_card_private *ctx = snd_soc_card_get_drvdata(card);
  160. struct snd_soc_component *component = NULL;
  161. char jack_name[NAME_SIZE];
  162. struct sof_hdmi_pcm *pcm;
  163. int err;
  164. if (list_empty(&ctx->hdmi_pcm_list))
  165. return -EINVAL;
  166. if (ctx->common_hdmi_codec_drv) {
  167. pcm = list_first_entry(&ctx->hdmi_pcm_list, struct sof_hdmi_pcm,
  168. head);
  169. component = pcm->codec_dai->component;
  170. return hda_dsp_hdmi_build_controls(card, component);
  171. }
  172. list_for_each_entry(pcm, &ctx->hdmi_pcm_list, head) {
  173. component = pcm->codec_dai->component;
  174. snprintf(jack_name, sizeof(jack_name),
  175. "HDMI/DP, pcm=%d Jack", pcm->device);
  176. err = snd_soc_card_jack_new(card, jack_name,
  177. SND_JACK_AVOUT, &pcm->hdmi_jack);
  178. if (err)
  179. return err;
  180. err = hdac_hdmi_jack_init(pcm->codec_dai, pcm->device,
  181. &pcm->hdmi_jack);
  182. if (err < 0)
  183. return err;
  184. }
  185. return hdac_hdmi_jack_port_init(component, &card->dapm);
  186. }
  187. static const struct snd_kcontrol_new sof_controls[] = {
  188. SOC_DAPM_PIN_SWITCH("Headphone Jack"),
  189. SOC_DAPM_PIN_SWITCH("Headset Mic"),
  190. };
  191. static const struct snd_soc_dapm_widget sof_widgets[] = {
  192. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  193. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  194. };
  195. static const struct snd_soc_dapm_widget dmic_widgets[] = {
  196. SND_SOC_DAPM_MIC("SoC DMIC", NULL),
  197. };
  198. static const struct snd_soc_dapm_route sof_map[] = {
  199. /* HP jack connectors - unknown if we have jack detection */
  200. {"Headphone Jack", NULL, "HP"},
  201. /* other jacks */
  202. {"HS", NULL, "Headset Mic"},
  203. };
  204. static const struct snd_soc_dapm_route dmic_map[] = {
  205. /* digital mics */
  206. {"DMic", NULL, "SoC DMIC"},
  207. };
  208. static int dmic_init(struct snd_soc_pcm_runtime *rtd)
  209. {
  210. struct snd_soc_card *card = rtd->card;
  211. int ret;
  212. ret = snd_soc_dapm_new_controls(&card->dapm, dmic_widgets,
  213. ARRAY_SIZE(dmic_widgets));
  214. if (ret) {
  215. dev_err(card->dev, "DMic widget addition failed: %d\n", ret);
  216. /* Don't need to add routes if widget addition failed */
  217. return ret;
  218. }
  219. ret = snd_soc_dapm_add_routes(&card->dapm, dmic_map,
  220. ARRAY_SIZE(dmic_map));
  221. if (ret)
  222. dev_err(card->dev, "DMic map addition failed: %d\n", ret);
  223. return ret;
  224. }
  225. /* sof audio machine driver for cs42l42 codec */
  226. static struct snd_soc_card sof_audio_card_cs42l42 = {
  227. .name = "cs42l42", /* the sof- prefix is added by the core */
  228. .owner = THIS_MODULE,
  229. .controls = sof_controls,
  230. .num_controls = ARRAY_SIZE(sof_controls),
  231. .dapm_widgets = sof_widgets,
  232. .num_dapm_widgets = ARRAY_SIZE(sof_widgets),
  233. .dapm_routes = sof_map,
  234. .num_dapm_routes = ARRAY_SIZE(sof_map),
  235. .fully_routed = true,
  236. .late_probe = sof_card_late_probe,
  237. };
  238. static struct snd_soc_dai_link_component cs42l42_component[] = {
  239. {
  240. .name = "i2c-10134242:00",
  241. .dai_name = "cs42l42",
  242. }
  243. };
  244. static struct snd_soc_dai_link_component dmic_component[] = {
  245. {
  246. .name = "dmic-codec",
  247. .dai_name = "dmic-hifi",
  248. }
  249. };
  250. static struct snd_soc_dai_link_component dummy_component[] = {
  251. {
  252. .name = "snd-soc-dummy",
  253. .dai_name = "snd-soc-dummy-dai",
  254. }
  255. };
  256. static int create_spk_amp_dai_links(struct device *dev,
  257. struct snd_soc_dai_link *links,
  258. struct snd_soc_dai_link_component *cpus,
  259. int *id, int ssp_amp)
  260. {
  261. int ret = 0;
  262. /* speaker amp */
  263. if (!(sof_cs42l42_quirk & SOF_SPEAKER_AMP_PRESENT))
  264. return 0;
  265. links[*id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-Codec",
  266. ssp_amp);
  267. if (!links[*id].name) {
  268. ret = -ENOMEM;
  269. goto devm_err;
  270. }
  271. links[*id].id = *id;
  272. if (sof_cs42l42_quirk & SOF_MAX98357A_SPEAKER_AMP_PRESENT) {
  273. max_98357a_dai_link(&links[*id]);
  274. } else if (sof_cs42l42_quirk & SOF_MAX98360A_SPEAKER_AMP_PRESENT) {
  275. max_98360a_dai_link(&links[*id]);
  276. } else {
  277. dev_err(dev, "no amp defined\n");
  278. ret = -EINVAL;
  279. goto devm_err;
  280. }
  281. links[*id].platforms = platform_component;
  282. links[*id].num_platforms = ARRAY_SIZE(platform_component);
  283. links[*id].dpcm_playback = 1;
  284. /* firmware-generated echo reference */
  285. links[*id].dpcm_capture = 1;
  286. links[*id].no_pcm = 1;
  287. links[*id].cpus = &cpus[*id];
  288. links[*id].num_cpus = 1;
  289. links[*id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
  290. "SSP%d Pin", ssp_amp);
  291. if (!links[*id].cpus->dai_name) {
  292. ret = -ENOMEM;
  293. goto devm_err;
  294. }
  295. (*id)++;
  296. devm_err:
  297. return ret;
  298. }
  299. static int create_hp_codec_dai_links(struct device *dev,
  300. struct snd_soc_dai_link *links,
  301. struct snd_soc_dai_link_component *cpus,
  302. int *id, int ssp_codec)
  303. {
  304. /* codec SSP */
  305. links[*id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-Codec",
  306. ssp_codec);
  307. if (!links[*id].name)
  308. goto devm_err;
  309. links[*id].id = *id;
  310. links[*id].codecs = cs42l42_component;
  311. links[*id].num_codecs = ARRAY_SIZE(cs42l42_component);
  312. links[*id].platforms = platform_component;
  313. links[*id].num_platforms = ARRAY_SIZE(platform_component);
  314. links[*id].init = sof_cs42l42_init;
  315. links[*id].exit = sof_cs42l42_exit;
  316. links[*id].ops = &sof_cs42l42_ops;
  317. links[*id].dpcm_playback = 1;
  318. links[*id].dpcm_capture = 1;
  319. links[*id].no_pcm = 1;
  320. links[*id].cpus = &cpus[*id];
  321. links[*id].num_cpus = 1;
  322. links[*id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
  323. "SSP%d Pin",
  324. ssp_codec);
  325. if (!links[*id].cpus->dai_name)
  326. goto devm_err;
  327. (*id)++;
  328. return 0;
  329. devm_err:
  330. return -ENOMEM;
  331. }
  332. static int create_dmic_dai_links(struct device *dev,
  333. struct snd_soc_dai_link *links,
  334. struct snd_soc_dai_link_component *cpus,
  335. int *id, int dmic_be_num)
  336. {
  337. int i;
  338. /* dmic */
  339. if (dmic_be_num <= 0)
  340. return 0;
  341. /* at least we have dmic01 */
  342. links[*id].name = "dmic01";
  343. links[*id].cpus = &cpus[*id];
  344. links[*id].cpus->dai_name = "DMIC01 Pin";
  345. links[*id].init = dmic_init;
  346. if (dmic_be_num > 1) {
  347. /* set up 2 BE links at most */
  348. links[*id + 1].name = "dmic16k";
  349. links[*id + 1].cpus = &cpus[*id + 1];
  350. links[*id + 1].cpus->dai_name = "DMIC16k Pin";
  351. dmic_be_num = 2;
  352. }
  353. for (i = 0; i < dmic_be_num; i++) {
  354. links[*id].id = *id;
  355. links[*id].num_cpus = 1;
  356. links[*id].codecs = dmic_component;
  357. links[*id].num_codecs = ARRAY_SIZE(dmic_component);
  358. links[*id].platforms = platform_component;
  359. links[*id].num_platforms = ARRAY_SIZE(platform_component);
  360. links[*id].ignore_suspend = 1;
  361. links[*id].dpcm_capture = 1;
  362. links[*id].no_pcm = 1;
  363. (*id)++;
  364. }
  365. return 0;
  366. }
  367. static int create_hdmi_dai_links(struct device *dev,
  368. struct snd_soc_dai_link *links,
  369. struct snd_soc_dai_link_component *cpus,
  370. int *id, int hdmi_num)
  371. {
  372. struct snd_soc_dai_link_component *idisp_components;
  373. int i;
  374. /* HDMI */
  375. if (hdmi_num <= 0)
  376. return 0;
  377. idisp_components = devm_kcalloc(dev,
  378. hdmi_num,
  379. sizeof(struct snd_soc_dai_link_component), GFP_KERNEL);
  380. if (!idisp_components)
  381. goto devm_err;
  382. for (i = 1; i <= hdmi_num; i++) {
  383. links[*id].name = devm_kasprintf(dev, GFP_KERNEL,
  384. "iDisp%d", i);
  385. if (!links[*id].name)
  386. goto devm_err;
  387. links[*id].id = *id;
  388. links[*id].cpus = &cpus[*id];
  389. links[*id].num_cpus = 1;
  390. links[*id].cpus->dai_name = devm_kasprintf(dev,
  391. GFP_KERNEL,
  392. "iDisp%d Pin",
  393. i);
  394. if (!links[*id].cpus->dai_name)
  395. goto devm_err;
  396. idisp_components[i - 1].name = "ehdaudio0D2";
  397. idisp_components[i - 1].dai_name = devm_kasprintf(dev,
  398. GFP_KERNEL,
  399. "intel-hdmi-hifi%d",
  400. i);
  401. if (!idisp_components[i - 1].dai_name)
  402. goto devm_err;
  403. links[*id].codecs = &idisp_components[i - 1];
  404. links[*id].num_codecs = 1;
  405. links[*id].platforms = platform_component;
  406. links[*id].num_platforms = ARRAY_SIZE(platform_component);
  407. links[*id].init = sof_hdmi_init;
  408. links[*id].dpcm_playback = 1;
  409. links[*id].no_pcm = 1;
  410. (*id)++;
  411. }
  412. return 0;
  413. devm_err:
  414. return -ENOMEM;
  415. }
  416. static int create_bt_offload_dai_links(struct device *dev,
  417. struct snd_soc_dai_link *links,
  418. struct snd_soc_dai_link_component *cpus,
  419. int *id, int ssp_bt)
  420. {
  421. /* bt offload */
  422. if (!(sof_cs42l42_quirk & SOF_BT_OFFLOAD_PRESENT))
  423. return 0;
  424. links[*id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-BT",
  425. ssp_bt);
  426. if (!links[*id].name)
  427. goto devm_err;
  428. links[*id].id = *id;
  429. links[*id].codecs = dummy_component;
  430. links[*id].num_codecs = ARRAY_SIZE(dummy_component);
  431. links[*id].platforms = platform_component;
  432. links[*id].num_platforms = ARRAY_SIZE(platform_component);
  433. links[*id].dpcm_playback = 1;
  434. links[*id].dpcm_capture = 1;
  435. links[*id].no_pcm = 1;
  436. links[*id].cpus = &cpus[*id];
  437. links[*id].num_cpus = 1;
  438. links[*id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
  439. "SSP%d Pin",
  440. ssp_bt);
  441. if (!links[*id].cpus->dai_name)
  442. goto devm_err;
  443. (*id)++;
  444. return 0;
  445. devm_err:
  446. return -ENOMEM;
  447. }
  448. static struct snd_soc_dai_link *sof_card_dai_links_create(struct device *dev,
  449. int ssp_codec,
  450. int ssp_amp,
  451. int ssp_bt,
  452. int dmic_be_num,
  453. int hdmi_num)
  454. {
  455. struct snd_soc_dai_link_component *cpus;
  456. struct snd_soc_dai_link *links;
  457. int ret, id = 0, link_seq;
  458. links = devm_kcalloc(dev, sof_audio_card_cs42l42.num_links,
  459. sizeof(struct snd_soc_dai_link), GFP_KERNEL);
  460. cpus = devm_kcalloc(dev, sof_audio_card_cs42l42.num_links,
  461. sizeof(struct snd_soc_dai_link_component), GFP_KERNEL);
  462. if (!links || !cpus)
  463. goto devm_err;
  464. link_seq = (sof_cs42l42_quirk & SOF_CS42L42_DAILINK_MASK) >> SOF_CS42L42_DAILINK_SHIFT;
  465. while (link_seq) {
  466. int link_type = link_seq & 0x07;
  467. switch (link_type) {
  468. case LINK_HP:
  469. ret = create_hp_codec_dai_links(dev, links, cpus, &id, ssp_codec);
  470. if (ret < 0) {
  471. dev_err(dev, "fail to create hp codec dai links, ret %d\n",
  472. ret);
  473. goto devm_err;
  474. }
  475. break;
  476. case LINK_SPK:
  477. ret = create_spk_amp_dai_links(dev, links, cpus, &id, ssp_amp);
  478. if (ret < 0) {
  479. dev_err(dev, "fail to create spk amp dai links, ret %d\n",
  480. ret);
  481. goto devm_err;
  482. }
  483. break;
  484. case LINK_DMIC:
  485. ret = create_dmic_dai_links(dev, links, cpus, &id, dmic_be_num);
  486. if (ret < 0) {
  487. dev_err(dev, "fail to create dmic dai links, ret %d\n",
  488. ret);
  489. goto devm_err;
  490. }
  491. break;
  492. case LINK_HDMI:
  493. ret = create_hdmi_dai_links(dev, links, cpus, &id, hdmi_num);
  494. if (ret < 0) {
  495. dev_err(dev, "fail to create hdmi dai links, ret %d\n",
  496. ret);
  497. goto devm_err;
  498. }
  499. break;
  500. case LINK_BT:
  501. ret = create_bt_offload_dai_links(dev, links, cpus, &id, ssp_bt);
  502. if (ret < 0) {
  503. dev_err(dev, "fail to create bt offload dai links, ret %d\n",
  504. ret);
  505. goto devm_err;
  506. }
  507. break;
  508. case LINK_NONE:
  509. /* caught here if it's not used as terminator in macro */
  510. default:
  511. dev_err(dev, "invalid link type %d\n", link_type);
  512. goto devm_err;
  513. }
  514. link_seq >>= 3;
  515. }
  516. return links;
  517. devm_err:
  518. return NULL;
  519. }
  520. static int sof_audio_probe(struct platform_device *pdev)
  521. {
  522. struct snd_soc_dai_link *dai_links;
  523. struct snd_soc_acpi_mach *mach;
  524. struct sof_card_private *ctx;
  525. int dmic_be_num, hdmi_num;
  526. int ret, ssp_bt, ssp_amp, ssp_codec;
  527. ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
  528. if (!ctx)
  529. return -ENOMEM;
  530. if (pdev->id_entry && pdev->id_entry->driver_data)
  531. sof_cs42l42_quirk = (unsigned long)pdev->id_entry->driver_data;
  532. mach = pdev->dev.platform_data;
  533. if (soc_intel_is_glk()) {
  534. dmic_be_num = 1;
  535. hdmi_num = 3;
  536. } else {
  537. dmic_be_num = 2;
  538. hdmi_num = (sof_cs42l42_quirk & SOF_CS42L42_NUM_HDMIDEV_MASK) >>
  539. SOF_CS42L42_NUM_HDMIDEV_SHIFT;
  540. /* default number of HDMI DAI's */
  541. if (!hdmi_num)
  542. hdmi_num = 3;
  543. }
  544. dev_dbg(&pdev->dev, "sof_cs42l42_quirk = %lx\n", sof_cs42l42_quirk);
  545. ssp_bt = (sof_cs42l42_quirk & SOF_CS42L42_SSP_BT_MASK) >>
  546. SOF_CS42L42_SSP_BT_SHIFT;
  547. ssp_amp = (sof_cs42l42_quirk & SOF_CS42L42_SSP_AMP_MASK) >>
  548. SOF_CS42L42_SSP_AMP_SHIFT;
  549. ssp_codec = sof_cs42l42_quirk & SOF_CS42L42_SSP_CODEC_MASK;
  550. /* compute number of dai links */
  551. sof_audio_card_cs42l42.num_links = 1 + dmic_be_num + hdmi_num;
  552. if (sof_cs42l42_quirk & SOF_SPEAKER_AMP_PRESENT)
  553. sof_audio_card_cs42l42.num_links++;
  554. if (sof_cs42l42_quirk & SOF_BT_OFFLOAD_PRESENT)
  555. sof_audio_card_cs42l42.num_links++;
  556. dai_links = sof_card_dai_links_create(&pdev->dev, ssp_codec, ssp_amp,
  557. ssp_bt, dmic_be_num, hdmi_num);
  558. if (!dai_links)
  559. return -ENOMEM;
  560. sof_audio_card_cs42l42.dai_link = dai_links;
  561. INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
  562. sof_audio_card_cs42l42.dev = &pdev->dev;
  563. /* set platform name for each dailink */
  564. ret = snd_soc_fixup_dai_links_platform_name(&sof_audio_card_cs42l42,
  565. mach->mach_params.platform);
  566. if (ret)
  567. return ret;
  568. ctx->common_hdmi_codec_drv = mach->mach_params.common_hdmi_codec_drv;
  569. snd_soc_card_set_drvdata(&sof_audio_card_cs42l42, ctx);
  570. return devm_snd_soc_register_card(&pdev->dev,
  571. &sof_audio_card_cs42l42);
  572. }
  573. static const struct platform_device_id board_ids[] = {
  574. {
  575. .name = "glk_cs4242_mx98357a",
  576. .driver_data = (kernel_ulong_t)(SOF_CS42L42_SSP_CODEC(2) |
  577. SOF_SPEAKER_AMP_PRESENT |
  578. SOF_MAX98357A_SPEAKER_AMP_PRESENT |
  579. SOF_CS42L42_SSP_AMP(1)) |
  580. SOF_CS42L42_DAILINK(LINK_SPK, LINK_HP, LINK_DMIC, LINK_HDMI, LINK_NONE),
  581. },
  582. {
  583. .name = "jsl_cs4242_mx98360a",
  584. .driver_data = (kernel_ulong_t)(SOF_CS42L42_SSP_CODEC(0) |
  585. SOF_SPEAKER_AMP_PRESENT |
  586. SOF_MAX98360A_SPEAKER_AMP_PRESENT |
  587. SOF_CS42L42_SSP_AMP(1)) |
  588. SOF_CS42L42_DAILINK(LINK_HP, LINK_DMIC, LINK_HDMI, LINK_SPK, LINK_NONE),
  589. },
  590. {
  591. .name = "adl_mx98360a_cs4242",
  592. .driver_data = (kernel_ulong_t)(SOF_CS42L42_SSP_CODEC(0) |
  593. SOF_SPEAKER_AMP_PRESENT |
  594. SOF_MAX98360A_SPEAKER_AMP_PRESENT |
  595. SOF_CS42L42_SSP_AMP(1) |
  596. SOF_CS42L42_NUM_HDMIDEV(4) |
  597. SOF_BT_OFFLOAD_PRESENT |
  598. SOF_CS42L42_SSP_BT(2) |
  599. SOF_CS42L42_DAILINK(LINK_HP, LINK_DMIC, LINK_HDMI, LINK_SPK, LINK_BT)),
  600. },
  601. { }
  602. };
  603. MODULE_DEVICE_TABLE(platform, board_ids);
  604. static struct platform_driver sof_audio = {
  605. .probe = sof_audio_probe,
  606. .driver = {
  607. .name = "sof_cs42l42",
  608. .pm = &snd_soc_pm_ops,
  609. },
  610. .id_table = board_ids,
  611. };
  612. module_platform_driver(sof_audio)
  613. /* Module information */
  614. MODULE_DESCRIPTION("SOF Audio Machine driver for CS42L42");
  615. MODULE_AUTHOR("Brent Lu <[email protected]>");
  616. MODULE_LICENSE("GPL");
  617. MODULE_IMPORT_NS(SND_SOC_INTEL_HDA_DSP_COMMON);
  618. MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_MAXIM_COMMON);