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// Copyright(c) 2021 Nuvoton Corporation.
  4
  5/*
  6 * Intel SOF Machine Driver with Nuvoton headphone codec NAU8825
  7 * and speaker codec RT1019P MAX98360a or MAX98373
  8 */
  9#include <linux/i2c.h>
 10#include <linux/input.h>
 11#include <linux/module.h>
 12#include <linux/platform_device.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 "../../codecs/nau8825.h"
 22#include "../common/soc-intel-quirks.h"
 23#include "sof_board_helpers.h"
 24#include "sof_realtek_common.h"
 25#include "sof_maxim_common.h"
 26#include "sof_nuvoton_common.h"
 27
 28static unsigned long sof_nau8825_quirk = SOF_SSP_PORT_CODEC(0);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 29
 30static struct snd_soc_jack_pin jack_pins[] = {
 31	{
 32		.pin    = "Headphone Jack",
 33		.mask   = SND_JACK_HEADPHONE,
 34	},
 35	{
 36		.pin    = "Headset Mic",
 37		.mask   = SND_JACK_MICROPHONE,
 38	},
 39};
 40
 41static int sof_nau8825_codec_init(struct snd_soc_pcm_runtime *rtd)
 42{
 43	struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
 44	struct snd_soc_component *component = snd_soc_rtd_to_codec(rtd, 0)->component;
 45	struct snd_soc_jack *jack = &ctx->headset_jack;
 
 46	int ret;
 47
 48	/*
 49	 * Headset buttons map to the google Reference headset.
 50	 * These can be configured by userspace.
 51	 */
 52	ret = snd_soc_card_jack_new_pins(rtd->card, "Headset Jack",
 53					 SND_JACK_HEADSET | SND_JACK_BTN_0 |
 54					 SND_JACK_BTN_1 | SND_JACK_BTN_2 |
 55					 SND_JACK_BTN_3,
 56					 jack,
 57					 jack_pins,
 58					 ARRAY_SIZE(jack_pins));
 59	if (ret) {
 60		dev_err(rtd->dev, "Headset Jack creation failed: %d\n", ret);
 61		return ret;
 62	}
 63
 
 
 64	snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
 65	snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
 66	snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
 67	snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
 68
 69	ret = snd_soc_component_set_jack(component, jack, NULL);
 
 70	if (ret) {
 71		dev_err(rtd->dev, "Headset Jack call-back failed: %d\n", ret);
 72		return ret;
 73	}
 74
 75	return ret;
 76};
 77
 78static void sof_nau8825_codec_exit(struct snd_soc_pcm_runtime *rtd)
 79{
 80	struct snd_soc_component *component = snd_soc_rtd_to_codec(rtd, 0)->component;
 81
 82	snd_soc_component_set_jack(component, NULL, NULL);
 83}
 84
 85static int sof_nau8825_hw_params(struct snd_pcm_substream *substream,
 86				 struct snd_pcm_hw_params *params)
 87{
 88	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
 89	struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
 90	int clk_freq, ret;
 91
 92	clk_freq = sof_dai_get_bclk(rtd); /* BCLK freq */
 93
 94	if (clk_freq <= 0) {
 95		dev_err(rtd->dev, "get bclk freq failed: %d\n", clk_freq);
 96		return -EINVAL;
 97	}
 98
 99	/* Configure clock for codec */
100	ret = snd_soc_dai_set_sysclk(codec_dai, NAU8825_CLK_FLL_BLK, 0,
101				     SND_SOC_CLOCK_IN);
102	if (ret < 0) {
103		dev_err(codec_dai->dev, "can't set BCLK clock %d\n", ret);
104		return ret;
105	}
106
107	/* Configure pll for codec */
108	ret = snd_soc_dai_set_pll(codec_dai, 0, 0, clk_freq,
109				  params_rate(params) * 256);
110	if (ret < 0) {
111		dev_err(codec_dai->dev, "can't set BCLK: %d\n", ret);
112		return ret;
113	}
114
115	return ret;
116}
117
118static const struct snd_soc_ops sof_nau8825_ops = {
119	.hw_params = sof_nau8825_hw_params,
120};
121
 
 
 
 
 
 
 
