1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun //
3*4882a593Smuzhiyun // Copyright (c) 2020 BayLibre, SAS.
4*4882a593Smuzhiyun // Author: Jerome Brunet <jbrunet@baylibre.com>
5*4882a593Smuzhiyun
6*4882a593Smuzhiyun #include <linux/bitfield.h>
7*4882a593Smuzhiyun #include <linux/clk.h>
8*4882a593Smuzhiyun #include <sound/pcm_params.h>
9*4882a593Smuzhiyun #include <sound/pcm_iec958.h>
10*4882a593Smuzhiyun #include <sound/soc.h>
11*4882a593Smuzhiyun #include <sound/soc-dai.h>
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun #include "aiu.h"
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun #define AIU_958_MISC_NON_PCM BIT(0)
16*4882a593Smuzhiyun #define AIU_958_MISC_MODE_16BITS BIT(1)
17*4882a593Smuzhiyun #define AIU_958_MISC_16BITS_ALIGN GENMASK(6, 5)
18*4882a593Smuzhiyun #define AIU_958_MISC_MODE_32BITS BIT(7)
19*4882a593Smuzhiyun #define AIU_958_MISC_U_FROM_STREAM BIT(12)
20*4882a593Smuzhiyun #define AIU_958_MISC_FORCE_LR BIT(13)
21*4882a593Smuzhiyun #define AIU_958_CTRL_HOLD_EN BIT(0)
22*4882a593Smuzhiyun #define AIU_CLK_CTRL_958_DIV_EN BIT(1)
23*4882a593Smuzhiyun #define AIU_CLK_CTRL_958_DIV GENMASK(5, 4)
24*4882a593Smuzhiyun #define AIU_CLK_CTRL_958_DIV_MORE BIT(12)
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun #define AIU_CS_WORD_LEN 4
27*4882a593Smuzhiyun #define AIU_958_INTERNAL_DIV 2
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun static void
aiu_encoder_spdif_divider_enable(struct snd_soc_component * component,bool enable)30*4882a593Smuzhiyun aiu_encoder_spdif_divider_enable(struct snd_soc_component *component,
31*4882a593Smuzhiyun bool enable)
32*4882a593Smuzhiyun {
33*4882a593Smuzhiyun snd_soc_component_update_bits(component, AIU_CLK_CTRL,
34*4882a593Smuzhiyun AIU_CLK_CTRL_958_DIV_EN,
35*4882a593Smuzhiyun enable ? AIU_CLK_CTRL_958_DIV_EN : 0);
36*4882a593Smuzhiyun }
37*4882a593Smuzhiyun
aiu_encoder_spdif_hold(struct snd_soc_component * component,bool enable)38*4882a593Smuzhiyun static void aiu_encoder_spdif_hold(struct snd_soc_component *component,
39*4882a593Smuzhiyun bool enable)
40*4882a593Smuzhiyun {
41*4882a593Smuzhiyun snd_soc_component_update_bits(component, AIU_958_CTRL,
42*4882a593Smuzhiyun AIU_958_CTRL_HOLD_EN,
43*4882a593Smuzhiyun enable ? AIU_958_CTRL_HOLD_EN : 0);
44*4882a593Smuzhiyun }
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun static int
aiu_encoder_spdif_trigger(struct snd_pcm_substream * substream,int cmd,struct snd_soc_dai * dai)47*4882a593Smuzhiyun aiu_encoder_spdif_trigger(struct snd_pcm_substream *substream, int cmd,
48*4882a593Smuzhiyun struct snd_soc_dai *dai)
49*4882a593Smuzhiyun {
50*4882a593Smuzhiyun struct snd_soc_component *component = dai->component;
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun switch (cmd) {
53*4882a593Smuzhiyun case SNDRV_PCM_TRIGGER_START:
54*4882a593Smuzhiyun case SNDRV_PCM_TRIGGER_RESUME:
55*4882a593Smuzhiyun case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
56*4882a593Smuzhiyun aiu_encoder_spdif_hold(component, false);
57*4882a593Smuzhiyun return 0;
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun case SNDRV_PCM_TRIGGER_STOP:
60*4882a593Smuzhiyun case SNDRV_PCM_TRIGGER_SUSPEND:
61*4882a593Smuzhiyun case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
62*4882a593Smuzhiyun aiu_encoder_spdif_hold(component, true);
63*4882a593Smuzhiyun return 0;
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun default:
66*4882a593Smuzhiyun return -EINVAL;
67*4882a593Smuzhiyun }
68*4882a593Smuzhiyun }
69*4882a593Smuzhiyun
aiu_encoder_spdif_setup_cs_word(struct snd_soc_component * component,struct snd_pcm_hw_params * params)70*4882a593Smuzhiyun static int aiu_encoder_spdif_setup_cs_word(struct snd_soc_component *component,
71*4882a593Smuzhiyun struct snd_pcm_hw_params *params)
72*4882a593Smuzhiyun {
73*4882a593Smuzhiyun u8 cs[AIU_CS_WORD_LEN];
74*4882a593Smuzhiyun unsigned int val;
75*4882a593Smuzhiyun int ret;
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun ret = snd_pcm_create_iec958_consumer_hw_params(params, cs,
78*4882a593Smuzhiyun AIU_CS_WORD_LEN);
79*4882a593Smuzhiyun if (ret < 0)
80*4882a593Smuzhiyun return ret;
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun /* Write the 1st half word */
83*4882a593Smuzhiyun val = cs[1] | cs[0] << 8;
84*4882a593Smuzhiyun snd_soc_component_write(component, AIU_958_CHSTAT_L0, val);
85*4882a593Smuzhiyun snd_soc_component_write(component, AIU_958_CHSTAT_R0, val);
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun /* Write the 2nd half word */
88*4882a593Smuzhiyun val = cs[3] | cs[2] << 8;
89*4882a593Smuzhiyun snd_soc_component_write(component, AIU_958_CHSTAT_L1, val);
90*4882a593Smuzhiyun snd_soc_component_write(component, AIU_958_CHSTAT_R1, val);
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun return 0;
93*4882a593Smuzhiyun }
94*4882a593Smuzhiyun
aiu_encoder_spdif_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)95*4882a593Smuzhiyun static int aiu_encoder_spdif_hw_params(struct snd_pcm_substream *substream,
96*4882a593Smuzhiyun struct snd_pcm_hw_params *params,
97*4882a593Smuzhiyun struct snd_soc_dai *dai)
98*4882a593Smuzhiyun {
99*4882a593Smuzhiyun struct snd_soc_component *component = dai->component;
100*4882a593Smuzhiyun struct aiu *aiu = snd_soc_component_get_drvdata(component);
101*4882a593Smuzhiyun unsigned int val = 0, mrate;
102*4882a593Smuzhiyun int ret;
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun /* Disable the clock while changing the settings */
105*4882a593Smuzhiyun aiu_encoder_spdif_divider_enable(component, false);
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun switch (params_physical_width(params)) {
108*4882a593Smuzhiyun case 16:
109*4882a593Smuzhiyun val |= AIU_958_MISC_MODE_16BITS;
110*4882a593Smuzhiyun val |= FIELD_PREP(AIU_958_MISC_16BITS_ALIGN, 2);
111*4882a593Smuzhiyun break;
112*4882a593Smuzhiyun case 32:
113*4882a593Smuzhiyun val |= AIU_958_MISC_MODE_32BITS;
114*4882a593Smuzhiyun break;
115*4882a593Smuzhiyun default:
116*4882a593Smuzhiyun dev_err(dai->dev, "Unsupport physical width\n");
117*4882a593Smuzhiyun return -EINVAL;
118*4882a593Smuzhiyun }
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun snd_soc_component_update_bits(component, AIU_958_MISC,
121*4882a593Smuzhiyun AIU_958_MISC_NON_PCM |
122*4882a593Smuzhiyun AIU_958_MISC_MODE_16BITS |
123*4882a593Smuzhiyun AIU_958_MISC_16BITS_ALIGN |
124*4882a593Smuzhiyun AIU_958_MISC_MODE_32BITS |
125*4882a593Smuzhiyun AIU_958_MISC_FORCE_LR |
126*4882a593Smuzhiyun AIU_958_MISC_U_FROM_STREAM,
127*4882a593Smuzhiyun val);
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun /* Set the stream channel status word */
130*4882a593Smuzhiyun ret = aiu_encoder_spdif_setup_cs_word(component, params);
131*4882a593Smuzhiyun if (ret) {
132*4882a593Smuzhiyun dev_err(dai->dev, "failed to set channel status word\n");
133*4882a593Smuzhiyun return ret;
134*4882a593Smuzhiyun }
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun snd_soc_component_update_bits(component, AIU_CLK_CTRL,
137*4882a593Smuzhiyun AIU_CLK_CTRL_958_DIV |
138*4882a593Smuzhiyun AIU_CLK_CTRL_958_DIV_MORE,
139*4882a593Smuzhiyun FIELD_PREP(AIU_CLK_CTRL_958_DIV,
140*4882a593Smuzhiyun __ffs(AIU_958_INTERNAL_DIV)));
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun /* 2 * 32bits per subframe * 2 channels = 128 */
143*4882a593Smuzhiyun mrate = params_rate(params) * 128 * AIU_958_INTERNAL_DIV;
144*4882a593Smuzhiyun ret = clk_set_rate(aiu->spdif.clks[MCLK].clk, mrate);
145*4882a593Smuzhiyun if (ret) {
146*4882a593Smuzhiyun dev_err(dai->dev, "failed to set mclk rate\n");
147*4882a593Smuzhiyun return ret;
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun aiu_encoder_spdif_divider_enable(component, true);
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun return 0;
153*4882a593Smuzhiyun }
154*4882a593Smuzhiyun
aiu_encoder_spdif_hw_free(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)155*4882a593Smuzhiyun static int aiu_encoder_spdif_hw_free(struct snd_pcm_substream *substream,
156*4882a593Smuzhiyun struct snd_soc_dai *dai)
157*4882a593Smuzhiyun {
158*4882a593Smuzhiyun struct snd_soc_component *component = dai->component;
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun aiu_encoder_spdif_divider_enable(component, false);
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun return 0;
163*4882a593Smuzhiyun }
164*4882a593Smuzhiyun
aiu_encoder_spdif_startup(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)165*4882a593Smuzhiyun static int aiu_encoder_spdif_startup(struct snd_pcm_substream *substream,
166*4882a593Smuzhiyun struct snd_soc_dai *dai)
167*4882a593Smuzhiyun {
168*4882a593Smuzhiyun struct aiu *aiu = snd_soc_component_get_drvdata(dai->component);
169*4882a593Smuzhiyun int ret;
170*4882a593Smuzhiyun
171*4882a593Smuzhiyun /*
172*4882a593Smuzhiyun * NOTE: Make sure the spdif block is on its own divider.
173*4882a593Smuzhiyun *
174*4882a593Smuzhiyun * The spdif can be clocked by the i2s master clock or its own
175*4882a593Smuzhiyun * clock. We should (in theory) change the source depending on the
176*4882a593Smuzhiyun * origin of the data.
177*4882a593Smuzhiyun *
178*4882a593Smuzhiyun * However, considering the clocking scheme used on these platforms,
179*4882a593Smuzhiyun * the master clocks will pick the same PLL source when they are
180*4882a593Smuzhiyun * playing from the same FIFO. The clock should be in sync so, it
181*4882a593Smuzhiyun * should not be necessary to reparent the spdif master clock.
182*4882a593Smuzhiyun */
183*4882a593Smuzhiyun ret = clk_set_parent(aiu->spdif.clks[MCLK].clk,
184*4882a593Smuzhiyun aiu->spdif_mclk);
185*4882a593Smuzhiyun if (ret)
186*4882a593Smuzhiyun return ret;
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun ret = clk_bulk_prepare_enable(aiu->spdif.clk_num, aiu->spdif.clks);
189*4882a593Smuzhiyun if (ret)
190*4882a593Smuzhiyun dev_err(dai->dev, "failed to enable spdif clocks\n");
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun return ret;
193*4882a593Smuzhiyun }
194*4882a593Smuzhiyun
aiu_encoder_spdif_shutdown(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)195*4882a593Smuzhiyun static void aiu_encoder_spdif_shutdown(struct snd_pcm_substream *substream,
196*4882a593Smuzhiyun struct snd_soc_dai *dai)
197*4882a593Smuzhiyun {
198*4882a593Smuzhiyun struct aiu *aiu = snd_soc_component_get_drvdata(dai->component);
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun clk_bulk_disable_unprepare(aiu->spdif.clk_num, aiu->spdif.clks);
201*4882a593Smuzhiyun }
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun const struct snd_soc_dai_ops aiu_encoder_spdif_dai_ops = {
204*4882a593Smuzhiyun .trigger = aiu_encoder_spdif_trigger,
205*4882a593Smuzhiyun .hw_params = aiu_encoder_spdif_hw_params,
206*4882a593Smuzhiyun .hw_free = aiu_encoder_spdif_hw_free,
207*4882a593Smuzhiyun .startup = aiu_encoder_spdif_startup,
208*4882a593Smuzhiyun .shutdown = aiu_encoder_spdif_shutdown,
209*4882a593Smuzhiyun };
210