xref: /OK3568_Linux_fs/kernel/sound/soc/intel/boards/sof_sdw_rt1308.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun // Copyright (c) 2020 Intel Corporation
3*4882a593Smuzhiyun 
4*4882a593Smuzhiyun /*
5*4882a593Smuzhiyun  *  sof_sdw_rt1308 - Helpers to handle RT1308 from generic machine driver
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <linux/device.h>
9*4882a593Smuzhiyun #include <linux/errno.h>
10*4882a593Smuzhiyun #include <sound/control.h>
11*4882a593Smuzhiyun #include <sound/soc.h>
12*4882a593Smuzhiyun #include <sound/soc-acpi.h>
13*4882a593Smuzhiyun #include <sound/soc-dapm.h>
14*4882a593Smuzhiyun #include "sof_sdw_common.h"
15*4882a593Smuzhiyun #include "../../codecs/rt1308.h"
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun static const struct snd_soc_dapm_widget rt1308_widgets[] = {
18*4882a593Smuzhiyun 	SND_SOC_DAPM_SPK("Speaker", NULL),
19*4882a593Smuzhiyun };
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun /*
22*4882a593Smuzhiyun  * dapm routes for rt1308 will be registered dynamically according
23*4882a593Smuzhiyun  * to the number of rt1308 used. The first two entries will be registered
24*4882a593Smuzhiyun  * for one codec case, and the last two entries are also registered
25*4882a593Smuzhiyun  * if two 1308s are used.
26*4882a593Smuzhiyun  */
27*4882a593Smuzhiyun static const struct snd_soc_dapm_route rt1308_map[] = {
28*4882a593Smuzhiyun 	{ "Speaker", NULL, "rt1308-1 SPOL" },
29*4882a593Smuzhiyun 	{ "Speaker", NULL, "rt1308-1 SPOR" },
30*4882a593Smuzhiyun 	{ "Speaker", NULL, "rt1308-2 SPOL" },
31*4882a593Smuzhiyun 	{ "Speaker", NULL, "rt1308-2 SPOR" },
32*4882a593Smuzhiyun };
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun static const struct snd_kcontrol_new rt1308_controls[] = {
35*4882a593Smuzhiyun 	SOC_DAPM_PIN_SWITCH("Speaker"),
36*4882a593Smuzhiyun };
37*4882a593Smuzhiyun 
first_spk_init(struct snd_soc_pcm_runtime * rtd)38*4882a593Smuzhiyun static int first_spk_init(struct snd_soc_pcm_runtime *rtd)
39*4882a593Smuzhiyun {
40*4882a593Smuzhiyun 	struct snd_soc_card *card = rtd->card;
41*4882a593Smuzhiyun 	int ret;
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun 	card->components = devm_kasprintf(card->dev, GFP_KERNEL,
44*4882a593Smuzhiyun 					  "%s spk:rt1308",
45*4882a593Smuzhiyun 					  card->components);
46*4882a593Smuzhiyun 	if (!card->components)
47*4882a593Smuzhiyun 		return -ENOMEM;
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun 	ret = snd_soc_add_card_controls(card, rt1308_controls,
50*4882a593Smuzhiyun 					ARRAY_SIZE(rt1308_controls));
51*4882a593Smuzhiyun 	if (ret) {
52*4882a593Smuzhiyun 		dev_err(card->dev, "rt1308 controls addition failed: %d\n", ret);
53*4882a593Smuzhiyun 		return ret;
54*4882a593Smuzhiyun 	}
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun 	ret = snd_soc_dapm_new_controls(&card->dapm, rt1308_widgets,
57*4882a593Smuzhiyun 					ARRAY_SIZE(rt1308_widgets));
58*4882a593Smuzhiyun 	if (ret) {
59*4882a593Smuzhiyun 		dev_err(card->dev, "rt1308 widgets addition failed: %d\n", ret);
60*4882a593Smuzhiyun 		return ret;
61*4882a593Smuzhiyun 	}
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun 	ret = snd_soc_dapm_add_routes(&card->dapm, rt1308_map, 2);
64*4882a593Smuzhiyun 	if (ret)
65*4882a593Smuzhiyun 		dev_err(rtd->dev, "failed to add first SPK map: %d\n", ret);
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun 	return ret;
68*4882a593Smuzhiyun }
69*4882a593Smuzhiyun 
second_spk_init(struct snd_soc_pcm_runtime * rtd)70*4882a593Smuzhiyun static int second_spk_init(struct snd_soc_pcm_runtime *rtd)
71*4882a593Smuzhiyun {
72*4882a593Smuzhiyun 	struct snd_soc_card *card = rtd->card;
73*4882a593Smuzhiyun 	int ret;
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun 	ret = snd_soc_dapm_add_routes(&card->dapm, rt1308_map + 2, 2);
76*4882a593Smuzhiyun 	if (ret)
77*4882a593Smuzhiyun 		dev_err(rtd->dev, "failed to add second SPK map: %d\n", ret);
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun 	return ret;
80*4882a593Smuzhiyun }
81*4882a593Smuzhiyun 
all_spk_init(struct snd_soc_pcm_runtime * rtd)82*4882a593Smuzhiyun static int all_spk_init(struct snd_soc_pcm_runtime *rtd)
83*4882a593Smuzhiyun {
84*4882a593Smuzhiyun 	int ret;
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun 	ret = first_spk_init(rtd);
87*4882a593Smuzhiyun 	if (ret)
88*4882a593Smuzhiyun 		return ret;
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun 	return second_spk_init(rtd);
91*4882a593Smuzhiyun }
92*4882a593Smuzhiyun 
rt1308_i2s_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)93*4882a593Smuzhiyun static int rt1308_i2s_hw_params(struct snd_pcm_substream *substream,
94*4882a593Smuzhiyun 				struct snd_pcm_hw_params *params)
95*4882a593Smuzhiyun {
96*4882a593Smuzhiyun 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
97*4882a593Smuzhiyun 	struct snd_soc_card *card = rtd->card;
98*4882a593Smuzhiyun 	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
99*4882a593Smuzhiyun 	int clk_id, clk_freq, pll_out;
100*4882a593Smuzhiyun 	int err;
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun 	clk_id = RT1308_PLL_S_MCLK;
103*4882a593Smuzhiyun 	clk_freq = 38400000;
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 	pll_out = params_rate(params) * 512;
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun 	/* Set rt1308 pll */
108*4882a593Smuzhiyun 	err = snd_soc_dai_set_pll(codec_dai, 0, clk_id, clk_freq, pll_out);
109*4882a593Smuzhiyun 	if (err < 0) {
110*4882a593Smuzhiyun 		dev_err(card->dev, "Failed to set RT1308 PLL: %d\n", err);
111*4882a593Smuzhiyun 		return err;
112*4882a593Smuzhiyun 	}
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun 	/* Set rt1308 sysclk */
115*4882a593Smuzhiyun 	err = snd_soc_dai_set_sysclk(codec_dai, RT1308_FS_SYS_S_PLL, pll_out,
116*4882a593Smuzhiyun 				     SND_SOC_CLOCK_IN);
117*4882a593Smuzhiyun 	if (err < 0) {
118*4882a593Smuzhiyun 		dev_err(card->dev, "Failed to set RT1308 SYSCLK: %d\n", err);
119*4882a593Smuzhiyun 		return err;
120*4882a593Smuzhiyun 	}
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun 	return 0;
123*4882a593Smuzhiyun }
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun /* machine stream operations */
126*4882a593Smuzhiyun struct snd_soc_ops sof_sdw_rt1308_i2s_ops = {
127*4882a593Smuzhiyun 	.hw_params = rt1308_i2s_hw_params,
128*4882a593Smuzhiyun };
129*4882a593Smuzhiyun 
sof_sdw_rt1308_init(const struct snd_soc_acpi_link_adr * link,struct snd_soc_dai_link * dai_links,struct sof_sdw_codec_info * info,bool playback)130*4882a593Smuzhiyun int sof_sdw_rt1308_init(const struct snd_soc_acpi_link_adr *link,
131*4882a593Smuzhiyun 			struct snd_soc_dai_link *dai_links,
132*4882a593Smuzhiyun 			struct sof_sdw_codec_info *info,
133*4882a593Smuzhiyun 			bool playback)
134*4882a593Smuzhiyun {
135*4882a593Smuzhiyun 	/* Count amp number and do init on playback link only. */
136*4882a593Smuzhiyun 	if (!playback)
137*4882a593Smuzhiyun 		return 0;
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	info->amp_num++;
140*4882a593Smuzhiyun 	if (info->amp_num == 1)
141*4882a593Smuzhiyun 		dai_links->init = first_spk_init;
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 	if (info->amp_num == 2) {
144*4882a593Smuzhiyun 		/*
145*4882a593Smuzhiyun 		 * if two 1308s are in one dai link, the init function
146*4882a593Smuzhiyun 		 * in this dai link will be first set for the first speaker,
147*4882a593Smuzhiyun 		 * and it should be reset to initialize all speakers when
148*4882a593Smuzhiyun 		 * the second speaker is found.
149*4882a593Smuzhiyun 		 */
150*4882a593Smuzhiyun 		if (dai_links->init)
151*4882a593Smuzhiyun 			dai_links->init = all_spk_init;
152*4882a593Smuzhiyun 		else
153*4882a593Smuzhiyun 			dai_links->init = second_spk_init;
154*4882a593Smuzhiyun 	}
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 	return 0;
157*4882a593Smuzhiyun }
158