122static int sof_card_late_probe(struct snd_soc_card *card)
123{
124	struct sof_card_private *ctx = snd_soc_card_get_drvdata(card);
125	struct snd_soc_dapm_context *dapm = &card->dapm;
 
126	int err;
127
128	if (ctx->amp_type == CODEC_MAX98373) {
129		/* Disable Left and Right Spk pin after boot */
130		snd_soc_dapm_disable_pin(dapm, "Left Spk");
131		snd_soc_dapm_disable_pin(dapm, "Right Spk");
132		err = snd_soc_dapm_sync(dapm);
133		if (err < 0)
134			return err;
135	}
136
137	return sof_intel_board_card_late_probe(card);
 
 
 
 
 
138}
139
140static const struct snd_kcontrol_new sof_controls[] = {
141	SOC_DAPM_PIN_SWITCH("Headphone Jack"),
142	SOC_DAPM_PIN_SWITCH("Headset Mic"),
 
 
 
 
 
 
143};
144
145static const struct snd_soc_dapm_widget sof_widgets[] = {
146	SND_SOC_DAPM_HP("Headphone Jack", NULL),
147	SND_SOC_DAPM_MIC("Headset Mic", NULL),
 
 
 
 
 
 
 
 
 
 
148};
149
150static const struct snd_soc_dapm_route sof_map[] = {
151	/* HP jack connectors - unknown if we have jack detection */
152	{ "Headphone Jack", NULL, "HPOL" },
153	{ "Headphone Jack", NULL, "HPOR" },
154
155	/* other jacks */
156	{ "MIC", NULL, "Headset Mic" },
157};
158
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
159/* sof audio machine driver for nau8825 codec */
160static struct snd_soc_card sof_audio_card_nau8825 = {
161	.name = "nau8825", /* the sof- prefix is added by the core */
162	.owner = THIS_MODULE,
163	.controls = sof_controls,
164	.num_controls = ARRAY_SIZE(sof_controls),
165	.dapm_widgets = sof_widgets,
166	.num_dapm_widgets = ARRAY_SIZE(sof_widgets),
167	.dapm_routes = sof_map,
168	.num_dapm_routes = ARRAY_SIZE(sof_map),
169	.fully_routed = true,
170	.late_probe = sof_card_late_probe,
171};
172
173static struct snd_soc_dai_link_component nau8825_component[] = {
174	{
175		.name = "i2c-10508825:00",
176		.dai_name = "nau8825-hifi",
177	}
178};
179
180static int
181sof_card_dai_links_create(struct device *dev, struct snd_soc_card *card,
182			  struct sof_card_private *ctx)
183{
184	int ret;
185
186	ret = sof_intel_board_set_dai_link(dev, card, ctx);
187	if (ret)
188		return ret;
189
190	if (!ctx->codec_link) {
191		dev_err(dev, "codec link not available");
192		return -EINVAL;
193	}
 
194
195	/* codec-specific fields for headphone codec */
196	ctx->codec_link->codecs = nau8825_component;
197	ctx->codec_link->num_codecs = ARRAY_SIZE(nau8825_component);
198	ctx->codec_link->init = sof_nau8825_codec_init;
199	ctx->codec_link->exit = sof_nau8825_codec_exit;
200	ctx->codec_link->ops = &sof_nau8825_ops;
201
202	if (ctx->amp_type == CODEC_NONE)
203		return 0;
204
205	if (!ctx->amp_link) {
206		dev_err(dev, "amp link not available");
207		return -EINVAL;
 
208	}
 
209
210	/* codec-specific fields for speaker amplifier */
211	switch (ctx->amp_type) {
212	case CODEC_MAX98360A:
213		max_98360a_dai_link(ctx->amp_link);
214		break;
215	case CODEC_MAX98373:
216		max_98373_dai_link(dev, ctx->amp_link);
217		break;
218	case CODEC_NAU8318:
219		nau8318_set_dai_link(ctx->amp_link);
220		break;
221	case CODEC_RT1015P:
222		sof_rt1015p_dai_link(ctx->amp_link);
223		break;
224	case CODEC_RT1019P:
225		sof_rt1019p_dai_link(ctx->amp_link);
226		break;
227	default:
228		dev_err(dev, "invalid amp type %d\n", ctx->amp_type);
229		return -EINVAL;
230	}
 
231
232	return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
233}
234
235static int sof_audio_probe(struct platform_device *pdev)
236{
237	struct snd_soc_acpi_mach *mach = pdev->dev.platform_data;
 
238	struct sof_card_private *ctx;
239	int ret;
 
 
 
 
 
240
241	if (pdev->id_entry && pdev->id_entry->driver_data)
242		sof_nau8825_quirk = (unsigned long)pdev->id_entry->driver_data;
243
 
 
 
 
 
 
 
 
244	dev_dbg(&pdev->dev, "sof_nau8825_quirk = %lx\n", sof_nau8825_quirk);
245
246	/* initialize ctx with board quirk */
247	ctx = sof_intel_board_get_ctx(&pdev->dev, sof_nau8825_quirk);
248	if (!ctx)
249		return -ENOMEM;
 
 
 
 
 
 
 
 
250
251	if (mach->mach_params.codec_mask & IDISP_CODEC_MASK)
252		ctx->hdmi.idisp_codec = true;
253
254	/* update dai_link */
255	ret = sof_card_dai_links_create(&pdev->dev, &sof_audio_card_nau8825, ctx);
256	if (ret)
257		return ret;
258
259	/* update codec_conf */
260	switch (ctx->amp_type) {
261	case CODEC_MAX98373:
262		max_98373_set_codec_conf(&sof_audio_card_nau8825);
263		break;
264	case CODEC_RT1015P:
265		sof_rt1015p_codec_conf(&sof_audio_card_nau8825);
266		break;
267	case CODEC_MAX98360A:
268	case CODEC_NAU8318:
269	case CODEC_RT1019P:
270	case CODEC_NONE:
271		/* no codec conf required */
272		break;
273	default:
274		dev_err(&pdev->dev, "invalid amp type %d\n", ctx->amp_type);
275		return -EINVAL;
276	}
 
277
278	sof_audio_card_nau8825.dev = &pdev->dev;
279
280	/* set platform name for each dailink */
281	ret = snd_soc_fixup_dai_links_platform_name(&sof_audio_card_nau8825,
282						    mach->mach_params.platform);
283	if (ret)
284		return ret;
285
286	snd_soc_card_set_drvdata(&sof_audio_card_nau8825, ctx);
287
288	return devm_snd_soc_register_card(&pdev->dev,
289					  &sof_audio_card_nau8825);
290}
291
292static const struct platform_device_id board_ids[] = {
293	{
 
 
 
 
 
 
 
 
294		.name = "adl_rt1019p_8825",
295		.driver_data = (kernel_ulong_t)(SOF_SSP_PORT_CODEC(0) |
296					SOF_SSP_PORT_AMP(2) |
297					SOF_NUM_IDISP_HDMI(4)),
 
 
298	},
299	{
300		.name = "adl_nau8825_def",
301		.driver_data = (kernel_ulong_t)(SOF_SSP_PORT_CODEC(0) |
302					SOF_SSP_PORT_AMP(1) |
303					SOF_NUM_IDISP_HDMI(4) |
304					SOF_SSP_PORT_BT_OFFLOAD(2) |
305					SOF_BT_OFFLOAD_PRESENT),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
306	},
307	{
308		.name = "rpl_nau8825_def",
309		.driver_data = (kernel_ulong_t)(SOF_SSP_PORT_CODEC(0) |
310					SOF_SSP_PORT_AMP(1) |
311					SOF_NUM_IDISP_HDMI(4) |
312					SOF_SSP_PORT_BT_OFFLOAD(2) |
313					SOF_BT_OFFLOAD_PRESENT),
 
 
314	},
315	{
316		.name = "mtl_nau8825_def",
317		.driver_data = (kernel_ulong_t)(SOF_SSP_PORT_CODEC(2) |
318					SOF_SSP_PORT_AMP(0) |
319					SOF_SSP_PORT_BT_OFFLOAD(1) |
320					SOF_BT_OFFLOAD_PRESENT),
 
 
 
321	},
322	{ }
323};
324MODULE_DEVICE_TABLE(platform, board_ids);
325
326static struct platform_driver sof_audio = {
327	.probe = sof_audio_probe,
328	.driver = {
329		.name = "sof_nau8825",
330		.pm = &snd_soc_pm_ops,
331	},
332	.id_table = board_ids,
333};
334module_platform_driver(sof_audio)
335
336/* Module information */
337MODULE_DESCRIPTION("SOF Audio Machine driver for NAU8825");
338MODULE_AUTHOR("David Lin <ctlin0@nuvoton.com>");
339MODULE_AUTHOR("Mac Chiang <mac.chiang@intel.com>");
340MODULE_AUTHOR("Brent Lu <brent.lu@intel.com>");
341MODULE_LICENSE("GPL");
342MODULE_IMPORT_NS("SND_SOC_INTEL_SOF_BOARD_HELPERS");
343MODULE_IMPORT_NS("SND_SOC_INTEL_SOF_MAXIM_COMMON");
344MODULE_IMPORT_NS("SND_SOC_INTEL_SOF_NUVOTON_COMMON");
345MODULE_IMPORT_NS("SND_SOC_INTEL_SOF_REALTEK_COMMON");
v6.2
  1// SPDX-License-Identifier: GPL-2.0-only
  2// Copyright(c) 2021 Intel Corporation.
  3// Copyright(c) 2021 Nuvoton Corporation.
  4
  5/*
  6 * Intel SOF Machine Driver with Nuvoton headphone codec NAU8825
  7 * and speaker codec RT1019P MAX98360a or MAX98373
  8 */
  9#include <linux/i2c.h>
 10#include <linux/input.h>
 11#include <linux/module.h>
 12#include <linux/platform_device.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 "../../codecs/nau8825.h"
 22#include "../common/soc-intel-quirks.h"
 23#include "hda_dsp_common.h"
 24#include "sof_realtek_common.h"
 25#include "sof_maxim_common.h"
 
 26
 27#define NAME_SIZE 32
 28
 29#define SOF_NAU8825_SSP_CODEC(quirk)		((quirk) & GENMASK(2, 0))
 30#define SOF_NAU8825_SSP_CODEC_MASK		(GENMASK(2, 0))
 31#define SOF_SPEAKER_AMP_PRESENT		BIT(3)
 32#define SOF_NAU8825_SSP_AMP_SHIFT		4
 33#define SOF_NAU8825_SSP_AMP_MASK		(GENMASK(6, 4))
 34#define SOF_NAU8825_SSP_AMP(quirk)	\
 35	(((quirk) << SOF_NAU8825_SSP_AMP_SHIFT) & SOF_NAU8825_SSP_AMP_MASK)
 36#define SOF_NAU8825_NUM_HDMIDEV_SHIFT		7
 37#define SOF_NAU8825_NUM_HDMIDEV_MASK		(GENMASK(9, 7))
 38#define SOF_NAU8825_NUM_HDMIDEV(quirk)	\
 39	(((quirk) << SOF_NAU8825_NUM_HDMIDEV_SHIFT) & SOF_NAU8825_NUM_HDMIDEV_MASK)
 40
 41/* BT audio offload: reserve 3 bits for future */
 42#define SOF_BT_OFFLOAD_SSP_SHIFT		10
 43#define SOF_BT_OFFLOAD_SSP_MASK		(GENMASK(12, 10))
 44#define SOF_BT_OFFLOAD_SSP(quirk)	\
 45	(((quirk) << SOF_BT_OFFLOAD_SSP_SHIFT) & SOF_BT_OFFLOAD_SSP_MASK)
 46#define SOF_SSP_BT_OFFLOAD_PRESENT		BIT(13)
 47#define SOF_RT1019P_SPEAKER_AMP_PRESENT	BIT(14)
 48#define SOF_MAX98373_SPEAKER_AMP_PRESENT	BIT(15)
 49#define SOF_MAX98360A_SPEAKER_AMP_PRESENT	BIT(16)
 50#define SOF_RT1015P_SPEAKER_AMP_PRESENT	BIT(17)
 51#define SOF_NAU8318_SPEAKER_AMP_PRESENT	BIT(18)
 52
 53static unsigned long sof_nau8825_quirk = SOF_NAU8825_SSP_CODEC(0);
 54
 55struct sof_hdmi_pcm {
 56	struct list_head head;
 57	struct snd_soc_dai *codec_dai;
 58	int device;
 59};
 60
 61struct sof_card_private {
 62	struct clk *mclk;
 63	struct snd_soc_jack sof_headset;
 64	struct list_head hdmi_pcm_list;
 65};
 66
 67static int sof_hdmi_init(struct snd_soc_pcm_runtime *rtd)
 68{
 69	struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
 70	struct snd_soc_dai *dai = asoc_rtd_to_codec(rtd, 0);
 71	struct sof_hdmi_pcm *pcm;
 72
 73	pcm = devm_kzalloc(rtd->card->dev, sizeof(*pcm), GFP_KERNEL);
 74	if (!pcm)
 75		return -ENOMEM;
 76
 77	/* dai_link id is 1:1 mapped to the PCM device */
 78	pcm->device = rtd->dai_link->id;
 79	pcm->codec_dai = dai;
 80
 81	list_add_tail(&pcm->head, &ctx->hdmi_pcm_list);
 82
 83	return 0;
 84}
 85
 86static struct snd_soc_jack_pin jack_pins[] = {
 87	{
 88		.pin    = "Headphone Jack",
 89		.mask   = SND_JACK_HEADPHONE,
 90	},
 91	{
 92		.pin    = "Headset Mic",
 93		.mask   = SND_JACK_MICROPHONE,
 94	},
 95};
 96
 97static int sof_nau8825_codec_init(struct snd_soc_pcm_runtime *rtd)
 98{
 99	struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
100	struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
101
102	struct snd_soc_jack *jack;
103	int ret;
104
105	/*
106	 * Headset buttons map to the google Reference headset.
107	 * These can be configured by userspace.
108	 */
109	ret = snd_soc_card_jack_new_pins(rtd->card, "Headset Jack",
110					 SND_JACK_HEADSET | SND_JACK_BTN_0 |
111					 SND_JACK_BTN_1 | SND_JACK_BTN_2 |
112					 SND_JACK_BTN_3,
113					 &ctx->sof_headset,
114					 jack_pins,
115					 ARRAY_SIZE(jack_pins));
116	if (ret) {
117		dev_err(rtd->dev, "Headset Jack creation failed: %d\n", ret);
118		return ret;
119	}
120
121	jack = &ctx->sof_headset;
122
123	snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
124	snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
125	snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
126	snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
 
127	ret = snd_soc_component_set_jack(component, jack, NULL);
128
129	if (ret) {
130		dev_err(rtd->dev, "Headset Jack call-back failed: %d\n", ret);
131		return ret;
132	}
133
134	return ret;
135};
136
137static void sof_nau8825_codec_exit(struct snd_soc_pcm_runtime *rtd)
138{
139	struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
140
141	snd_soc_component_set_jack(component, NULL, NULL);
142}
143
144static int sof_nau8825_hw_params(struct snd_pcm_substream *substream,
145				 struct snd_pcm_hw_params *params)
146{
147	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
148	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
149	int clk_freq, ret;
150
151	clk_freq = sof_dai_get_bclk(rtd); /* BCLK freq */
152
153	if (clk_freq <= 0) {
154		dev_err(rtd->dev, "get bclk freq failed: %d\n", clk_freq);
155		return -EINVAL;
156	}
157
158	/* Configure clock for codec */
159	ret = snd_soc_dai_set_sysclk(codec_dai, NAU8825_CLK_FLL_BLK, 0,
160				     SND_SOC_CLOCK_IN);
161	if (ret < 0) {
162		dev_err(codec_dai->dev, "can't set BCLK clock %d\n", ret);
163		return ret;
164	}
165
166	/* Configure pll for codec */
167	ret = snd_soc_dai_set_pll(codec_dai, 0, 0, clk_freq,
168				  params_rate(params) * 256);
169	if (ret < 0) {
170		dev_err(codec_dai->dev, "can't set BCLK: %d\n", ret);
171		return ret;
172	}
173
174	return ret;
175}
176
177static struct snd_soc_ops sof_nau8825_ops = {
178	.hw_params = sof_nau8825_hw_params,
179};
180
181static struct snd_soc_dai_link_component platform_component[] = {
182	{
183		/* name might be overridden during probe */
184		.name = "0000:00:1f.3"
185	}
186};
187
188static int sof_card_late_probe(struct snd_soc_card *card)
189{
190	struct sof_card_private *ctx = snd_soc_card_get_drvdata(card);
191	struct snd_soc_dapm_context *dapm = &card->dapm;
192	struct sof_hdmi_pcm *pcm;
193	int err;
194
195	if (sof_nau8825_quirk & SOF_MAX98373_SPEAKER_AMP_PRESENT) {
196		/* Disable Left and Right Spk pin after boot */
197		snd_soc_dapm_disable_pin(dapm, "Left Spk");
198		snd_soc_dapm_disable_pin(dapm, "Right Spk");
199		err = snd_soc_dapm_sync(dapm);
200		if (err < 0)
201			return err;
202	}
203
204	if (list_empty(&ctx->hdmi_pcm_list))
205		return -EINVAL;
206
207	pcm = list_first_entry(&ctx->hdmi_pcm_list, struct sof_hdmi_pcm, head);
208
209	return hda_dsp_hdmi_build_controls(card, pcm->codec_dai->component);
210}
211
212static const struct snd_kcontrol_new sof_controls[] = {
213	SOC_DAPM_PIN_SWITCH("Headphone Jack"),
214	SOC_DAPM_PIN_SWITCH("Headset Mic"),
215	SOC_DAPM_PIN_SWITCH("Left Spk"),
216	SOC_DAPM_PIN_SWITCH("Right Spk"),
217};
218
219static const struct snd_kcontrol_new speaker_controls[] = {
220	SOC_DAPM_PIN_SWITCH("Spk"),
221};
222
223static const struct snd_soc_dapm_widget sof_widgets[] = {
224	SND_SOC_DAPM_HP("Headphone Jack", NULL),
225	SND_SOC_DAPM_MIC("Headset Mic", NULL),
226	SND_SOC_DAPM_SPK("Left Spk", NULL),
227	SND_SOC_DAPM_SPK("Right Spk", NULL),
228};
229
230static const struct snd_soc_dapm_widget speaker_widgets[] = {
231	SND_SOC_DAPM_SPK("Spk", NULL),
232};
233
234static const struct snd_soc_dapm_widget dmic_widgets[] = {
235	SND_SOC_DAPM_MIC("SoC DMIC", NULL),
236};
237
238static const struct snd_soc_dapm_route sof_map[] = {
239	/* HP jack connectors - unknown if we have jack detection */
240	{ "Headphone Jack", NULL, "HPOL" },
241	{ "Headphone Jack", NULL, "HPOR" },
242
243	/* other jacks */
244	{ "MIC", NULL, "Headset Mic" },
245};
246
247static const struct snd_soc_dapm_route speaker_map[] = {
248	/* speaker */
249	{ "Spk", NULL, "Speaker" },
250};
251
252static const struct snd_soc_dapm_route dmic_map[] = {
253	/* digital mics */
254	{"DMic", NULL, "SoC DMIC"},
255};
256
257static int speaker_codec_init(struct snd_soc_pcm_runtime *rtd)
258{
259	struct snd_soc_card *card = rtd->card;
260	int ret;
261
262	ret = snd_soc_dapm_new_controls(&card->dapm, speaker_widgets,
263					ARRAY_SIZE(speaker_widgets));
264	if (ret) {
265		dev_err(rtd->dev, "unable to add dapm controls, ret %d\n", ret);
266		/* Don't need to add routes if widget addition failed */
267		return ret;
268	}
269
270	ret = snd_soc_add_card_controls(card, speaker_controls,
271					ARRAY_SIZE(speaker_controls));
272	if (ret) {
273		dev_err(rtd->dev, "unable to add card controls, ret %d\n", ret);
274		return ret;
275	}
276
277	ret = snd_soc_dapm_add_routes(&card->dapm, speaker_map,
278				      ARRAY_SIZE(speaker_map));
279
280	if (ret)
281		dev_err(rtd->dev, "Speaker map addition failed: %d\n", ret);
282	return ret;
283}
284
285static int dmic_init(struct snd_soc_pcm_runtime *rtd)
286{
287	struct snd_soc_card *card = rtd->card;
288	int ret;
289
290	ret = snd_soc_dapm_new_controls(&card->dapm, dmic_widgets,
291					ARRAY_SIZE(dmic_widgets));
292	if (ret) {
293		dev_err(card->dev, "DMic widget addition failed: %d\n", ret);
294		/* Don't need to add routes if widget addition failed */
295		return ret;
296	}
297
298	ret = snd_soc_dapm_add_routes(&card->dapm, dmic_map,
299				      ARRAY_SIZE(dmic_map));
300
301	if (ret)
302		dev_err(card->dev, "DMic map addition failed: %d\n", ret);
303
304	return ret;
305}
306
307/* sof audio machine driver for nau8825 codec */
308static struct snd_soc_card sof_audio_card_nau8825 = {
309	.name = "nau8825", /* the sof- prefix is added by the core */
310	.owner = THIS_MODULE,
311	.controls = sof_controls,
312	.num_controls = ARRAY_SIZE(sof_controls),
313	.dapm_widgets = sof_widgets,
314	.num_dapm_widgets = ARRAY_SIZE(sof_widgets),
315	.dapm_routes = sof_map,
316	.num_dapm_routes = ARRAY_SIZE(sof_map),
317	.fully_routed = true,
318	.late_probe = sof_card_late_probe,
319};
320
321static struct snd_soc_dai_link_component nau8825_component[] = {
322	{
323		.name = "i2c-10508825:00",
324		.dai_name = "nau8825-hifi",
325	}
326};
327
328static struct snd_soc_dai_link_component dmic_component[] = {
329	{
330		.name = "dmic-codec",
331		.dai_name = "dmic-hifi",
 
 
 
 
 
 
 
 
 
332	}
333};
334
335static struct snd_soc_dai_link_component rt1019p_component[] = {
336	{
337		.name = "RTL1019:00",
338		.dai_name = "HiFi",
339	}
340};
 
 
 
