Debian Codegraph @

sound/soc/fsl/fsl_xcvr.c

raw
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright 2019 NXP
3
4 #include <linux/bitrev.h>
5 #include <linux/clk.h>
6 #include <linux/firmware.h>
7 #include <linux/interrupt.h>
8 #include <linux/module.h>
9 #include <linux/of_platform.h>
10 #include <linux/pm_runtime.h>
11 #include <linux/regmap.h>
12 #include <linux/reset.h>
13 #include <sound/dmaengine_pcm.h>
14 #include <sound/pcm_iec958.h>
15 #include <sound/pcm_params.h>
16
17 #include "fsl_xcvr.h"
18 #include "fsl_utils.h"
19 #include "imx-pcm.h"
20
21 #define FSL_XCVR_CAPDS_SIZE 256
22 #define SPDIF_NUM_RATES 7
23
24 enum fsl_xcvr_pll_verison {
25 PLL_MX8MP,
26 PLL_MX95,
27 };
28
29 struct fsl_xcvr_soc_data {
30 const char *fw_name;
31 bool spdif_only;
32 bool use_edma;
33 bool use_phy;
34 enum fsl_xcvr_pll_verison pll_ver;
35 };
36
37 struct fsl_xcvr {
38 const struct fsl_xcvr_soc_data *soc_data;
39 struct platform_device *pdev;
40 struct regmap *regmap;
41 struct regmap *regmap_phy;
42 struct regmap *regmap_pll;
43 struct clk *ipg_clk;
44 struct clk *pll_ipg_clk;
45 struct clk *phy_clk;
46 struct clk *spba_clk;
47 struct clk *pll8k_clk;
48 struct clk *pll11k_clk;
49 struct reset_control *reset;
50 u8 streams;
51 u32 mode;
52 u32 arc_mode;
53 void __iomem *ram_addr;
54 struct snd_dmaengine_dai_dma_data dma_prms_rx;
55 struct snd_dmaengine_dai_dma_data dma_prms_tx;
56 struct snd_aes_iec958 rx_iec958;
57 struct snd_aes_iec958 tx_iec958;
58 u8 cap_ds[FSL_XCVR_CAPDS_SIZE];
59 struct work_struct work_rst;
60 spinlock_t lock; /* Protect hw_reset and trigger */
61 struct snd_pcm_hw_constraint_list spdif_constr_rates;
62 u32 spdif_constr_rates_list[SPDIF_NUM_RATES];
63 };
64
65 static const struct fsl_xcvr_pll_conf {
66 u8 mfi; /* min=0x18, max=0x38 */
67 u32 mfn; /* signed int, 2's compl., min=0x3FFF0000, max=0x00010000 */
68 u32 mfd; /* unsigned int */
69 u32 fout; /* Fout = Fref*(MFI + MFN/MFD), Fref is 24MHz */
70 } fsl_xcvr_pll_cfg[] = {
71 { .mfi = 54, .mfn = 1, .mfd = 6, .fout = 1300000000, }, /* 1.3 GHz */
72 { .mfi = 32, .mfn = 96, .mfd = 125, .fout = 786432000, }, /* 8000 Hz */
73 { .mfi = 30, .mfn = 66, .mfd = 625, .fout = 722534400, }, /* 11025 Hz */
74 { .mfi = 29, .mfn = 1, .mfd = 6, .fout = 700000000, }, /* 700 MHz */
75 };
76
77 /*
78 * HDMI2.1 spec defines 6- and 12-channels layout for one bit audio
79 * stream. Todo: to check how this case can be considered below
80 */
81 static const u32 fsl_xcvr_earc_channels[] = { 1, 2, 8, 16, 32, };
82 static const struct snd_pcm_hw_constraint_list fsl_xcvr_earc_channels_constr = {
83 .count = ARRAY_SIZE(fsl_xcvr_earc_channels),
84 .list = fsl_xcvr_earc_channels,
85 };
86
87 static const u32 fsl_xcvr_earc_rates[] = {
88 32000, 44100, 48000, 64000, 88200, 96000,
89 128000, 176400, 192000, 256000, 352800, 384000,
90 512000, 705600, 768000, 1024000, 1411200, 1536000,
91 };
92 static const struct snd_pcm_hw_constraint_list fsl_xcvr_earc_rates_constr = {
93 .count = ARRAY_SIZE(fsl_xcvr_earc_rates),
94 .list = fsl_xcvr_earc_rates,
95 };
96
97 static const u32 fsl_xcvr_spdif_channels[] = { 2, };
98 static const struct snd_pcm_hw_constraint_list fsl_xcvr_spdif_channels_constr = {
99 .count = ARRAY_SIZE(fsl_xcvr_spdif_channels),
100 .list = fsl_xcvr_spdif_channels,
101 };
102
103 static const u32 fsl_xcvr_spdif_rates[] = {
104 32000, 44100, 48000, 88200, 96000, 176400, 192000,
105 };
106 static const struct snd_pcm_hw_constraint_list fsl_xcvr_spdif_rates_constr = {
107 .count = ARRAY_SIZE(fsl_xcvr_spdif_rates),
108 .list = fsl_xcvr_spdif_rates,
109 };
110
111 static int fsl_xcvr_arc_mode_put(struct snd_kcontrol *kcontrol,
112 struct snd_ctl_elem_value *ucontrol)
113 {
114 struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol);
115 struct fsl_xcvr *xcvr = snd_soc_dai_get_drvdata(dai);
116 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
117 unsigned int *item = ucontrol->value.enumerated.item;
118 int val = snd_soc_enum_item_to_val(e, item[0]);
119 int ret;
120
121 if (val < 0 || val > 1)
122 return -EINVAL;
123
124 ret = (xcvr->arc_mode != val);
125
126 xcvr->arc_mode = val;
127
128 return ret;
129 }
130
131 static int fsl_xcvr_arc_mode_get(struct snd_kcontrol *kcontrol,
132 struct snd_ctl_elem_value *ucontrol)
133 {
134 struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol);
135 struct fsl_xcvr *xcvr = snd_soc_dai_get_drvdata(dai);
136
137 ucontrol->value.enumerated.item[0] = xcvr->arc_mode;
138
139 return 0;
140 }
141
142 static const u32 fsl_xcvr_phy_arc_cfg[] = {
143 FSL_XCVR_PHY_CTRL_ARC_MODE_SE_EN, FSL_XCVR_PHY_CTRL_ARC_MODE_CM_EN,
144 };
145
146 static const char * const fsl_xcvr_arc_mode[] = { "Single Ended", "Common", };
147 static const struct soc_enum fsl_xcvr_arc_mode_enum =
148 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(fsl_xcvr_arc_mode), fsl_xcvr_arc_mode);
149 static struct snd_kcontrol_new fsl_xcvr_arc_mode_kctl =
150 SOC_ENUM_EXT("ARC Mode", fsl_xcvr_arc_mode_enum,
151 fsl_xcvr_arc_mode_get, fsl_xcvr_arc_mode_put);
152
153 /* Capabilities data structure, bytes */
154 static int fsl_xcvr_type_capds_bytes_info(struct snd_kcontrol *kcontrol,
155 struct snd_ctl_elem_info *uinfo)
156 {
157 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
158 uinfo->count = FSL_XCVR_CAPDS_SIZE;
159
160 return 0;
161 }
162
163 static int fsl_xcvr_capds_get(struct snd_kcontrol *kcontrol,
164 struct snd_ctl_elem_value *ucontrol)
165 {
166 struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol);
167 struct fsl_xcvr *xcvr = snd_soc_dai_get_drvdata(dai);
168
169 memcpy(ucontrol->value.bytes.data, xcvr->cap_ds, FSL_XCVR_CAPDS_SIZE);
170
171 return 0;
172 }
173
174 static int fsl_xcvr_capds_put(struct snd_kcontrol *kcontrol,
175 struct snd_ctl_elem_value *ucontrol)
176 {
177 struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol);
178 struct fsl_xcvr *xcvr = snd_soc_dai_get_drvdata(dai);
179
180 memcpy(xcvr->cap_ds, ucontrol->value.bytes.data, FSL_XCVR_CAPDS_SIZE);
181
182 return 0;
183 }
184
185 static struct snd_kcontrol_new fsl_xcvr_earc_capds_kctl = {
186 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
187 .name = "Capabilities Data Structure",
188 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
189 .info = fsl_xcvr_type_capds_bytes_info,
190 .get = fsl_xcvr_capds_get,
191 .put = fsl_xcvr_capds_put,
192 };
193
194 static int fsl_xcvr_activate_ctl(struct snd_soc_dai *dai, const char *name,
195 bool active)
196 {
197 struct snd_soc_card *card = dai->component->card;
198 struct snd_kcontrol *kctl;
199 bool enabled;
200
201 lockdep_assert_held(&card->snd_card->controls_rwsem);
202
203 kctl = snd_soc_card_get_kcontrol(card, name);
204 if (kctl == NULL)
205 return -ENOENT;
206
207 enabled = ((kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_WRITE) != 0);
208 if (active == enabled)
209 return 0; /* nothing to do */
210
211 if (active)
212 kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
213 else
214 kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_WRITE;
215
216 snd_ctl_notify(card->snd_card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
217
218 return 1;
219 }
220
221 static int fsl_xcvr_mode_put(struct snd_kcontrol *kcontrol,
222 struct snd_ctl_elem_value *ucontrol)
223 {
224 struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol);
225 struct fsl_xcvr *xcvr = snd_soc_dai_get_drvdata(dai);
226 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
227 unsigned int *item = ucontrol->value.enumerated.item;
228 int val = snd_soc_enum_item_to_val(e, item[0]);
229 struct snd_soc_card *card = dai->component->card;
230 struct snd_soc_pcm_runtime *rtd;
231 int ret;
232
233 if (val < FSL_XCVR_MODE_SPDIF || val > FSL_XCVR_MODE_EARC)
234 return -EINVAL;
235
236 ret = (xcvr->mode != val);
237
238 xcvr->mode = val;
239
240 fsl_xcvr_activate_ctl(dai, fsl_xcvr_arc_mode_kctl.name,
241 (xcvr->mode == FSL_XCVR_MODE_ARC));
242 fsl_xcvr_activate_ctl(dai, fsl_xcvr_earc_capds_kctl.name,
243 (xcvr->mode == FSL_XCVR_MODE_EARC));
244 /* Allow playback for SPDIF only */
245 rtd = snd_soc_get_pcm_runtime(card, card->dai_link);
246 rtd->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count =
247 (xcvr->mode == FSL_XCVR_MODE_SPDIF ? 1 : 0);
248 return ret;
249 }
250
251 static int fsl_xcvr_mode_get(struct snd_kcontrol *kcontrol,
252 struct snd_ctl_elem_value *ucontrol)
253 {
254 struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol);
255 struct fsl_xcvr *xcvr = snd_soc_dai_get_drvdata(dai);
256
257 ucontrol->value.enumerated.item[0] = xcvr->mode;
258
259 return 0;
260 }
261
262 static const char * const fsl_xcvr_mode[] = { "SPDIF", "ARC RX", "eARC", };
263 static const struct soc_enum fsl_xcvr_mode_enum =
264 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(fsl_xcvr_mode), fsl_xcvr_mode);
265 static struct snd_kcontrol_new fsl_xcvr_mode_kctl =
266 SOC_ENUM_EXT("XCVR Mode", fsl_xcvr_mode_enum,
267 fsl_xcvr_mode_get, fsl_xcvr_mode_put);
268
269 /** phy: true => phy, false => pll */
270 static int fsl_xcvr_ai_write(struct fsl_xcvr *xcvr, u8 reg, u32 data, bool phy)
271 {
272 struct device *dev = &xcvr->pdev->dev;
273 u32 val, idx, tidx;
274 int ret;
275
276 idx = BIT(phy ? 26 : 24);
277 tidx = BIT(phy ? 27 : 25);
278
279 regmap_write(xcvr->regmap, FSL_XCVR_PHY_AI_CTRL_CLR, 0xFF | FSL_XCVR_PHY_AI_CTRL_AI_RWB);
280 regmap_write(xcvr->regmap, FSL_XCVR_PHY_AI_CTRL_SET, reg);
281 regmap_write(xcvr->regmap, FSL_XCVR_PHY_AI_WDATA, data);
282 regmap_write(xcvr->regmap, FSL_XCVR_PHY_AI_CTRL_TOG, idx);
283
284 ret = regmap_read_poll_timeout(xcvr->regmap, FSL_XCVR_PHY_AI_CTRL, val,
285 (val & idx) == ((val & tidx) >> 1),
286 10, 10000);
287 if (ret)
288 dev_err(dev, "AI timeout: failed to set %s reg 0x%02x=0x%08x\n",
289 phy ? "PHY" : "PLL", reg, data);
290 return ret;
291 }
292
293 static int fsl_xcvr_ai_read(struct fsl_xcvr *xcvr, u8 reg, u32 *data, bool phy)
294 {
295 struct device *dev = &xcvr->pdev->dev;
296 u32 val, idx, tidx;
297 int ret;
298
299 idx = BIT(phy ? 26 : 24);
300 tidx = BIT(phy ? 27 : 25);
301
302 regmap_write(xcvr->regmap, FSL_XCVR_PHY_AI_CTRL_CLR, 0xFF | FSL_XCVR_PHY_AI_CTRL_AI_RWB);
303 regmap_write(xcvr->regmap, FSL_XCVR_PHY_AI_CTRL_SET, reg | FSL_XCVR_PHY_AI_CTRL_AI_RWB);
304 regmap_write(xcvr->regmap, FSL_XCVR_PHY_AI_CTRL_TOG, idx);
305
306 ret = regmap_read_poll_timeout(xcvr->regmap, FSL_XCVR_PHY_AI_CTRL, val,
307 (val & idx) == ((val & tidx) >> 1),
308 10, 10000);
309 if (ret)
310 dev_err(dev, "AI timeout: failed to read %s reg 0x%02x\n",
311 phy ? "PHY" : "PLL", reg);
312
313 regmap_read(xcvr->regmap, FSL_XCVR_PHY_AI_RDATA, data);
314
315 return ret;
316 }
317
318 static int fsl_xcvr_phy_reg_read(void *context, unsigned int reg, unsigned int *val)
319 {
320 struct fsl_xcvr *xcvr = context;
321
322 return fsl_xcvr_ai_read(xcvr, reg, val, 1);
323 }
324
325 static int fsl_xcvr_phy_reg_write(void *context, unsigned int reg, unsigned int val)
326 {
327 struct fsl_xcvr *xcvr = context;
328
329 return fsl_xcvr_ai_write(xcvr, reg, val, 1);
330 }
331
332 static int fsl_xcvr_pll_reg_read(void *context, unsigned int reg, unsigned int *val)
333 {
334 struct fsl_xcvr *xcvr = context;
335
336 return fsl_xcvr_ai_read(xcvr, reg, val, 0);
337 }
338
339 static int fsl_xcvr_pll_reg_write(void *context, unsigned int reg, unsigned int val)
340 {
341 struct fsl_xcvr *xcvr = context;
342
343 return fsl_xcvr_ai_write(xcvr, reg, val, 0);
344 }
345
346 static int fsl_xcvr_en_phy_pll(struct fsl_xcvr *xcvr, u32 freq, bool tx)
347 {
348 struct device *dev = &xcvr->pdev->dev;
349 u32 i, div = 0, log2, val;
350 int ret;
351
352 if (!xcvr->soc_data->use_phy)
353 return 0;
354
355 for (i = 0; i < ARRAY_SIZE(fsl_xcvr_pll_cfg); i++) {
356 if (fsl_xcvr_pll_cfg[i].fout % freq == 0) {
357 div = fsl_xcvr_pll_cfg[i].fout / freq;
358 break;
359 }
360 }
361
362 if (!div || i >= ARRAY_SIZE(fsl_xcvr_pll_cfg))
363 return -EINVAL;
364
365 log2 = ilog2(div);
366
367 /* Release AI interface from reset */
368 ret = regmap_write(xcvr->regmap, FSL_XCVR_PHY_AI_CTRL_SET,
369 FSL_XCVR_PHY_AI_CTRL_AI_RESETN);
370 if (ret < 0) {
371 dev_err(dev, "Error while setting IER0: %d\n", ret);
372 return ret;
373 }
374
375 switch (xcvr->soc_data->pll_ver) {
376 case PLL_MX8MP:
377 /* PLL: BANDGAP_SET: EN_VBG (enable bandgap) */
378 regmap_set_bits(xcvr->regmap_pll, FSL_XCVR_PLL_BANDGAP,
379 FSL_XCVR_PLL_BANDGAP_EN_VBG);
380
381 /* PLL: CTRL0: DIV_INTEGER */
382 regmap_write(xcvr->regmap_pll, FSL_XCVR_PLL_CTRL0, fsl_xcvr_pll_cfg[i].mfi);
383 /* PLL: NUMERATOR: MFN */
384 regmap_write(xcvr->regmap_pll, FSL_XCVR_PLL_NUM, fsl_xcvr_pll_cfg[i].mfn);
385 /* PLL: DENOMINATOR: MFD */
386 regmap_write(xcvr->regmap_pll, FSL_XCVR_PLL_DEN, fsl_xcvr_pll_cfg[i].mfd);
387 /* PLL: CTRL0_SET: HOLD_RING_OFF, POWER_UP */
388 regmap_set_bits(xcvr->regmap_pll, FSL_XCVR_PLL_CTRL0,
389 FSL_XCVR_PLL_CTRL0_HROFF | FSL_XCVR_PLL_CTRL0_PWP);
390 udelay(25);
391 /* PLL: CTRL0: Clear Hold Ring Off */
392 regmap_clear_bits(xcvr->regmap_pll, FSL_XCVR_PLL_CTRL0,
393 FSL_XCVR_PLL_CTRL0_HROFF);
394 udelay(100);
395 if (tx) { /* TX is enabled for SPDIF only */
396 /* PLL: POSTDIV: PDIV0 */
397 regmap_write(xcvr->regmap_pll, FSL_XCVR_PLL_PDIV,
398 FSL_XCVR_PLL_PDIVx(log2, 0));
399 /* PLL: CTRL_SET: CLKMUX0_EN */
400 regmap_set_bits(xcvr->regmap_pll, FSL_XCVR_PLL_CTRL0,
401 FSL_XCVR_PLL_CTRL0_CM0_EN);
402 } else if (xcvr->mode == FSL_XCVR_MODE_EARC) { /* eARC RX */
403 /* PLL: POSTDIV: PDIV1 */
404 regmap_write(xcvr->regmap_pll, FSL_XCVR_PLL_PDIV,
405 FSL_XCVR_PLL_PDIVx(log2, 1));
406 /* PLL: CTRL_SET: CLKMUX1_EN */
407 regmap_set_bits(xcvr->regmap_pll, FSL_XCVR_PLL_CTRL0,
408 FSL_XCVR_PLL_CTRL0_CM1_EN);
409 } else { /* SPDIF / ARC RX */
410 /* PLL: POSTDIV: PDIV2 */
411 regmap_write(xcvr->regmap_pll, FSL_XCVR_PLL_PDIV,
412 FSL_XCVR_PLL_PDIVx(log2, 2));
413 /* PLL: CTRL_SET: CLKMUX2_EN */
414 regmap_set_bits(xcvr->regmap_pll, FSL_XCVR_PLL_CTRL0,
415 FSL_XCVR_PLL_CTRL0_CM2_EN);
416 }
417 break;
418 case PLL_MX95:
419 val = fsl_xcvr_pll_cfg[i].mfi << FSL_XCVR_GP_PLL_DIV_MFI_SHIFT | div;
420 regmap_write(xcvr->regmap_pll, FSL_XCVR_GP_PLL_DIV, val);
421 val = fsl_xcvr_pll_cfg[i].mfn << FSL_XCVR_GP_PLL_NUMERATOR_MFN_SHIFT;
422 regmap_write(xcvr->regmap_pll, FSL_XCVR_GP_PLL_NUMERATOR, val);
423 regmap_write(xcvr->regmap_pll, FSL_XCVR_GP_PLL_DENOMINATOR,
424 fsl_xcvr_pll_cfg[i].mfd);
425 val = FSL_XCVR_GP_PLL_CTRL_POWERUP | FSL_XCVR_GP_PLL_CTRL_CLKMUX_EN;
426 regmap_write(xcvr->regmap_pll, FSL_XCVR_GP_PLL_CTRL, val);
427 break;
428 default:
429 dev_err(dev, "Error for PLL version %d\n", xcvr->soc_data->pll_ver);
430 return -EINVAL;
431 }
432
433 if (xcvr->mode == FSL_XCVR_MODE_EARC) { /* eARC mode */
434 /* PHY: CTRL_SET: TX_DIFF_OE, PHY_EN */
435 regmap_set_bits(xcvr->regmap_phy, FSL_XCVR_PHY_CTRL,
436 FSL_XCVR_PHY_CTRL_TSDIFF_OE |
437 FSL_XCVR_PHY_CTRL_PHY_EN);
438 /* PHY: CTRL2_SET: EARC_TX_MODE */
439 regmap_set_bits(xcvr->regmap_phy, FSL_XCVR_PHY_CTRL2,
440 FSL_XCVR_PHY_CTRL2_EARC_TXMS);
441 } else if (!tx) { /* SPDIF / ARC RX mode */
442 if (xcvr->mode == FSL_XCVR_MODE_SPDIF)
443 /* PHY: CTRL_SET: SPDIF_EN */
444 regmap_set_bits(xcvr->regmap_phy, FSL_XCVR_PHY_CTRL,
445 FSL_XCVR_PHY_CTRL_SPDIF_EN);
446 else /* PHY: CTRL_SET: ARC RX setup */
447 regmap_set_bits(xcvr->regmap_phy, FSL_XCVR_PHY_CTRL,
448 FSL_XCVR_PHY_CTRL_PHY_EN |
449 FSL_XCVR_PHY_CTRL_RX_CM_EN |
450 fsl_xcvr_phy_arc_cfg[xcvr->arc_mode]);
451 }
452
453 dev_dbg(dev, "PLL Fexp: %u, Fout: %u, mfi: %u, mfn: %u, mfd: %d, div: %u, pdiv0: %u\n",
454 freq, fsl_xcvr_pll_cfg[i].fout, fsl_xcvr_pll_cfg[i].mfi,
455 fsl_xcvr_pll_cfg[i].mfn, fsl_xcvr_pll_cfg[i].mfd, div, log2);
456 return 0;
457 }
458
459 static int fsl_xcvr_en_aud_pll(struct fsl_xcvr *xcvr, u32 freq)
460 {
461 struct device *dev = &xcvr->pdev->dev;
462 int ret;
463
464 freq = xcvr->soc_data->spdif_only ? freq / 5 : freq;
465 clk_disable_unprepare(xcvr->phy_clk);
466 fsl_asoc_reparent_pll_clocks(dev, xcvr->phy_clk,
467 xcvr->pll8k_clk, xcvr->pll11k_clk, freq);
468 ret = clk_set_rate(xcvr->phy_clk, freq);
469 if (ret < 0) {
470 dev_err(dev, "Error while setting AUD PLL rate: %d\n", ret);
471 return ret;
472 }
473 ret = clk_prepare_enable(xcvr->phy_clk);
474 if (ret) {
475 dev_err(dev, "failed to start PHY clock: %d\n", ret);
476 return ret;
477 }
478
479 if (!xcvr->soc_data->use_phy)
480 return 0;
481 /* Release AI interface from reset */
482 ret = regmap_write(xcvr->regmap, FSL_XCVR_PHY_AI_CTRL_SET,
483 FSL_XCVR_PHY_AI_CTRL_AI_RESETN);
484 if (ret < 0) {
485 dev_err(dev, "Error while setting IER0: %d\n", ret);
486 return ret;
487 }
488
489 if (xcvr->mode == FSL_XCVR_MODE_EARC) { /* eARC mode */
490 /* PHY: CTRL_SET: TX_DIFF_OE, PHY_EN */
491 regmap_set_bits(xcvr->regmap_phy, FSL_XCVR_PHY_CTRL,
492 FSL_XCVR_PHY_CTRL_TSDIFF_OE |
493 FSL_XCVR_PHY_CTRL_PHY_EN);
494 /* PHY: CTRL2_SET: EARC_TX_MODE */
495 regmap_set_bits(xcvr->regmap_phy, FSL_XCVR_PHY_CTRL2,
496 FSL_XCVR_PHY_CTRL2_EARC_TXMS);
497 } else { /* SPDIF mode */
498 /* PHY: CTRL_SET: TX_CLK_AUD_SS | SPDIF_EN */
499 regmap_set_bits(xcvr->regmap_phy, FSL_XCVR_PHY_CTRL,
500 FSL_XCVR_PHY_CTRL_TX_CLK_AUD_SS |
501 FSL_XCVR_PHY_CTRL_SPDIF_EN);
502 }
503
504 dev_dbg(dev, "PLL Fexp: %u\n", freq);
505
506 return 0;
507 }
508
509 #define FSL_XCVR_SPDIF_RX_FREQ 175000000
510 static int fsl_xcvr_prepare(struct snd_pcm_substream *substream,
511 struct snd_soc_dai *dai)
512 {
513 struct fsl_xcvr *xcvr = snd_soc_dai_get_drvdata(dai);
514 bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
515 u32 m_ctl = 0, v_ctl = 0;
516 u32 r = substream->runtime->rate, ch = substream->runtime->channels;
517 u32 fout = 32 * r * ch * 10;
518 int ret = 0;
519
520 switch (xcvr->mode) {
521 case FSL_XCVR_MODE_SPDIF:
522 if (xcvr->soc_data->spdif_only && tx) {
523 ret = regmap_update_bits(xcvr->regmap, FSL_XCVR_TX_DPTH_CTRL,
524 FSL_XCVR_TX_DPTH_CTRL_BYPASS_FEM,
525 FSL_XCVR_TX_DPTH_CTRL_BYPASS_FEM);
526 if (ret < 0) {
527 dev_err(dai->dev, "Failed to set bypass fem: %d\n", ret);
528 return ret;
529 }
530 }
531 fallthrough;
532 case FSL_XCVR_MODE_ARC:
533 if (tx) {
534 ret = fsl_xcvr_en_aud_pll(xcvr, fout);
535 if (ret < 0) {
536 dev_err(dai->dev, "Failed to set TX freq %u: %d\n",
537 fout, ret);
538 return ret;
539 }
540
541 ret = regmap_set_bits(xcvr->regmap, FSL_XCVR_TX_DPTH_CTRL,
542 FSL_XCVR_TX_DPTH_CTRL_FRM_FMT);
543 if (ret < 0) {
544 dev_err(dai->dev, "Failed to set TX_DPTH: %d\n", ret);
545 return ret;
546 }
547
548 /**
549 * set SPDIF MODE - this flag is used to gate
550 * SPDIF output, useless for SPDIF RX
551 */
552 m_ctl |= FSL_XCVR_EXT_CTRL_SPDIF_MODE;
553 v_ctl |= FSL_XCVR_EXT_CTRL_SPDIF_MODE;
554 } else {
555 /**
556 * Clear RX FIFO, flip RX FIFO bits,
557 * disable eARC related HW mode detects
558 */
559 ret = regmap_set_bits(xcvr->regmap, FSL_XCVR_RX_DPTH_CTRL,
560 FSL_XCVR_RX_DPTH_CTRL_STORE_FMT |
561 FSL_XCVR_RX_DPTH_CTRL_CLR_RX_FIFO |
562 FSL_XCVR_RX_DPTH_CTRL_COMP |
563 FSL_XCVR_RX_DPTH_CTRL_LAYB_CTRL);
564 if (ret < 0) {
565 dev_err(dai->dev, "Failed to set RX_DPTH: %d\n", ret);
566 return ret;
567 }
568
569 ret = fsl_xcvr_en_phy_pll(xcvr, FSL_XCVR_SPDIF_RX_FREQ, tx);
570 if (ret < 0) {
571 dev_err(dai->dev, "Failed to set RX freq %u: %d\n",
572 FSL_XCVR_SPDIF_RX_FREQ, ret);
573 return ret;
574 }
575 }
576 break;
577 case FSL_XCVR_MODE_EARC:
578 if (!tx) {
579 /** Clear RX FIFO, flip RX FIFO bits */
580 ret = regmap_set_bits(xcvr->regmap, FSL_XCVR_RX_DPTH_CTRL,
581 FSL_XCVR_RX_DPTH_CTRL_STORE_FMT |
582 FSL_XCVR_RX_DPTH_CTRL_CLR_RX_FIFO);
583 if (ret < 0) {
584 dev_err(dai->dev, "Failed to set RX_DPTH: %d\n", ret);
585 return ret;
586 }
587
588 /** Enable eARC related HW mode detects */
589 ret = regmap_clear_bits(xcvr->regmap, FSL_XCVR_RX_DPTH_CTRL,
590 FSL_XCVR_RX_DPTH_CTRL_COMP |
591 FSL_XCVR_RX_DPTH_CTRL_LAYB_CTRL);
592 if (ret < 0) {
593 dev_err(dai->dev, "Failed to clr TX_DPTH: %d\n", ret);
594 return ret;
595 }
596 }
597
598 /* clear CMDC RESET */
599 m_ctl |= FSL_XCVR_EXT_CTRL_CMDC_RESET(tx);
600 /* set TX_RX_MODE */
601 m_ctl |= FSL_XCVR_EXT_CTRL_TX_RX_MODE;
602 v_ctl |= (tx ? FSL_XCVR_EXT_CTRL_TX_RX_MODE : 0);
603 break;
604 }
605
606 ret = regmap_update_bits(xcvr->regmap, FSL_XCVR_EXT_CTRL, m_ctl, v_ctl);
607 if (ret < 0) {
608 dev_err(dai->dev, "Error while setting EXT_CTRL: %d\n", ret);
609 return ret;
610 }
611
612 return 0;
613 }
614
615 static int fsl_xcvr_constr(const struct snd_pcm_substream *substream,
616 const struct snd_pcm_hw_constraint_list *channels,
617 const struct snd_pcm_hw_constraint_list *rates)
618 {
619 struct snd_pcm_runtime *rt = substream->runtime;
620 int ret;
621
622 ret = snd_pcm_hw_constraint_list(rt, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
623 channels);
624 if (ret < 0)
625 return ret;
626
627 ret = snd_pcm_hw_constraint_list(rt, 0, SNDRV_PCM_HW_PARAM_RATE,
628 rates);
629 if (ret < 0)
630 return ret;
631
632 return 0;
633 }
634
635 static int fsl_xcvr_startup(struct snd_pcm_substream *substream,
636 struct snd_soc_dai *dai)
637 {
638 struct fsl_xcvr *xcvr = snd_soc_dai_get_drvdata(dai);
639 bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
640 int ret = 0;
641
642 if (xcvr->streams & BIT(substream->stream)) {
643 dev_err(dai->dev, "%sX busy\n", tx ? "T" : "R");
644 return -EBUSY;
645 }
646
647 /*
648 * EDMA controller needs period size to be a multiple of
649 * tx/rx maxburst
650 */
651 if (xcvr->soc_data->use_edma)
652 snd_pcm_hw_constraint_step(substream->runtime, 0,
653 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
654 tx ? xcvr->dma_prms_tx.maxburst :
655 xcvr->dma_prms_rx.maxburst);
656
657 switch (xcvr->mode) {
658 case FSL_XCVR_MODE_SPDIF:
659 case FSL_XCVR_MODE_ARC:
660 if (xcvr->soc_data->spdif_only && tx)
661 ret = fsl_xcvr_constr(substream, &fsl_xcvr_spdif_channels_constr,
662 &xcvr->spdif_constr_rates);
663 else
664 ret = fsl_xcvr_constr(substream, &fsl_xcvr_spdif_channels_constr,
665 &fsl_xcvr_spdif_rates_constr);
666 break;
667 case FSL_XCVR_MODE_EARC:
668 ret = fsl_xcvr_constr(substream, &fsl_xcvr_earc_channels_constr,
669 &fsl_xcvr_earc_rates_constr);
670 break;
671 }
672 if (ret < 0)
673 return ret;
674
675 xcvr->streams |= BIT(substream->stream);
676
677 if (!xcvr->soc_data->spdif_only) {
678 struct snd_soc_card *card = dai->component->card;
679
680 /* Disable XCVR controls if there is stream started */
681 down_read(&card->snd_card->controls_rwsem);
682 fsl_xcvr_activate_ctl(dai, fsl_xcvr_mode_kctl.name, false);
683 fsl_xcvr_activate_ctl(dai, fsl_xcvr_arc_mode_kctl.name, false);
684 fsl_xcvr_activate_ctl(dai, fsl_xcvr_earc_capds_kctl.name, false);
685 up_read(&card->snd_card->controls_rwsem);
686 }
687
688 return 0;
689 }
690
691 static void fsl_xcvr_shutdown(struct snd_pcm_substream *substream,
692 struct snd_soc_dai *dai)
693 {
694 struct fsl_xcvr *xcvr = snd_soc_dai_get_drvdata(dai);
695 bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
696 u32 mask = 0, val = 0;
697 int ret;
698
699 xcvr->streams &= ~BIT(substream->stream);
700
701 /* Enable XCVR controls if there is no stream started */
702 if (!xcvr->streams) {
703 if (!xcvr->soc_data->spdif_only) {
704 struct snd_soc_card *card = dai->component->card;
705
706 down_read(&card->snd_card->controls_rwsem);
707 fsl_xcvr_activate_ctl(dai, fsl_xcvr_mode_kctl.name, true);
708 fsl_xcvr_activate_ctl(dai, fsl_xcvr_arc_mode_kctl.name,
709 (xcvr->mode == FSL_XCVR_MODE_ARC));
710 fsl_xcvr_activate_ctl(dai, fsl_xcvr_earc_capds_kctl.name,
711 (xcvr->mode == FSL_XCVR_MODE_EARC));
712 up_read(&card->snd_card->controls_rwsem);
713 }
714 ret = regmap_update_bits(xcvr->regmap, FSL_XCVR_EXT_IER0,
715 FSL_XCVR_IRQ_EARC_ALL, 0);
716 if (ret < 0) {
717 dev_err(dai->dev, "Failed to set IER0: %d\n", ret);
718 return;
719 }
720
721 /* clear SPDIF MODE */
722 if (xcvr->mode == FSL_XCVR_MODE_SPDIF)
723 mask |= FSL_XCVR_EXT_CTRL_SPDIF_MODE;
724 }
725
726 if (xcvr->mode == FSL_XCVR_MODE_EARC) {
727 /* set CMDC RESET */
728 mask |= FSL_XCVR_EXT_CTRL_CMDC_RESET(tx);
729 val |= FSL_XCVR_EXT_CTRL_CMDC_RESET(tx);
730 }
731
732 ret = regmap_update_bits(xcvr->regmap, FSL_XCVR_EXT_CTRL, mask, val);
733 if (ret < 0) {
734 dev_err(dai->dev, "Err setting DPATH RESET: %d\n", ret);
735 return;
736 }
737 }
738
739 static int fsl_xcvr_trigger(struct snd_pcm_substream *substream, int cmd,
740 struct snd_soc_dai *dai)
741 {
742 struct fsl_xcvr *xcvr = snd_soc_dai_get_drvdata(dai);
743 bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
744 unsigned long lock_flags;
745 int ret = 0;
746
747 spin_lock_irqsave(&xcvr->lock, lock_flags);
748
749 switch (cmd) {
750 case SNDRV_PCM_TRIGGER_START:
751 case SNDRV_PCM_TRIGGER_RESUME:
752 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
753 /* set DPATH RESET */
754 ret = regmap_update_bits(xcvr->regmap, FSL_XCVR_EXT_CTRL,
755 FSL_XCVR_EXT_CTRL_DPTH_RESET(tx),
756 FSL_XCVR_EXT_CTRL_DPTH_RESET(tx));
757 if (ret < 0) {
758 dev_err(dai->dev, "Failed to set DPATH RESET: %d\n", ret);
759 goto release_lock;
760 }
761
762 if (tx) {
763 switch (xcvr->mode) {
764 case FSL_XCVR_MODE_EARC:
765 /* set isr_cmdc_tx_en, w1c */
766 ret = regmap_write(xcvr->regmap,
767 FSL_XCVR_ISR_SET,
768 FSL_XCVR_ISR_CMDC_TX_EN);
769 if (ret < 0) {
770 dev_err(dai->dev, "err updating isr %d\n", ret);
771 goto release_lock;
772 }
773 fallthrough;
774 case FSL_XCVR_MODE_SPDIF:
775 ret = regmap_set_bits(xcvr->regmap,
776 FSL_XCVR_TX_DPTH_CTRL,
777 FSL_XCVR_TX_DPTH_CTRL_STRT_DATA_TX);
778 if (ret < 0) {
779 dev_err(dai->dev, "Failed to start DATA_TX: %d\n", ret);
780 goto release_lock;
781 }
782 break;
783 }
784 }
785
786 /* enable DMA RD/WR */
787 ret = regmap_update_bits(xcvr->regmap, FSL_XCVR_EXT_CTRL,
788 FSL_XCVR_EXT_CTRL_DMA_DIS(tx), 0);
789 if (ret < 0) {
790 dev_err(dai->dev, "Failed to enable DMA: %d\n", ret);
791 goto release_lock;
792 }
793
794 ret = regmap_update_bits(xcvr->regmap, FSL_XCVR_EXT_IER0,
795 FSL_XCVR_IRQ_EARC_ALL, FSL_XCVR_IRQ_EARC_ALL);
796 if (ret < 0) {
797 dev_err(dai->dev, "Error while setting IER0: %d\n", ret);
798 goto release_lock;
799 }
800
801 /* clear DPATH RESET */
802 ret = regmap_update_bits(xcvr->regmap, FSL_XCVR_EXT_CTRL,
803 FSL_XCVR_EXT_CTRL_DPTH_RESET(tx),
804 0);
805 if (ret < 0) {
806 dev_err(dai->dev, "Failed to clear DPATH RESET: %d\n", ret);
807 goto release_lock;
808 }
809
810 break;
811 case SNDRV_PCM_TRIGGER_STOP:
812 case SNDRV_PCM_TRIGGER_SUSPEND:
813 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
814 /* disable DMA RD/WR */
815 ret = regmap_update_bits(xcvr->regmap, FSL_XCVR_EXT_CTRL,
816 FSL_XCVR_EXT_CTRL_DMA_DIS(tx),
817 FSL_XCVR_EXT_CTRL_DMA_DIS(tx));
818 if (ret < 0) {
819 dev_err(dai->dev, "Failed to disable DMA: %d\n", ret);
820 goto release_lock;
821 }
822
823 ret = regmap_update_bits(xcvr->regmap, FSL_XCVR_EXT_IER0,
824 FSL_XCVR_IRQ_EARC_ALL, 0);
825 if (ret < 0) {
826 dev_err(dai->dev, "Failed to clear IER0: %d\n", ret);
827 goto release_lock;
828 }
829
830 if (tx) {
831 switch (xcvr->mode) {
832 case FSL_XCVR_MODE_SPDIF:
833 ret = regmap_clear_bits(xcvr->regmap,
834 FSL_XCVR_TX_DPTH_CTRL,
835 FSL_XCVR_TX_DPTH_CTRL_STRT_DATA_TX);
836 if (ret < 0) {
837 dev_err(dai->dev, "Failed to stop DATA_TX: %d\n", ret);
838 goto release_lock;
839 }
840 if (xcvr->soc_data->spdif_only)
841 break;
842 else
843 fallthrough;
844 case FSL_XCVR_MODE_EARC:
845 /* clear ISR_CMDC_TX_EN, W1C */
846 ret = regmap_write(xcvr->regmap,
847 FSL_XCVR_ISR_CLR,
848 FSL_XCVR_ISR_CMDC_TX_EN);
849 if (ret < 0) {
850 dev_err(dai->dev,
851 "Err updating ISR %d\n", ret);
852 goto release_lock;
853 }
854 break;
855 }
856 }
857 break;
858 default:
859 ret = -EINVAL;
860 break;
861 }
862
863 release_lock:
864 spin_unlock_irqrestore(&xcvr->lock, lock_flags);
865 return ret;
866 }
867
868 static int fsl_xcvr_load_firmware(struct fsl_xcvr *xcvr)
869 {
870 struct device *dev = &xcvr->pdev->dev;
871 const struct firmware *fw;
872 int ret = 0, rem, off, out, page = 0, size = FSL_XCVR_REG_OFFSET;
873 u32 mask, val;
874
875 ret = request_firmware(&fw, xcvr->soc_data->fw_name, dev);
876 if (ret) {
877 dev_err(dev, "failed to request firmware.\n");
878 return ret;
879 }
880
881 rem = fw->size;
882
883 /* RAM is 20KiB = 16KiB code + 4KiB data => max 10 pages 2KiB each */
884 if (rem > 16384) {
885 dev_err(dev, "FW size %d is bigger than 16KiB.\n", rem);
886 release_firmware(fw);
887 return -ENOMEM;
888 }
889
890 for (page = 0; page < 10; page++) {
891 ret = regmap_update_bits(xcvr->regmap, FSL_XCVR_EXT_CTRL,
892 FSL_XCVR_EXT_CTRL_PAGE_MASK,
893 FSL_XCVR_EXT_CTRL_PAGE(page));
894 if (ret < 0) {
895 dev_err(dev, "FW: failed to set page %d, err=%d\n",
896 page, ret);
897 goto err_firmware;
898 }
899
900 off = page * size;
901 out = min(rem, size);
902 /* IPG clock is assumed to be running, otherwise it will hang */
903 if (out > 0) {
904 /* write firmware into code memory */
905 memcpy_toio(xcvr->ram_addr, fw->data + off, out);
906 rem -= out;
907 if (rem == 0) {
908 /* last part of firmware written */
909 /* clean remaining part of code memory page */
910 memset_io(xcvr->ram_addr + out, 0, size - out);
911 }
912 } else {
913 /* clean current page, including data memory */
914 memset_io(xcvr->ram_addr, 0, size);
915 }
916 }
917
918 err_firmware:
919 release_firmware(fw);
920 if (ret < 0)
921 return ret;
922
923 /* configure watermarks */
924 mask = FSL_XCVR_EXT_CTRL_RX_FWM_MASK | FSL_XCVR_EXT_CTRL_TX_FWM_MASK;
925 val = FSL_XCVR_EXT_CTRL_RX_FWM(FSL_XCVR_FIFO_WMK_RX);
926 val |= FSL_XCVR_EXT_CTRL_TX_FWM(FSL_XCVR_FIFO_WMK_TX);
927 /* disable DMA RD/WR */
928 mask |= FSL_XCVR_EXT_CTRL_DMA_RD_DIS | FSL_XCVR_EXT_CTRL_DMA_WR_DIS;
929 val |= FSL_XCVR_EXT_CTRL_DMA_RD_DIS | FSL_XCVR_EXT_CTRL_DMA_WR_DIS;
930 /* Data RAM is 4KiB, last two pages: 8 and 9. Select page 8. */
931 mask |= FSL_XCVR_EXT_CTRL_PAGE_MASK;
932 val |= FSL_XCVR_EXT_CTRL_PAGE(8);
933
934 ret = regmap_update_bits(xcvr->regmap, FSL_XCVR_EXT_CTRL, mask, val);
935 if (ret < 0) {
936 dev_err(dev, "Failed to set watermarks: %d\n", ret);
937 return ret;
938 }
939
940 /* Store Capabilities Data Structure into Data RAM */
941 memcpy_toio(xcvr->ram_addr + FSL_XCVR_CAP_DATA_STR, xcvr->cap_ds,
942 FSL_XCVR_CAPDS_SIZE);
943 return 0;
944 }
945
946 static int fsl_xcvr_type_iec958_info(struct snd_kcontrol *kcontrol,
947 struct snd_ctl_elem_info *uinfo)
948 {
949 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
950 uinfo->count = 1;
951
952 return 0;
953 }
954
955 static int fsl_xcvr_type_iec958_bytes_info(struct snd_kcontrol *kcontrol,
956 struct snd_ctl_elem_info *uinfo)
957 {
958 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
959 uinfo->count = sizeof_field(struct snd_aes_iec958, status);
960
961 return 0;
962 }
963
964 static int fsl_xcvr_rx_cs_get(struct snd_kcontrol *kcontrol,
965 struct snd_ctl_elem_value *ucontrol)
966 {
967 struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol);
968 struct fsl_xcvr *xcvr = snd_soc_dai_get_drvdata(dai);
969
970 memcpy(ucontrol->value.iec958.status, xcvr->rx_iec958.status, 24);
971
972 return 0;
973 }
974
975 static int fsl_xcvr_tx_cs_get(struct snd_kcontrol *kcontrol,
976 struct snd_ctl_elem_value *ucontrol)
977 {
978 struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol);
979 struct fsl_xcvr *xcvr = snd_soc_dai_get_drvdata(dai);
980
981 memcpy(ucontrol->value.iec958.status, xcvr->tx_iec958.status, 24);
982
983 return 0;
984 }
985
986 static int fsl_xcvr_tx_cs_put(struct snd_kcontrol *kcontrol,
987 struct snd_ctl_elem_value *ucontrol)
988 {
989 struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol);
990 struct fsl_xcvr *xcvr = snd_soc_dai_get_drvdata(dai);
991
992 memcpy(xcvr->tx_iec958.status, ucontrol->value.iec958.status, 24);
993
994 return 0;
995 }
996
997 static struct snd_kcontrol_new fsl_xcvr_rx_ctls[] = {
998 /* Channel status controller */
999 {
1000 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1001 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
1002 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1003 .info = fsl_xcvr_type_iec958_info,
1004 .get = fsl_xcvr_rx_cs_get,
1005 },
1006 /* Capture channel status, bytes */
1007 {
1008 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1009 .name = "Capture Channel Status",
1010 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1011 .info = fsl_xcvr_type_iec958_bytes_info,
1012 .get = fsl_xcvr_rx_cs_get,
1013 },
1014 };
1015
1016 static struct snd_kcontrol_new fsl_xcvr_tx_ctls[] = {
1017 /* Channel status controller */
1018 {
1019 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1020 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
1021 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1022 .info = fsl_xcvr_type_iec958_info,
1023 .get = fsl_xcvr_tx_cs_get,
1024 .put = fsl_xcvr_tx_cs_put,
1025 },
1026 /* Playback channel status, bytes */
1027 {
1028 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1029 .name = "Playback Channel Status",
1030 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1031 .info = fsl_xcvr_type_iec958_bytes_info,
1032 .get = fsl_xcvr_tx_cs_get,
1033 .put = fsl_xcvr_tx_cs_put,
1034 },
1035 };
1036
1037 static int fsl_xcvr_dai_probe(struct snd_soc_dai *dai)
1038 {
1039 struct fsl_xcvr *xcvr = snd_soc_dai_get_drvdata(dai);
1040
1041 snd_soc_dai_init_dma_data(dai, &xcvr->dma_prms_tx, &xcvr->dma_prms_rx);
1042
1043 if (xcvr->soc_data->spdif_only)
1044 xcvr->mode = FSL_XCVR_MODE_SPDIF;
1045 else {
1046 snd_soc_add_dai_controls(dai, &fsl_xcvr_mode_kctl, 1);
1047 snd_soc_add_dai_controls(dai, &fsl_xcvr_arc_mode_kctl, 1);
1048 snd_soc_add_dai_controls(dai, &fsl_xcvr_earc_capds_kctl, 1);
1049 }
1050 snd_soc_add_dai_controls(dai, fsl_xcvr_tx_ctls,
1051 ARRAY_SIZE(fsl_xcvr_tx_ctls));
1052 snd_soc_add_dai_controls(dai, fsl_xcvr_rx_ctls,
1053 ARRAY_SIZE(fsl_xcvr_rx_ctls));
1054 return 0;
1055 }
1056
1057 static const struct snd_soc_dai_ops fsl_xcvr_dai_ops = {
1058 .probe = fsl_xcvr_dai_probe,
1059 .prepare = fsl_xcvr_prepare,
1060 .startup = fsl_xcvr_startup,
1061 .shutdown = fsl_xcvr_shutdown,
1062 .trigger = fsl_xcvr_trigger,
1063 };
1064
1065 static struct snd_soc_dai_driver fsl_xcvr_dai = {
1066 .ops = &fsl_xcvr_dai_ops,
1067 .playback = {
1068 .stream_name = "CPU-Playback",
1069 .channels_min = 1,
1070 .channels_max = 32,
1071 .rate_min = 32000,
1072 .rate_max = 1536000,
1073 .rates = SNDRV_PCM_RATE_KNOT,
1074 .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
1075 },
1076 .capture = {
1077 .stream_name = "CPU-Capture",
1078 .channels_min = 1,
1079 .channels_max = 32,
1080 .rate_min = 32000,
1081 .rate_max = 1536000,
1082 .rates = SNDRV_PCM_RATE_KNOT,
1083 .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
1084 },
1085 };
1086
1087 static const struct snd_soc_component_driver fsl_xcvr_comp = {
1088 .name = "fsl-xcvr-dai",
1089 .legacy_dai_naming = 1,
1090 };
1091
1092 static const struct reg_default fsl_xcvr_reg_defaults[] = {
1093 { FSL_XCVR_VERSION, 0x00000000 },
1094 { FSL_XCVR_EXT_CTRL, 0xF8204040 },
1095 { FSL_XCVR_EXT_STATUS, 0x00000000 },
1096 { FSL_XCVR_EXT_IER0, 0x00000000 },
1097 { FSL_XCVR_EXT_IER1, 0x00000000 },
1098 { FSL_XCVR_EXT_ISR, 0x00000000 },
1099 { FSL_XCVR_EXT_ISR_SET, 0x00000000 },
1100 { FSL_XCVR_EXT_ISR_CLR, 0x00000000 },
1101 { FSL_XCVR_EXT_ISR_TOG, 0x00000000 },
1102 { FSL_XCVR_IER, 0x00000000 },
1103 { FSL_XCVR_ISR, 0x00000000 },
1104 { FSL_XCVR_ISR_SET, 0x00000000 },
1105 { FSL_XCVR_ISR_CLR, 0x00000000 },
1106 { FSL_XCVR_ISR_TOG, 0x00000000 },
1107 { FSL_XCVR_CLK_CTRL, 0x0000018F },
1108 { FSL_XCVR_RX_DPTH_CTRL, 0x00040CC1 },
1109 { FSL_XCVR_RX_DPTH_CTRL_SET, 0x00040CC1 },
1110 { FSL_XCVR_RX_DPTH_CTRL_CLR, 0x00040CC1 },
1111 { FSL_XCVR_RX_DPTH_CTRL_TOG, 0x00040CC1 },
1112 { FSL_XCVR_RX_DPTH_CNTR_CTRL, 0x00000000 },
1113 { FSL_XCVR_RX_DPTH_CNTR_CTRL_SET, 0x00000000 },
1114 { FSL_XCVR_RX_DPTH_CNTR_CTRL_CLR, 0x00000000 },
1115 { FSL_XCVR_RX_DPTH_CNTR_CTRL_TOG, 0x00000000 },
1116 { FSL_XCVR_RX_DPTH_TSCR, 0x00000000 },
1117 { FSL_XCVR_RX_DPTH_BCR, 0x00000000 },
1118 { FSL_XCVR_RX_DPTH_BCTR, 0x00000000 },
1119 { FSL_XCVR_RX_DPTH_BCRR, 0x00000000 },
1120 { FSL_XCVR_TX_DPTH_CTRL, 0x00000000 },
1121 { FSL_XCVR_TX_DPTH_CTRL_SET, 0x00000000 },
1122 { FSL_XCVR_TX_DPTH_CTRL_CLR, 0x00000000 },
1123 { FSL_XCVR_TX_DPTH_CTRL_TOG, 0x00000000 },
1124 { FSL_XCVR_TX_CS_DATA_0, 0x00000000 },
1125 { FSL_XCVR_TX_CS_DATA_1, 0x00000000 },
1126 { FSL_XCVR_TX_CS_DATA_2, 0x00000000 },
1127 { FSL_XCVR_TX_CS_DATA_3, 0x00000000 },
1128 { FSL_XCVR_TX_CS_DATA_4, 0x00000000 },
1129 { FSL_XCVR_TX_CS_DATA_5, 0x00000000 },
1130 { FSL_XCVR_TX_DPTH_CNTR_CTRL, 0x00000000 },
1131 { FSL_XCVR_TX_DPTH_CNTR_CTRL_SET, 0x00000000 },
1132 { FSL_XCVR_TX_DPTH_CNTR_CTRL_CLR, 0x00000000 },
1133 { FSL_XCVR_TX_DPTH_CNTR_CTRL_TOG, 0x00000000 },
1134 { FSL_XCVR_TX_DPTH_TSCR, 0x00000000 },
1135 { FSL_XCVR_TX_DPTH_BCR, 0x00000000 },
1136 { FSL_XCVR_TX_DPTH_BCTR, 0x00000000 },
1137 { FSL_XCVR_TX_DPTH_BCRR, 0x00000000 },
1138 { FSL_XCVR_DEBUG_REG_0, 0x00000000 },
1139 { FSL_XCVR_DEBUG_REG_1, 0x00000000 },
1140 };
1141
1142 static bool fsl_xcvr_readable_reg(struct device *dev, unsigned int reg)
1143 {
1144 struct fsl_xcvr *xcvr = dev_get_drvdata(dev);
1145
1146 if (!xcvr->soc_data->use_phy)
1147 if ((reg >= FSL_XCVR_IER && reg <= FSL_XCVR_PHY_AI_RDATA) ||
1148 reg > FSL_XCVR_TX_DPTH_BCRR)
1149 return false;
1150 switch (reg) {
1151 case FSL_XCVR_VERSION:
1152 case FSL_XCVR_EXT_CTRL:
1153 case FSL_XCVR_EXT_STATUS:
1154 case FSL_XCVR_EXT_IER0:
1155 case FSL_XCVR_EXT_IER1:
1156 case FSL_XCVR_EXT_ISR:
1157 case FSL_XCVR_EXT_ISR_SET:
1158 case FSL_XCVR_EXT_ISR_CLR:
1159 case FSL_XCVR_EXT_ISR_TOG:
1160 case FSL_XCVR_IER:
1161 case FSL_XCVR_ISR:
1162 case FSL_XCVR_ISR_SET:
1163 case FSL_XCVR_ISR_CLR:
1164 case FSL_XCVR_ISR_TOG:
1165 case FSL_XCVR_PHY_AI_CTRL:
1166 case FSL_XCVR_PHY_AI_CTRL_SET:
1167 case FSL_XCVR_PHY_AI_CTRL_CLR:
1168 case FSL_XCVR_PHY_AI_CTRL_TOG:
1169 case FSL_XCVR_PHY_AI_RDATA:
1170 case FSL_XCVR_CLK_CTRL:
1171 case FSL_XCVR_RX_DPTH_CTRL:
1172 case FSL_XCVR_RX_DPTH_CTRL_SET:
1173 case FSL_XCVR_RX_DPTH_CTRL_CLR:
1174 case FSL_XCVR_RX_DPTH_CTRL_TOG:
1175 case FSL_XCVR_RX_CS_DATA_0:
1176 case FSL_XCVR_RX_CS_DATA_1:
1177 case FSL_XCVR_RX_CS_DATA_2:
1178 case FSL_XCVR_RX_CS_DATA_3:
1179 case FSL_XCVR_RX_CS_DATA_4:
1180 case FSL_XCVR_RX_CS_DATA_5:
1181 case FSL_XCVR_RX_DPTH_CNTR_CTRL:
1182 case FSL_XCVR_RX_DPTH_CNTR_CTRL_SET:
1183 case FSL_XCVR_RX_DPTH_CNTR_CTRL_CLR:
1184 case FSL_XCVR_RX_DPTH_CNTR_CTRL_TOG:
1185 case FSL_XCVR_RX_DPTH_TSCR:
1186 case FSL_XCVR_RX_DPTH_BCR:
1187 case FSL_XCVR_RX_DPTH_BCTR:
1188 case FSL_XCVR_RX_DPTH_BCRR:
1189 case FSL_XCVR_TX_DPTH_CTRL:
1190 case FSL_XCVR_TX_DPTH_CTRL_SET:
1191 case FSL_XCVR_TX_DPTH_CTRL_CLR:
1192 case FSL_XCVR_TX_DPTH_CTRL_TOG:
1193 case FSL_XCVR_TX_CS_DATA_0:
1194 case FSL_XCVR_TX_CS_DATA_1:
1195 case FSL_XCVR_TX_CS_DATA_2:
1196 case FSL_XCVR_TX_CS_DATA_3:
1197 case FSL_XCVR_TX_CS_DATA_4:
1198 case FSL_XCVR_TX_CS_DATA_5:
1199 case FSL_XCVR_TX_DPTH_CNTR_CTRL:
1200 case FSL_XCVR_TX_DPTH_CNTR_CTRL_SET:
1201 case FSL_XCVR_TX_DPTH_CNTR_CTRL_CLR:
1202 case FSL_XCVR_TX_DPTH_CNTR_CTRL_TOG:
1203 case FSL_XCVR_TX_DPTH_TSCR:
1204 case FSL_XCVR_TX_DPTH_BCR:
1205 case FSL_XCVR_TX_DPTH_BCTR:
1206 case FSL_XCVR_TX_DPTH_BCRR:
1207 case FSL_XCVR_DEBUG_REG_0:
1208 case FSL_XCVR_DEBUG_REG_1:
1209 return true;
1210 default:
1211 return false;
1212 }
1213 }
1214
1215 static bool fsl_xcvr_writeable_reg(struct device *dev, unsigned int reg)
1216 {
1217 struct fsl_xcvr *xcvr = dev_get_drvdata(dev);
1218
1219 if (!xcvr->soc_data->use_phy)
1220 if (reg >= FSL_XCVR_IER && reg <= FSL_XCVR_PHY_AI_RDATA)
1221 return false;
1222 switch (reg) {
1223 case FSL_XCVR_EXT_CTRL:
1224 case FSL_XCVR_EXT_IER0:
1225 case FSL_XCVR_EXT_IER1:
1226 case FSL_XCVR_EXT_ISR:
1227 case FSL_XCVR_EXT_ISR_SET:
1228 case FSL_XCVR_EXT_ISR_CLR:
1229 case FSL_XCVR_EXT_ISR_TOG:
1230 case FSL_XCVR_IER:
1231 case FSL_XCVR_ISR_SET:
1232 case FSL_XCVR_ISR_CLR:
1233 case FSL_XCVR_ISR_TOG:
1234 case FSL_XCVR_PHY_AI_CTRL:
1235 case FSL_XCVR_PHY_AI_CTRL_SET:
1236 case FSL_XCVR_PHY_AI_CTRL_CLR:
1237 case FSL_XCVR_PHY_AI_CTRL_TOG:
1238 case FSL_XCVR_PHY_AI_WDATA:
1239 case FSL_XCVR_CLK_CTRL:
1240 case FSL_XCVR_RX_DPTH_CTRL:
1241 case FSL_XCVR_RX_DPTH_CTRL_SET:
1242 case FSL_XCVR_RX_DPTH_CTRL_CLR:
1243 case FSL_XCVR_RX_DPTH_CTRL_TOG:
1244 case FSL_XCVR_RX_DPTH_CNTR_CTRL:
1245 case FSL_XCVR_RX_DPTH_CNTR_CTRL_SET:
1246 case FSL_XCVR_RX_DPTH_CNTR_CTRL_CLR:
1247 case FSL_XCVR_RX_DPTH_CNTR_CTRL_TOG:
1248 case FSL_XCVR_TX_DPTH_CTRL:
1249 case FSL_XCVR_TX_DPTH_CTRL_SET:
1250 case FSL_XCVR_TX_DPTH_CTRL_CLR:
1251 case FSL_XCVR_TX_DPTH_CTRL_TOG:
1252 case FSL_XCVR_TX_CS_DATA_0:
1253 case FSL_XCVR_TX_CS_DATA_1:
1254 case FSL_XCVR_TX_CS_DATA_2:
1255 case FSL_XCVR_TX_CS_DATA_3:
1256 case FSL_XCVR_TX_CS_DATA_4:
1257 case FSL_XCVR_TX_CS_DATA_5:
1258 case FSL_XCVR_TX_DPTH_CNTR_CTRL:
1259 case FSL_XCVR_TX_DPTH_CNTR_CTRL_SET:
1260 case FSL_XCVR_TX_DPTH_CNTR_CTRL_CLR:
1261 case FSL_XCVR_TX_DPTH_CNTR_CTRL_TOG:
1262 return true;
1263 default:
1264 return false;
1265 }
1266 }
1267
1268 static bool fsl_xcvr_volatile_reg(struct device *dev, unsigned int reg)
1269 {
1270 switch (reg) {
1271 case FSL_XCVR_EXT_STATUS:
1272 case FSL_XCVR_EXT_ISR:
1273 case FSL_XCVR_EXT_ISR_SET:
1274 case FSL_XCVR_EXT_ISR_CLR:
1275 case FSL_XCVR_EXT_ISR_TOG:
1276 case FSL_XCVR_ISR:
1277 case FSL_XCVR_ISR_SET:
1278 case FSL_XCVR_ISR_CLR:
1279 case FSL_XCVR_ISR_TOG:
1280 case FSL_XCVR_PHY_AI_CTRL:
1281 case FSL_XCVR_PHY_AI_CTRL_SET:
1282 case FSL_XCVR_PHY_AI_CTRL_CLR:
1283 case FSL_XCVR_PHY_AI_CTRL_TOG:
1284 case FSL_XCVR_PHY_AI_RDATA:
1285 case FSL_XCVR_RX_CS_DATA_0:
1286 case FSL_XCVR_RX_CS_DATA_1:
1287 case FSL_XCVR_RX_CS_DATA_2:
1288 case FSL_XCVR_RX_CS_DATA_3:
1289 case FSL_XCVR_RX_CS_DATA_4:
1290 case FSL_XCVR_RX_CS_DATA_5:
1291 case FSL_XCVR_RX_DPTH_CNTR_CTRL:
1292 case FSL_XCVR_RX_DPTH_CNTR_CTRL_SET:
1293 case FSL_XCVR_RX_DPTH_CNTR_CTRL_CLR:
1294 case FSL_XCVR_RX_DPTH_CNTR_CTRL_TOG:
1295 case FSL_XCVR_RX_DPTH_TSCR:
1296 case FSL_XCVR_RX_DPTH_BCR:
1297 case FSL_XCVR_RX_DPTH_BCTR:
1298 case FSL_XCVR_RX_DPTH_BCRR:
1299 case FSL_XCVR_TX_DPTH_CNTR_CTRL:
1300 case FSL_XCVR_TX_DPTH_CNTR_CTRL_SET:
1301 case FSL_XCVR_TX_DPTH_CNTR_CTRL_CLR:
1302 case FSL_XCVR_TX_DPTH_CNTR_CTRL_TOG:
1303 case FSL_XCVR_TX_DPTH_TSCR:
1304 case FSL_XCVR_TX_DPTH_BCR:
1305 case FSL_XCVR_TX_DPTH_BCTR:
1306 case FSL_XCVR_TX_DPTH_BCRR:
1307 case FSL_XCVR_DEBUG_REG_0:
1308 case FSL_XCVR_DEBUG_REG_1:
1309 return true;
1310 default:
1311 return false;
1312 }
1313 }
1314
1315 static const struct regmap_config fsl_xcvr_regmap_cfg = {
1316 .reg_bits = 32,
1317 .reg_stride = 4,
1318 .val_bits = 32,
1319 .max_register = FSL_XCVR_MAX_REG,
1320 .reg_defaults = fsl_xcvr_reg_defaults,
1321 .num_reg_defaults = ARRAY_SIZE(fsl_xcvr_reg_defaults),
1322 .readable_reg = fsl_xcvr_readable_reg,
1323 .volatile_reg = fsl_xcvr_volatile_reg,
1324 .writeable_reg = fsl_xcvr_writeable_reg,
1325 .cache_type = REGCACHE_FLAT,
1326 };
1327
1328 static const struct reg_default fsl_xcvr_phy_reg_defaults[] = {
1329 { FSL_XCVR_PHY_CTRL, 0x58200804 },
1330 { FSL_XCVR_PHY_STATUS, 0x00000000 },
1331 { FSL_XCVR_PHY_ANALOG_TRIM, 0x00260F13 },
1332 { FSL_XCVR_PHY_SLEW_RATE_TRIM, 0x00000411 },
1333 { FSL_XCVR_PHY_DATA_TEST_DELAY, 0x00990000 },
1334 { FSL_XCVR_PHY_TEST_CTRL, 0x00000000 },
1335 { FSL_XCVR_PHY_DIFF_CDR_CTRL, 0x016D0009 },
1336 { FSL_XCVR_PHY_CTRL2, 0x80000000 },
1337 };
1338
1339 static const struct regmap_config fsl_xcvr_regmap_phy_cfg = {
1340 .name = "phy",
1341 .reg_bits = 8,
1342 .reg_stride = 4,
1343 .val_bits = 32,
1344 .max_register = FSL_XCVR_PHY_CTRL2_TOG,
1345 .reg_defaults = fsl_xcvr_phy_reg_defaults,
1346 .num_reg_defaults = ARRAY_SIZE(fsl_xcvr_phy_reg_defaults),
1347 .cache_type = REGCACHE_FLAT,
1348 .reg_read = fsl_xcvr_phy_reg_read,
1349 .reg_write = fsl_xcvr_phy_reg_write,
1350 };
1351
1352 static const struct regmap_config fsl_xcvr_regmap_pllv0_cfg = {
1353 .name = "pllv0",
1354 .reg_bits = 8,
1355 .reg_stride = 4,
1356 .val_bits = 32,
1357 .max_register = FSL_XCVR_PLL_STAT0_TOG,
1358 .cache_type = REGCACHE_FLAT,
1359 .reg_read = fsl_xcvr_pll_reg_read,
1360 .reg_write = fsl_xcvr_pll_reg_write,
1361 };
1362
1363 static const struct regmap_config fsl_xcvr_regmap_pllv1_cfg = {
1364 .name = "pllv1",
1365 .reg_bits = 8,
1366 .reg_stride = 4,
1367 .val_bits = 32,
1368 .max_register = FSL_XCVR_GP_PLL_STATUS_TOG,
1369 .cache_type = REGCACHE_FLAT,
1370 .reg_read = fsl_xcvr_pll_reg_read,
1371 .reg_write = fsl_xcvr_pll_reg_write,
1372 };
1373
1374 static void reset_rx_work(struct work_struct *work)
1375 {
1376 struct fsl_xcvr *xcvr = container_of(work, struct fsl_xcvr, work_rst);
1377 struct device *dev = &xcvr->pdev->dev;
1378 unsigned long lock_flags;
1379 u32 ext_ctrl;
1380
1381 dev_dbg(dev, "reset rx path\n");
1382 spin_lock_irqsave(&xcvr->lock, lock_flags);
1383 regmap_read(xcvr->regmap, FSL_XCVR_EXT_CTRL, &ext_ctrl);
1384
1385 if (!(ext_ctrl & FSL_XCVR_EXT_CTRL_DMA_RD_DIS)) {
1386 regmap_update_bits(xcvr->regmap, FSL_XCVR_EXT_CTRL,
1387 FSL_XCVR_EXT_CTRL_DMA_RD_DIS,
1388 FSL_XCVR_EXT_CTRL_DMA_RD_DIS);
1389 regmap_update_bits(xcvr->regmap, FSL_XCVR_EXT_CTRL,
1390 FSL_XCVR_EXT_CTRL_RX_DPTH_RESET,
1391 FSL_XCVR_EXT_CTRL_RX_DPTH_RESET);
1392 regmap_update_bits(xcvr->regmap, FSL_XCVR_EXT_CTRL,
1393 FSL_XCVR_EXT_CTRL_DMA_RD_DIS,
1394 0);
1395 regmap_update_bits(xcvr->regmap, FSL_XCVR_EXT_CTRL,
1396 FSL_XCVR_EXT_CTRL_RX_DPTH_RESET,
1397 0);
1398 }
1399 spin_unlock_irqrestore(&xcvr->lock, lock_flags);
1400 }
1401
1402 static irqreturn_t irq0_isr(int irq, void *devid)
1403 {
1404 struct fsl_xcvr *xcvr = (struct fsl_xcvr *)devid;
1405 struct device *dev = &xcvr->pdev->dev;
1406 struct regmap *regmap = xcvr->regmap;
1407 void __iomem *reg_ctrl, *reg_buff;
1408 u32 isr, isr_clr = 0, val, i;
1409
1410 regmap_read(regmap, FSL_XCVR_EXT_ISR, &isr);
1411
1412 if (isr & FSL_XCVR_IRQ_NEW_CS) {
1413 dev_dbg(dev, "Received new CS block\n");
1414 isr_clr |= FSL_XCVR_IRQ_NEW_CS;
1415 if (xcvr->soc_data->fw_name) {
1416 /* Data RAM is 4KiB, last two pages: 8 and 9. Select page 8. */
1417 regmap_update_bits(xcvr->regmap, FSL_XCVR_EXT_CTRL,
1418 FSL_XCVR_EXT_CTRL_PAGE_MASK,
1419 FSL_XCVR_EXT_CTRL_PAGE(8));
1420
1421 /* Find updated CS buffer */
1422 reg_ctrl = xcvr->ram_addr + FSL_XCVR_RX_CS_CTRL_0;
1423 reg_buff = xcvr->ram_addr + FSL_XCVR_RX_CS_BUFF_0;
1424 memcpy_fromio(&val, reg_ctrl, sizeof(val));
1425 if (!val) {
1426 reg_ctrl = xcvr->ram_addr + FSL_XCVR_RX_CS_CTRL_1;
1427 reg_buff = xcvr->ram_addr + FSL_XCVR_RX_CS_BUFF_1;
1428 memcpy_fromio(&val, reg_ctrl, sizeof(val));
1429 }
1430
1431 if (val) {
1432 /* copy CS buffer */
1433 memcpy_fromio(&xcvr->rx_iec958.status, reg_buff,
1434 sizeof(xcvr->rx_iec958.status));
1435 for (i = 0; i < 6; i++) {
1436 val = *(u32 *)(xcvr->rx_iec958.status + i*4);
1437 *(u32 *)(xcvr->rx_iec958.status + i*4) =
1438 bitrev32(val);
1439 }
1440 /* clear CS control register */
1441 writel_relaxed(0, reg_ctrl);
1442 }
1443 } else {
1444 regmap_read(xcvr->regmap, FSL_XCVR_RX_CS_DATA_0,
1445 (u32 *)&xcvr->rx_iec958.status[0]);
1446 regmap_read(xcvr->regmap, FSL_XCVR_RX_CS_DATA_1,
1447 (u32 *)&xcvr->rx_iec958.status[4]);
1448 regmap_read(xcvr->regmap, FSL_XCVR_RX_CS_DATA_2,
1449 (u32 *)&xcvr->rx_iec958.status[8]);
1450 regmap_read(xcvr->regmap, FSL_XCVR_RX_CS_DATA_3,
1451 (u32 *)&xcvr->rx_iec958.status[12]);
1452 regmap_read(xcvr->regmap, FSL_XCVR_RX_CS_DATA_4,
1453 (u32 *)&xcvr->rx_iec958.status[16]);
1454 regmap_read(xcvr->regmap, FSL_XCVR_RX_CS_DATA_5,
1455 (u32 *)&xcvr->rx_iec958.status[20]);
1456 for (i = 0; i < 6; i++) {
1457 val = *(u32 *)(xcvr->rx_iec958.status + i * 4);
1458 *(u32 *)(xcvr->rx_iec958.status + i * 4) =
1459 bitrev32(val);
1460 }
1461 regmap_set_bits(xcvr->regmap, FSL_XCVR_RX_DPTH_CTRL,
1462 FSL_XCVR_RX_DPTH_CTRL_CSA);
1463 }
1464 }
1465 if (isr & FSL_XCVR_IRQ_NEW_UD) {
1466 dev_dbg(dev, "Received new UD block\n");
1467 isr_clr |= FSL_XCVR_IRQ_NEW_UD;
1468 }
1469 if (isr & FSL_XCVR_IRQ_MUTE) {
1470 dev_dbg(dev, "HW mute bit detected\n");
1471 isr_clr |= FSL_XCVR_IRQ_MUTE;
1472 }
1473 if (isr & FSL_XCVR_IRQ_FIFO_UOFL_ERR) {
1474 dev_dbg(dev, "RX/TX FIFO full/empty\n");
1475 isr_clr |= FSL_XCVR_IRQ_FIFO_UOFL_ERR;
1476 }
1477 if (isr & FSL_XCVR_IRQ_ARC_MODE) {
1478 dev_dbg(dev, "CMDC SM falls out of eARC mode\n");
1479 isr_clr |= FSL_XCVR_IRQ_ARC_MODE;
1480 }
1481 if (isr & FSL_XCVR_IRQ_DMA_RD_REQ) {
1482 dev_dbg(dev, "DMA read request\n");
1483 isr_clr |= FSL_XCVR_IRQ_DMA_RD_REQ;
1484 }
1485 if (isr & FSL_XCVR_IRQ_DMA_WR_REQ) {
1486 dev_dbg(dev, "DMA write request\n");
1487 isr_clr |= FSL_XCVR_IRQ_DMA_WR_REQ;
1488 }
1489 if (isr & FSL_XCVR_IRQ_CMDC_STATUS_UPD) {
1490 dev_dbg(dev, "CMDC status update\n");
1491 isr_clr |= FSL_XCVR_IRQ_CMDC_STATUS_UPD;
1492 }
1493 if (isr & FSL_XCVR_IRQ_PREAMBLE_MISMATCH) {
1494 dev_dbg(dev, "Preamble mismatch\n");
1495 isr_clr |= FSL_XCVR_IRQ_PREAMBLE_MISMATCH;
1496 }
1497 if (isr & FSL_XCVR_IRQ_UNEXP_PRE_REC) {
1498 dev_dbg(dev, "Unexpected preamble received\n");
1499 isr_clr |= FSL_XCVR_IRQ_UNEXP_PRE_REC;
1500 }
1501 if (isr & FSL_XCVR_IRQ_M_W_PRE_MISMATCH) {
1502 dev_dbg(dev, "M/W preamble mismatch\n");
1503 isr_clr |= FSL_XCVR_IRQ_M_W_PRE_MISMATCH;
1504 }
1505 if (isr & FSL_XCVR_IRQ_B_PRE_MISMATCH) {
1506 dev_dbg(dev, "B preamble mismatch\n");
1507 isr_clr |= FSL_XCVR_IRQ_B_PRE_MISMATCH;
1508 }
1509
1510 if (isr & (FSL_XCVR_IRQ_PREAMBLE_MISMATCH |
1511 FSL_XCVR_IRQ_UNEXP_PRE_REC |
1512 FSL_XCVR_IRQ_M_W_PRE_MISMATCH |
1513 FSL_XCVR_IRQ_B_PRE_MISMATCH)) {
1514 schedule_work(&xcvr->work_rst);
1515 }
1516
1517 if (isr_clr) {
1518 regmap_write(regmap, FSL_XCVR_EXT_ISR_CLR, isr_clr);
1519 return IRQ_HANDLED;
1520 }
1521
1522 return IRQ_NONE;
1523 }
1524
1525 static const struct fsl_xcvr_soc_data fsl_xcvr_imx8mp_data = {
1526 .fw_name = "imx/xcvr/xcvr-imx8mp.bin",
1527 .use_phy = true,
1528 .pll_ver = PLL_MX8MP,
1529 };
1530
1531 static const struct fsl_xcvr_soc_data fsl_xcvr_imx93_data = {
1532 .spdif_only = true,
1533 .use_edma = true,
1534 };
1535
1536 static const struct fsl_xcvr_soc_data fsl_xcvr_imx95_data = {
1537 .fw_name = "imx/xcvr/xcvr-imx95.bin",
1538 .spdif_only = true,
1539 .use_phy = true,
1540 .use_edma = true,
1541 .pll_ver = PLL_MX95,
1542 };
1543
1544 static const struct of_device_id fsl_xcvr_dt_ids[] = {
1545 { .compatible = "fsl,imx8mp-xcvr", .data = &fsl_xcvr_imx8mp_data },
1546 { .compatible = "fsl,imx93-xcvr", .data = &fsl_xcvr_imx93_data},
1547 { .compatible = "fsl,imx95-xcvr", .data = &fsl_xcvr_imx95_data},
1548 { /* sentinel */ }
1549 };
1550 MODULE_DEVICE_TABLE(of, fsl_xcvr_dt_ids);
1551
1552 static int fsl_xcvr_probe(struct platform_device *pdev)
1553 {
1554 struct device *dev = &pdev->dev;
1555 struct fsl_xcvr *xcvr;
1556 struct resource *rx_res, *tx_res;
1557 void __iomem *regs;
1558 int ret, irq;
1559
1560 xcvr = devm_kzalloc(dev, sizeof(*xcvr), GFP_KERNEL);
1561 if (!xcvr)
1562 return -ENOMEM;
1563
1564 xcvr->pdev = pdev;
1565 xcvr->soc_data = of_device_get_match_data(&pdev->dev);
1566
1567 xcvr->ipg_clk = devm_clk_get(dev, "ipg");
1568 if (IS_ERR(xcvr->ipg_clk))
1569 return dev_err_probe(dev, PTR_ERR(xcvr->ipg_clk),
1570 "failed to get ipg clock\n");
1571
1572 xcvr->phy_clk = devm_clk_get(dev, "phy");
1573 if (IS_ERR(xcvr->phy_clk))
1574 return dev_err_probe(dev, PTR_ERR(xcvr->phy_clk),
1575 "failed to get phy clock\n");
1576
1577 xcvr->spba_clk = devm_clk_get(dev, "spba");
1578 if (IS_ERR(xcvr->spba_clk))
1579 return dev_err_probe(dev, PTR_ERR(xcvr->spba_clk),
1580 "failed to get spba clock\n");
1581
1582 xcvr->pll_ipg_clk = devm_clk_get(dev, "pll_ipg");
1583 if (IS_ERR(xcvr->pll_ipg_clk))
1584 return dev_err_probe(dev, PTR_ERR(xcvr->pll_ipg_clk),
1585 "failed to get pll_ipg clock\n");
1586
1587 fsl_asoc_get_pll_clocks(dev, &xcvr->pll8k_clk,
1588 &xcvr->pll11k_clk);
1589
1590 if (xcvr->soc_data->spdif_only) {
1591 if (!(xcvr->pll8k_clk || xcvr->pll11k_clk))
1592 xcvr->pll8k_clk = xcvr->phy_clk;
1593 fsl_asoc_constrain_rates(&xcvr->spdif_constr_rates,
1594 &fsl_xcvr_spdif_rates_constr,
1595 xcvr->pll8k_clk, xcvr->pll11k_clk, NULL,
1596 xcvr->spdif_constr_rates_list);
1597 }
1598
1599 xcvr->ram_addr = devm_platform_ioremap_resource_byname(pdev, "ram");
1600 if (IS_ERR(xcvr->ram_addr))
1601 return PTR_ERR(xcvr->ram_addr);
1602
1603 regs = devm_platform_ioremap_resource_byname(pdev, "regs");
1604 if (IS_ERR(regs))
1605 return PTR_ERR(regs);
1606
1607 xcvr->regmap = devm_regmap_init_mmio_clk(dev, NULL, regs,
1608 &fsl_xcvr_regmap_cfg);
1609 if (IS_ERR(xcvr->regmap))
1610 return dev_err_probe(dev, PTR_ERR(xcvr->regmap), "failed to init XCVR regmap\n");
1611
1612 if (xcvr->soc_data->use_phy) {
1613 xcvr->regmap_phy = devm_regmap_init(dev, NULL, xcvr,
1614 &fsl_xcvr_regmap_phy_cfg);
1615 if (IS_ERR(xcvr->regmap_phy))
1616 return dev_err_probe(dev, PTR_ERR(xcvr->regmap_phy),
1617 "failed to init XCVR PHY regmap\n");
1618
1619 switch (xcvr->soc_data->pll_ver) {
1620 case PLL_MX8MP:
1621 xcvr->regmap_pll = devm_regmap_init(dev, NULL, xcvr,
1622 &fsl_xcvr_regmap_pllv0_cfg);
1623 if (IS_ERR(xcvr->regmap_pll))
1624 return dev_err_probe(dev, PTR_ERR(xcvr->regmap_pll),
1625 "failed to init XCVR PLL regmap\n");
1626 break;
1627 case PLL_MX95:
1628 xcvr->regmap_pll = devm_regmap_init(dev, NULL, xcvr,
1629 &fsl_xcvr_regmap_pllv1_cfg);
1630 if (IS_ERR(xcvr->regmap_pll))
1631 return dev_err_probe(dev, PTR_ERR(xcvr->regmap_pll),
1632 "failed to init XCVR PLL regmap\n");
1633 break;
1634 default:
1635 return dev_err_probe(dev, -EINVAL,
1636 "Error for PLL version %d\n",
1637 xcvr->soc_data->pll_ver);
1638 }
1639 }
1640
1641 xcvr->reset = devm_reset_control_get_optional_exclusive(dev, NULL);
1642 if (IS_ERR(xcvr->reset))
1643 return dev_err_probe(dev, PTR_ERR(xcvr->reset),
1644 "failed to get XCVR reset control\n");
1645
1646 /* get IRQs */
1647 irq = platform_get_irq(pdev, 0);
1648 if (irq < 0)
1649 return irq;
1650
1651 ret = devm_request_irq(dev, irq, irq0_isr, 0, pdev->name, xcvr);
1652 if (ret)
1653 return dev_err_probe(dev, ret, "failed to claim IRQ0\n");
1654
1655 rx_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rxfifo");
1656 tx_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "txfifo");
1657 if (!rx_res || !tx_res)
1658 return dev_err_probe(dev, -EINVAL, "could not find rxfifo or txfifo resource\n");
1659 xcvr->dma_prms_rx.chan_name = "rx";
1660 xcvr->dma_prms_tx.chan_name = "tx";
1661 xcvr->dma_prms_rx.addr = rx_res->start;
1662 xcvr->dma_prms_tx.addr = tx_res->start;
1663 xcvr->dma_prms_rx.maxburst = FSL_XCVR_MAXBURST_RX;
1664 xcvr->dma_prms_tx.maxburst = FSL_XCVR_MAXBURST_TX;
1665
1666 platform_set_drvdata(pdev, xcvr);
1667 pm_runtime_enable(dev);
1668 regcache_cache_only(xcvr->regmap, true);
1669 if (xcvr->soc_data->use_phy) {
1670 regcache_cache_only(xcvr->regmap_phy, true);
1671 regcache_cache_only(xcvr->regmap_pll, true);
1672 }
1673
1674 /*
1675 * Register platform component before registering cpu dai for there
1676 * is not defer probe for platform component in snd_soc_add_pcm_runtime().
1677 */
1678 ret = devm_snd_dmaengine_pcm_register(dev, NULL, 0);
1679 if (ret) {
1680 pm_runtime_disable(dev);
1681 return dev_err_probe(dev, ret, "failed to pcm register\n");
1682 }
1683
1684 ret = devm_snd_soc_register_component(dev, &fsl_xcvr_comp,
1685 &fsl_xcvr_dai, 1);
1686 if (ret) {
1687 pm_runtime_disable(dev);
1688 dev_err(dev, "failed to register component %s\n",
1689 fsl_xcvr_comp.name);
1690 }
1691
1692 INIT_WORK(&xcvr->work_rst, reset_rx_work);
1693 spin_lock_init(&xcvr->lock);
1694 return ret;
1695 }
1696
1697 static void fsl_xcvr_remove(struct platform_device *pdev)
1698 {
1699 struct fsl_xcvr *xcvr = dev_get_drvdata(&pdev->dev);
1700
1701 cancel_work_sync(&xcvr->work_rst);
1702 pm_runtime_disable(&pdev->dev);
1703 }
1704
1705 static int fsl_xcvr_runtime_suspend(struct device *dev)
1706 {
1707 struct fsl_xcvr *xcvr = dev_get_drvdata(dev);
1708 int ret;
1709
1710 if (!xcvr->soc_data->spdif_only &&
1711 xcvr->mode == FSL_XCVR_MODE_EARC) {
1712 /* Assert M0+ reset */
1713 ret = regmap_update_bits(xcvr->regmap, FSL_XCVR_EXT_CTRL,
1714 FSL_XCVR_EXT_CTRL_CORE_RESET,
1715 FSL_XCVR_EXT_CTRL_CORE_RESET);
1716 if (ret < 0)
1717 dev_err(dev, "Failed to assert M0+ core: %d\n", ret);
1718 }
1719
1720 regcache_cache_only(xcvr->regmap, true);
1721 if (xcvr->soc_data->use_phy) {
1722 regcache_cache_only(xcvr->regmap_phy, true);
1723 regcache_cache_only(xcvr->regmap_pll, true);
1724 }
1725
1726 clk_disable_unprepare(xcvr->spba_clk);
1727 clk_disable_unprepare(xcvr->phy_clk);
1728 clk_disable_unprepare(xcvr->pll_ipg_clk);
1729 clk_disable_unprepare(xcvr->ipg_clk);
1730
1731 return 0;
1732 }
1733
1734 static int fsl_xcvr_runtime_resume(struct device *dev)
1735 {
1736 struct fsl_xcvr *xcvr = dev_get_drvdata(dev);
1737 int ret;
1738
1739 ret = reset_control_assert(xcvr->reset);
1740 if (ret < 0) {
1741 dev_err(dev, "Failed to assert M0+ reset: %d\n", ret);
1742 return ret;
1743 }
1744
1745 ret = clk_prepare_enable(xcvr->ipg_clk);
1746 if (ret) {
1747 dev_err(dev, "failed to start IPG clock.\n");
1748 return ret;
1749 }
1750
1751 ret = clk_prepare_enable(xcvr->pll_ipg_clk);
1752 if (ret) {
1753 dev_err(dev, "failed to start PLL IPG clock.\n");
1754 goto stop_ipg_clk;
1755 }
1756
1757 ret = clk_prepare_enable(xcvr->phy_clk);
1758 if (ret) {
1759 dev_err(dev, "failed to start PHY clock: %d\n", ret);
1760 goto stop_pll_ipg_clk;
1761 }
1762
1763 ret = clk_prepare_enable(xcvr->spba_clk);
1764 if (ret) {
1765 dev_err(dev, "failed to start SPBA clock.\n");
1766 goto stop_phy_clk;
1767 }
1768
1769 ret = reset_control_deassert(xcvr->reset);
1770 if (ret) {
1771 dev_err(dev, "failed to deassert M0+ reset.\n");
1772 goto stop_spba_clk;
1773 }
1774
1775 regcache_cache_only(xcvr->regmap, false);
1776 regcache_mark_dirty(xcvr->regmap);
1777 ret = regcache_sync(xcvr->regmap);
1778
1779 if (ret) {
1780 dev_err(dev, "failed to sync regcache.\n");
1781 goto stop_spba_clk;
1782 }
1783
1784 if (xcvr->soc_data->use_phy) {
1785 ret = regmap_write(xcvr->regmap, FSL_XCVR_PHY_AI_CTRL_SET,
1786 FSL_XCVR_PHY_AI_CTRL_AI_RESETN);
1787 if (ret < 0) {
1788 dev_err(dev, "Error while release PHY reset: %d\n", ret);
1789 goto stop_spba_clk;
1790 }
1791
1792 regcache_cache_only(xcvr->regmap_phy, false);
1793 regcache_mark_dirty(xcvr->regmap_phy);
1794 ret = regcache_sync(xcvr->regmap_phy);
1795 if (ret) {
1796 dev_err(dev, "failed to sync phy regcache.\n");
1797 goto stop_spba_clk;
1798 }
1799
1800 regcache_cache_only(xcvr->regmap_pll, false);
1801 regcache_mark_dirty(xcvr->regmap_pll);
1802 ret = regcache_sync(xcvr->regmap_pll);
1803 if (ret) {
1804 dev_err(dev, "failed to sync pll regcache.\n");
1805 goto stop_spba_clk;
1806 }
1807 }
1808
1809 if (xcvr->soc_data->fw_name) {
1810 ret = fsl_xcvr_load_firmware(xcvr);
1811 if (ret) {
1812 dev_err(dev, "failed to load firmware.\n");
1813 goto stop_spba_clk;
1814 }
1815
1816 /* Release M0+ reset */
1817 ret = regmap_update_bits(xcvr->regmap, FSL_XCVR_EXT_CTRL,
1818 FSL_XCVR_EXT_CTRL_CORE_RESET, 0);
1819 if (ret < 0) {
1820 dev_err(dev, "M0+ core release failed: %d\n", ret);
1821 goto stop_spba_clk;
1822 }
1823
1824 /* Let M0+ core complete firmware initialization */
1825 msleep(50);
1826 }
1827
1828 return 0;
1829
1830 stop_spba_clk:
1831 clk_disable_unprepare(xcvr->spba_clk);
1832 stop_phy_clk:
1833 clk_disable_unprepare(xcvr->phy_clk);
1834 stop_pll_ipg_clk:
1835 clk_disable_unprepare(xcvr->pll_ipg_clk);
1836 stop_ipg_clk:
1837 clk_disable_unprepare(xcvr->ipg_clk);
1838
1839 return ret;
1840 }
1841
1842 static const struct dev_pm_ops fsl_xcvr_pm_ops = {
1843 RUNTIME_PM_OPS(fsl_xcvr_runtime_suspend, fsl_xcvr_runtime_resume, NULL)
1844 SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, pm_runtime_force_resume)
1845 };
1846
1847 static struct platform_driver fsl_xcvr_driver = {
1848 .probe = fsl_xcvr_probe,
1849 .driver = {
1850 .name = "fsl-xcvr",
1851 .pm = pm_ptr(&fsl_xcvr_pm_ops),
1852 .of_match_table = fsl_xcvr_dt_ids,
1853 },
1854 .remove = fsl_xcvr_remove,
1855 };
1856 module_platform_driver(fsl_xcvr_driver);
1857
1858 MODULE_AUTHOR("Viorel Suman <[email protected]>");
1859 MODULE_DESCRIPTION("NXP Audio Transceiver (XCVR) driver");
1860 MODULE_LICENSE("GPL v2");