Linux Audio

Check our new training course

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