341
342static struct snd_soc_dai_link_component nau8318_components[] = {
343	{
344		.name = "NVTN2012:00",
345		.dai_name = "nau8315-hifi",
346	}
347};
348
349static struct snd_soc_dai_link_component dummy_component[] = {
350	{
351		.name = "snd-soc-dummy",
352		.dai_name = "snd-soc-dummy-dai",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
353	}
354};
355
356static struct snd_soc_dai_link *sof_card_dai_links_create(struct device *dev,
357							  int ssp_codec,
358							  int ssp_amp,
359							  int dmic_be_num,
360							  int hdmi_num)
361{
362	struct snd_soc_dai_link_component *idisp_components;
363	struct snd_soc_dai_link_component *cpus;
364	struct snd_soc_dai_link *links;
365	int i, id = 0;
366
367	links = devm_kcalloc(dev, sof_audio_card_nau8825.num_links,
368			    sizeof(struct snd_soc_dai_link), GFP_KERNEL);
369	cpus = devm_kcalloc(dev, sof_audio_card_nau8825.num_links,
370			    sizeof(struct snd_soc_dai_link_component), GFP_KERNEL);
371	if (!links || !cpus)
372		goto devm_err;
373
374	/* codec SSP */
375	links[id].name = devm_kasprintf(dev, GFP_KERNEL,
376					"SSP%d-Codec", ssp_codec);
377	if (!links[id].name)
378		goto devm_err;
379
380	links[id].id = id;
381	links[id].codecs = nau8825_component;
382	links[id].num_codecs = ARRAY_SIZE(nau8825_component);
383	links[id].platforms = platform_component;
384	links[id].num_platforms = ARRAY_SIZE(platform_component);
385	links[id].init = sof_nau8825_codec_init;
386	links[id].exit = sof_nau8825_codec_exit;
387	links[id].ops = &sof_nau8825_ops;
388	links[id].dpcm_playback = 1;
389	links[id].dpcm_capture = 1;
390	links[id].no_pcm = 1;
391	links[id].cpus = &cpus[id];
392	links[id].num_cpus = 1;
393
394	links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
395						  "SSP%d Pin",
396						  ssp_codec);
397	if (!links[id].cpus->dai_name)
398		goto devm_err;
399
400	id++;
401
402	/* dmic */
403	if (dmic_be_num > 0) {
404		/* at least we have dmic01 */
405		links[id].name = "dmic01";
406		links[id].cpus = &cpus[id];
407		links[id].cpus->dai_name = "DMIC01 Pin";
408		links[id].init = dmic_init;
409		if (dmic_be_num > 1) {
410			/* set up 2 BE links at most */
411			links[id + 1].name = "dmic16k";
412			links[id + 1].cpus = &cpus[id + 1];
413			links[id + 1].cpus->dai_name = "DMIC16k Pin";
414			dmic_be_num = 2;
415		}
416	}
417
418	for (i = 0; i < dmic_be_num; i++) {
419		links[id].id = id;
420		links[id].num_cpus = 1;
421		links[id].codecs = dmic_component;
422		links[id].num_codecs = ARRAY_SIZE(dmic_component);
423		links[id].platforms = platform_component;
424		links[id].num_platforms = ARRAY_SIZE(platform_component);
425		links[id].ignore_suspend = 1;
426		links[id].dpcm_capture = 1;
427		links[id].no_pcm = 1;
428		id++;
429	}
430
431	/* HDMI */
432	if (hdmi_num > 0) {
433		idisp_components = devm_kcalloc(dev,
434						hdmi_num,
435						sizeof(struct snd_soc_dai_link_component),
436						GFP_KERNEL);
437		if (!idisp_components)
438			goto devm_err;
439	}
440	for (i = 1; i <= hdmi_num; i++) {
441		links[id].name = devm_kasprintf(dev, GFP_KERNEL,
442						"iDisp%d", i);
443		if (!links[id].name)
444			goto devm_err;
445
446		links[id].id = id;
447		links[id].cpus = &cpus[id];
448		links[id].num_cpus = 1;
449		links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
450							  "iDisp%d Pin", i);
451		if (!links[id].cpus->dai_name)
452			goto devm_err;
453
454		idisp_components[i - 1].name = "ehdaudio0D2";
455		idisp_components[i - 1].dai_name = devm_kasprintf(dev,
456								  GFP_KERNEL,
457								  "intel-hdmi-hifi%d",
458								  i);
459		if (!idisp_components[i - 1].dai_name)
460			goto devm_err;
461
462		links[id].codecs = &idisp_components[i - 1];
463		links[id].num_codecs = 1;
464		links[id].platforms = platform_component;
465		links[id].num_platforms = ARRAY_SIZE(platform_component);
466		links[id].init = sof_hdmi_init;
467		links[id].dpcm_playback = 1;
468		links[id].no_pcm = 1;
469		id++;
470	}
471
472	/* speaker amp */
473	if (sof_nau8825_quirk & SOF_SPEAKER_AMP_PRESENT) {
474		links[id].name = devm_kasprintf(dev, GFP_KERNEL,
475						"SSP%d-Codec", ssp_amp);
476		if (!links[id].name)
477			goto devm_err;
478
479		links[id].id = id;
480		if (sof_nau8825_quirk & SOF_RT1019P_SPEAKER_AMP_PRESENT) {
481			links[id].codecs = rt1019p_component;
482			links[id].num_codecs = ARRAY_SIZE(rt1019p_component);
483			links[id].init = speaker_codec_init;
484		} else if (sof_nau8825_quirk &
485				SOF_MAX98373_SPEAKER_AMP_PRESENT) {
486			links[id].codecs = max_98373_components;
487			links[id].num_codecs = ARRAY_SIZE(max_98373_components);
488			links[id].init = max_98373_spk_codec_init;
489			links[id].ops = &max_98373_ops;
490		} else if (sof_nau8825_quirk &
491				SOF_MAX98360A_SPEAKER_AMP_PRESENT) {
492			max_98360a_dai_link(&links[id]);
493		} else if (sof_nau8825_quirk & SOF_RT1015P_SPEAKER_AMP_PRESENT) {
494			sof_rt1015p_dai_link(&links[id]);
495		} else if (sof_nau8825_quirk &
496				SOF_NAU8318_SPEAKER_AMP_PRESENT) {
497			links[id].codecs = nau8318_components;
498			links[id].num_codecs = ARRAY_SIZE(nau8318_components);
499			links[id].init = speaker_codec_init;
500		} else {
501			goto devm_err;
502		}
503
504		links[id].platforms = platform_component;
505		links[id].num_platforms = ARRAY_SIZE(platform_component);
506		links[id].dpcm_playback = 1;
507		/* feedback stream or firmware-generated echo reference */
508		links[id].dpcm_capture = 1;
509
510		links[id].no_pcm = 1;
511		links[id].cpus = &cpus[id];
512		links[id].num_cpus = 1;
513		links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
514							  "SSP%d Pin",
515							  ssp_amp);
516		if (!links[id].cpus->dai_name)
517			goto devm_err;
518		id++;
519	}
520
521	/* BT audio offload */
522	if (sof_nau8825_quirk & SOF_SSP_BT_OFFLOAD_PRESENT) {
523		int port = (sof_nau8825_quirk & SOF_BT_OFFLOAD_SSP_MASK) >>
524				SOF_BT_OFFLOAD_SSP_SHIFT;
525
526		links[id].id = id;
527		links[id].cpus = &cpus[id];
528		links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
529							  "SSP%d Pin", port);
530		if (!links[id].cpus->dai_name)
531			goto devm_err;
532		links[id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-BT", port);
533		if (!links[id].name)
534			goto devm_err;
535		links[id].codecs = dummy_component;
536		links[id].num_codecs = ARRAY_SIZE(dummy_component);
537		links[id].platforms = platform_component;
538		links[id].num_platforms = ARRAY_SIZE(platform_component);
539		links[id].dpcm_playback = 1;
540		links[id].dpcm_capture = 1;
541		links[id].no_pcm = 1;
542		links[id].num_cpus = 1;
543	}
544
545	return links;
546devm_err:
547	return NULL;
548}
549
550static int sof_audio_probe(struct platform_device *pdev)
551{
552	struct snd_soc_dai_link *dai_links;
553	struct snd_soc_acpi_mach *mach;
554	struct sof_card_private *ctx;
555	int dmic_be_num, hdmi_num;
556	int ret, ssp_amp, ssp_codec;
557
558	ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
559	if (!ctx)
560		return -ENOMEM;
561
562	if (pdev->id_entry && pdev->id_entry->driver_data)
563		sof_nau8825_quirk = (unsigned long)pdev->id_entry->driver_data;
564
565	mach = pdev->dev.platform_data;
566
567	/* A speaker amp might not be present when the quirk claims one is.
568	 * Detect this via whether the machine driver match includes quirk_data.
569	 */
570	if ((sof_nau8825_quirk & SOF_SPEAKER_AMP_PRESENT) && !mach->quirk_data)
571		sof_nau8825_quirk &= ~SOF_SPEAKER_AMP_PRESENT;
572
573	dev_dbg(&pdev->dev, "sof_nau8825_quirk = %lx\n", sof_nau8825_quirk);
574
575	/* default number of DMIC DAI's */
576	dmic_be_num = 2;
577	hdmi_num = (sof_nau8825_quirk & SOF_NAU8825_NUM_HDMIDEV_MASK) >>
578			SOF_NAU8825_NUM_HDMIDEV_SHIFT;
579	/* default number of HDMI DAI's */
580	if (!hdmi_num)
581		hdmi_num = 3;
582
583	ssp_amp = (sof_nau8825_quirk & SOF_NAU8825_SSP_AMP_MASK) >>
584			SOF_NAU8825_SSP_AMP_SHIFT;
585
586	ssp_codec = sof_nau8825_quirk & SOF_NAU8825_SSP_CODEC_MASK;
587
588	/* compute number of dai links */
589	sof_audio_card_nau8825.num_links = 1 + dmic_be_num + hdmi_num;
590
591	if (sof_nau8825_quirk & SOF_SPEAKER_AMP_PRESENT)
592		sof_audio_card_nau8825.num_links++;
 
 
593
594	if (sof_nau8825_quirk & SOF_MAX98373_SPEAKER_AMP_PRESENT)
 
 
595		max_98373_set_codec_conf(&sof_audio_card_nau8825);
596	else if (sof_nau8825_quirk & SOF_RT1015P_SPEAKER_AMP_PRESENT)
 
597		sof_rt1015p_codec_conf(&sof_audio_card_nau8825);
598
599	if (sof_nau8825_quirk & SOF_SSP_BT_OFFLOAD_PRESENT)
600		sof_audio_card_nau8825.num_links++;
601
602	dai_links = sof_card_dai_links_create(&pdev->dev, ssp_codec, ssp_amp,
603					      dmic_be_num, hdmi_num);
604	if (!dai_links)
605		return -ENOMEM;
606
607	sof_audio_card_nau8825.dai_link = dai_links;
608
609	INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
610
611	sof_audio_card_nau8825.dev = &pdev->dev;
612
613	/* set platform name for each dailink */
614	ret = snd_soc_fixup_dai_links_platform_name(&sof_audio_card_nau8825,
615						    mach->mach_params.platform);
616	if (ret)
617		return ret;
618
619	snd_soc_card_set_drvdata(&sof_audio_card_nau8825, ctx);
620
621	return devm_snd_soc_register_card(&pdev->dev,
622					  &sof_audio_card_nau8825);
623}
624
625static const struct platform_device_id board_ids[] = {
626	{
627		.name = "sof_nau8825",
628		.driver_data = (kernel_ulong_t)(SOF_NAU8825_SSP_CODEC(0) |
629					SOF_NAU8825_NUM_HDMIDEV(4) |
630					SOF_BT_OFFLOAD_SSP(2) |
631					SOF_SSP_BT_OFFLOAD_PRESENT),
632
633	},
634	{
635		.name = "adl_rt1019p_8825",
636		.driver_data = (kernel_ulong_t)(SOF_NAU8825_SSP_CODEC(0) |
637					SOF_SPEAKER_AMP_PRESENT |
638					SOF_RT1019P_SPEAKER_AMP_PRESENT |
639					SOF_NAU8825_SSP_AMP(2) |
640					SOF_NAU8825_NUM_HDMIDEV(4)),
641	},
642	{
643		.name = "adl_max98373_8825",
644		.driver_data = (kernel_ulong_t)(SOF_NAU8825_SSP_CODEC(0) |
645					SOF_SPEAKER_AMP_PRESENT |
646					SOF_MAX98373_SPEAKER_AMP_PRESENT |
647					SOF_NAU8825_SSP_AMP(1) |
648					SOF_NAU8825_NUM_HDMIDEV(4) |
649					SOF_BT_OFFLOAD_SSP(2) |
650					SOF_SSP_BT_OFFLOAD_PRESENT),
651	},
652	{
653		/* The limitation of length of char array, shorten the name */
654		.name = "adl_mx98360a_8825",
655		.driver_data = (kernel_ulong_t)(SOF_NAU8825_SSP_CODEC(0) |
656					SOF_SPEAKER_AMP_PRESENT |
657					SOF_MAX98360A_SPEAKER_AMP_PRESENT |
658					SOF_NAU8825_SSP_AMP(1) |
659					SOF_NAU8825_NUM_HDMIDEV(4) |
660					SOF_BT_OFFLOAD_SSP(2) |
661					SOF_SSP_BT_OFFLOAD_PRESENT),
662
663	},
664	{
665		.name = "adl_rt1015p_8825",
666		.driver_data = (kernel_ulong_t)(SOF_NAU8825_SSP_CODEC(0) |
667					SOF_SPEAKER_AMP_PRESENT |
668					SOF_RT1015P_SPEAKER_AMP_PRESENT |
669					SOF_NAU8825_SSP_AMP(1) |
670					SOF_NAU8825_NUM_HDMIDEV(4) |
671					SOF_BT_OFFLOAD_SSP(2) |
672					SOF_SSP_BT_OFFLOAD_PRESENT),
673	},
674	{
675		.name = "adl_nau8318_8825",
676		.driver_data = (kernel_ulong_t)(SOF_NAU8825_SSP_CODEC(0) |
677					SOF_SPEAKER_AMP_PRESENT |
678					SOF_NAU8318_SPEAKER_AMP_PRESENT |
679					SOF_NAU8825_SSP_AMP(1) |
680					SOF_NAU8825_NUM_HDMIDEV(4) |
681					SOF_BT_OFFLOAD_SSP(2) |
682					SOF_SSP_BT_OFFLOAD_PRESENT),
683	},
684	{ }
685};
686MODULE_DEVICE_TABLE(platform, board_ids);
687
688static struct platform_driver sof_audio = {
689	.probe = sof_audio_probe,
690	.driver = {
691		.name = "sof_nau8825",
692		.pm = &snd_soc_pm_ops,
693	},
694	.id_table = board_ids,
695};
696module_platform_driver(sof_audio)
697
698/* Module information */
699MODULE_DESCRIPTION("SOF Audio Machine driver for NAU8825");
700MODULE_AUTHOR("David Lin <ctlin0@nuvoton.com>");
701MODULE_AUTHOR("Mac Chiang <mac.chiang@intel.com>");
 
702MODULE_LICENSE("GPL");
703MODULE_IMPORT_NS(SND_SOC_INTEL_HDA_DSP_COMMON);
704MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_MAXIM_COMMON);
705MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_REALTEK_COMMON);