Revert "unicode: Don't special case ignorable code points"
[linux.git] / sound / soc / apple / mca.c
blobb4f4696809dd231a64229114c5550bb5f237607f
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // Apple SoCs MCA driver
4 //
5 // Copyright (C) The Asahi Linux Contributors
6 //
7 // The MCA peripheral is made up of a number of identical units called clusters.
8 // Each cluster has its separate clock parent, SYNC signal generator, carries
9 // four SERDES units and has a dedicated I2S port on the SoC's periphery.
11 // The clusters can operate independently, or can be combined together in a
12 // configurable manner. We mostly treat them as self-contained independent
13 // units and don't configure any cross-cluster connections except for the I2S
14 // ports. The I2S ports can be routed to any of the clusters (irrespective
15 // of their native cluster). We map this onto ASoC's (DPCM) notion of backend
16 // and frontend DAIs. The 'cluster guts' are frontends which are dynamically
17 // routed to backend I2S ports.
19 // DAI references in devicetree are resolved to backends. The routing between
20 // frontends and backends is determined by the machine driver in the DAPM paths
21 // it supplies.
23 #include <linux/bitfield.h>
24 #include <linux/clk.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/init.h>
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/of.h>
30 #include <linux/of_clk.h>
31 #include <linux/of_dma.h>
32 #include <linux/platform_device.h>
33 #include <linux/pm_domain.h>
34 #include <linux/regmap.h>
35 #include <linux/reset.h>
36 #include <linux/slab.h>
38 #include <sound/core.h>
39 #include <sound/pcm.h>
40 #include <sound/pcm_params.h>
41 #include <sound/soc.h>
42 #include <sound/dmaengine_pcm.h>
44 #define USE_RXB_FOR_CAPTURE
46 /* Relative to cluster base */
47 #define REG_STATUS 0x0
48 #define STATUS_MCLK_EN BIT(0)
49 #define REG_MCLK_CONF 0x4
50 #define MCLK_CONF_DIV GENMASK(11, 8)
52 #define REG_SYNCGEN_STATUS 0x100
53 #define SYNCGEN_STATUS_EN BIT(0)
54 #define REG_SYNCGEN_MCLK_SEL 0x104
55 #define SYNCGEN_MCLK_SEL GENMASK(3, 0)
56 #define REG_SYNCGEN_HI_PERIOD 0x108
57 #define REG_SYNCGEN_LO_PERIOD 0x10c
59 #define REG_PORT_ENABLES 0x600
60 #define PORT_ENABLES_CLOCKS GENMASK(2, 1)
61 #define PORT_ENABLES_TX_DATA BIT(3)
62 #define REG_PORT_CLOCK_SEL 0x604
63 #define PORT_CLOCK_SEL GENMASK(11, 8)
64 #define REG_PORT_DATA_SEL 0x608
65 #define PORT_DATA_SEL_TXA(cl) (1 << ((cl)*2))
66 #define PORT_DATA_SEL_TXB(cl) (2 << ((cl)*2))
68 #define REG_INTSTATE 0x700
69 #define REG_INTMASK 0x704
71 /* Bases of serdes units (relative to cluster) */
72 #define CLUSTER_RXA_OFF 0x200
73 #define CLUSTER_TXA_OFF 0x300
74 #define CLUSTER_RXB_OFF 0x400
75 #define CLUSTER_TXB_OFF 0x500
77 #define CLUSTER_TX_OFF CLUSTER_TXA_OFF
79 #ifndef USE_RXB_FOR_CAPTURE
80 #define CLUSTER_RX_OFF CLUSTER_RXA_OFF
81 #else
82 #define CLUSTER_RX_OFF CLUSTER_RXB_OFF
83 #endif
85 /* Relative to serdes unit base */
86 #define REG_SERDES_STATUS 0x00
87 #define SERDES_STATUS_EN BIT(0)
88 #define SERDES_STATUS_RST BIT(1)
89 #define REG_TX_SERDES_CONF 0x04
90 #define REG_RX_SERDES_CONF 0x08
91 #define SERDES_CONF_NCHANS GENMASK(3, 0)
92 #define SERDES_CONF_WIDTH_MASK GENMASK(8, 4)
93 #define SERDES_CONF_WIDTH_16BIT 0x40
94 #define SERDES_CONF_WIDTH_20BIT 0x80
95 #define SERDES_CONF_WIDTH_24BIT 0xc0
96 #define SERDES_CONF_WIDTH_32BIT 0x100
97 #define SERDES_CONF_BCLK_POL 0x400
98 #define SERDES_CONF_LSB_FIRST 0x800
99 #define SERDES_CONF_UNK1 BIT(12)
100 #define SERDES_CONF_UNK2 BIT(13)
101 #define SERDES_CONF_UNK3 BIT(14)
102 #define SERDES_CONF_NO_DATA_FEEDBACK BIT(15)
103 #define SERDES_CONF_SYNC_SEL GENMASK(18, 16)
104 #define REG_TX_SERDES_BITSTART 0x08
105 #define REG_RX_SERDES_BITSTART 0x0c
106 #define REG_TX_SERDES_SLOTMASK 0x0c
107 #define REG_RX_SERDES_SLOTMASK 0x10
108 #define REG_RX_SERDES_PORT 0x04
110 /* Relative to switch base */
111 #define REG_DMA_ADAPTER_A(cl) (0x8000 * (cl))
112 #define REG_DMA_ADAPTER_B(cl) (0x8000 * (cl) + 0x4000)
113 #define DMA_ADAPTER_TX_LSB_PAD GENMASK(4, 0)
114 #define DMA_ADAPTER_TX_NCHANS GENMASK(6, 5)
115 #define DMA_ADAPTER_RX_MSB_PAD GENMASK(12, 8)
116 #define DMA_ADAPTER_RX_NCHANS GENMASK(14, 13)
117 #define DMA_ADAPTER_NCHANS GENMASK(22, 20)
119 #define SWITCH_STRIDE 0x8000
120 #define CLUSTER_STRIDE 0x4000
122 #define MAX_NCLUSTERS 6
124 #define APPLE_MCA_FMTBITS (SNDRV_PCM_FMTBIT_S16_LE | \
125 SNDRV_PCM_FMTBIT_S24_LE | \
126 SNDRV_PCM_FMTBIT_S32_LE)
128 struct mca_cluster {
129 int no;
130 __iomem void *base;
131 struct mca_data *host;
132 struct device *pd_dev;
133 struct clk *clk_parent;
134 struct dma_chan *dma_chans[SNDRV_PCM_STREAM_LAST + 1];
136 bool port_started[SNDRV_PCM_STREAM_LAST + 1];
137 int port_driver; /* The cluster driving this cluster's port */
139 bool clocks_in_use[SNDRV_PCM_STREAM_LAST + 1];
140 struct device_link *pd_link;
142 unsigned int bclk_ratio;
144 /* Masks etc. picked up via the set_tdm_slot method */
145 int tdm_slots;
146 int tdm_slot_width;
147 unsigned int tdm_tx_mask;
148 unsigned int tdm_rx_mask;
151 struct mca_data {
152 struct device *dev;
154 __iomem void *switch_base;
156 struct device *pd_dev;
157 struct reset_control *rstc;
158 struct device_link *pd_link;
160 /* Mutex for accessing port_driver of foreign clusters */
161 struct mutex port_mutex;
163 int nclusters;
164 struct mca_cluster clusters[] __counted_by(nclusters);
167 static void mca_modify(struct mca_cluster *cl, int regoffset, u32 mask, u32 val)
169 __iomem void *ptr = cl->base + regoffset;
170 u32 newval;
172 newval = (val & mask) | (readl_relaxed(ptr) & ~mask);
173 writel_relaxed(newval, ptr);
177 * Get the cluster of FE or BE DAI
179 static struct mca_cluster *mca_dai_to_cluster(struct snd_soc_dai *dai)
181 struct mca_data *mca = snd_soc_dai_get_drvdata(dai);
183 * FE DAIs are 0 ... nclusters - 1
184 * BE DAIs are nclusters ... 2*nclusters - 1
186 int cluster_no = dai->id % mca->nclusters;
188 return &mca->clusters[cluster_no];
191 /* called before PCM trigger */
192 static void mca_fe_early_trigger(struct snd_pcm_substream *substream, int cmd,
193 struct snd_soc_dai *dai)
195 struct mca_cluster *cl = mca_dai_to_cluster(dai);
196 bool is_tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
197 int serdes_unit = is_tx ? CLUSTER_TX_OFF : CLUSTER_RX_OFF;
198 int serdes_conf =
199 serdes_unit + (is_tx ? REG_TX_SERDES_CONF : REG_RX_SERDES_CONF);
201 switch (cmd) {
202 case SNDRV_PCM_TRIGGER_START:
203 case SNDRV_PCM_TRIGGER_RESUME:
204 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
205 mca_modify(cl, serdes_conf, SERDES_CONF_SYNC_SEL,
206 FIELD_PREP(SERDES_CONF_SYNC_SEL, 0));
207 mca_modify(cl, serdes_conf, SERDES_CONF_SYNC_SEL,
208 FIELD_PREP(SERDES_CONF_SYNC_SEL, 7));
209 mca_modify(cl, serdes_unit + REG_SERDES_STATUS,
210 SERDES_STATUS_EN | SERDES_STATUS_RST,
211 SERDES_STATUS_RST);
213 * Experiments suggest that it takes at most ~1 us
214 * for the bit to clear, so wait 2 us for good measure.
216 udelay(2);
217 WARN_ON(readl_relaxed(cl->base + serdes_unit + REG_SERDES_STATUS) &
218 SERDES_STATUS_RST);
219 mca_modify(cl, serdes_conf, SERDES_CONF_SYNC_SEL,
220 FIELD_PREP(SERDES_CONF_SYNC_SEL, 0));
221 mca_modify(cl, serdes_conf, SERDES_CONF_SYNC_SEL,
222 FIELD_PREP(SERDES_CONF_SYNC_SEL, cl->no + 1));
223 break;
224 default:
225 break;
229 static int mca_fe_trigger(struct snd_pcm_substream *substream, int cmd,
230 struct snd_soc_dai *dai)
232 struct mca_cluster *cl = mca_dai_to_cluster(dai);
233 bool is_tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
234 int serdes_unit = is_tx ? CLUSTER_TX_OFF : CLUSTER_RX_OFF;
236 switch (cmd) {
237 case SNDRV_PCM_TRIGGER_START:
238 case SNDRV_PCM_TRIGGER_RESUME:
239 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
240 mca_modify(cl, serdes_unit + REG_SERDES_STATUS,
241 SERDES_STATUS_EN | SERDES_STATUS_RST,
242 SERDES_STATUS_EN);
243 break;
245 case SNDRV_PCM_TRIGGER_STOP:
246 case SNDRV_PCM_TRIGGER_SUSPEND:
247 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
248 mca_modify(cl, serdes_unit + REG_SERDES_STATUS,
249 SERDES_STATUS_EN, 0);
250 break;
252 default:
253 return -EINVAL;
256 return 0;
259 static int mca_fe_enable_clocks(struct mca_cluster *cl)
261 struct mca_data *mca = cl->host;
262 int ret;
264 ret = clk_prepare_enable(cl->clk_parent);
265 if (ret) {
266 dev_err(mca->dev,
267 "cluster %d: unable to enable clock parent: %d\n",
268 cl->no, ret);
269 return ret;
273 * We can't power up the device earlier than this because
274 * the power state driver would error out on seeing the device
275 * as clock-gated.
277 cl->pd_link = device_link_add(mca->dev, cl->pd_dev,
278 DL_FLAG_STATELESS | DL_FLAG_PM_RUNTIME |
279 DL_FLAG_RPM_ACTIVE);
280 if (!cl->pd_link) {
281 dev_err(mca->dev,
282 "cluster %d: unable to prop-up power domain\n", cl->no);
283 clk_disable_unprepare(cl->clk_parent);
284 return -EINVAL;
287 writel_relaxed(cl->no + 1, cl->base + REG_SYNCGEN_MCLK_SEL);
288 mca_modify(cl, REG_SYNCGEN_STATUS, SYNCGEN_STATUS_EN,
289 SYNCGEN_STATUS_EN);
290 mca_modify(cl, REG_STATUS, STATUS_MCLK_EN, STATUS_MCLK_EN);
292 return 0;
295 static void mca_fe_disable_clocks(struct mca_cluster *cl)
297 mca_modify(cl, REG_SYNCGEN_STATUS, SYNCGEN_STATUS_EN, 0);
298 mca_modify(cl, REG_STATUS, STATUS_MCLK_EN, 0);
300 device_link_del(cl->pd_link);
301 clk_disable_unprepare(cl->clk_parent);
304 static bool mca_fe_clocks_in_use(struct mca_cluster *cl)
306 struct mca_data *mca = cl->host;
307 struct mca_cluster *be_cl;
308 int stream, i;
310 mutex_lock(&mca->port_mutex);
311 for (i = 0; i < mca->nclusters; i++) {
312 be_cl = &mca->clusters[i];
314 if (be_cl->port_driver != cl->no)
315 continue;
317 for_each_pcm_streams(stream) {
318 if (be_cl->clocks_in_use[stream]) {
319 mutex_unlock(&mca->port_mutex);
320 return true;
324 mutex_unlock(&mca->port_mutex);
325 return false;
328 static int mca_be_prepare(struct snd_pcm_substream *substream,
329 struct snd_soc_dai *dai)
331 struct mca_cluster *cl = mca_dai_to_cluster(dai);
332 struct mca_data *mca = cl->host;
333 struct mca_cluster *fe_cl;
334 int ret;
336 if (cl->port_driver < 0)
337 return -EINVAL;
339 fe_cl = &mca->clusters[cl->port_driver];
342 * Typically the CODECs we are paired with will require clocks
343 * to be present at time of unmute with the 'mute_stream' op
344 * or at time of DAPM widget power-up. We need to enable clocks
345 * here at the latest (frontend prepare would be too late).
347 if (!mca_fe_clocks_in_use(fe_cl)) {
348 ret = mca_fe_enable_clocks(fe_cl);
349 if (ret < 0)
350 return ret;
353 cl->clocks_in_use[substream->stream] = true;
355 return 0;
358 static int mca_be_hw_free(struct snd_pcm_substream *substream,
359 struct snd_soc_dai *dai)
361 struct mca_cluster *cl = mca_dai_to_cluster(dai);
362 struct mca_data *mca = cl->host;
363 struct mca_cluster *fe_cl;
365 if (cl->port_driver < 0)
366 return -EINVAL;
369 * We are operating on a foreign cluster here, but since we
370 * belong to the same PCM, accesses should have been
371 * synchronized at ASoC level.
373 fe_cl = &mca->clusters[cl->port_driver];
374 if (!mca_fe_clocks_in_use(fe_cl))
375 return 0; /* Nothing to do */
377 cl->clocks_in_use[substream->stream] = false;
379 if (!mca_fe_clocks_in_use(fe_cl))
380 mca_fe_disable_clocks(fe_cl);
382 return 0;
385 static unsigned int mca_crop_mask(unsigned int mask, int nchans)
387 while (hweight32(mask) > nchans)
388 mask &= ~(1 << __fls(mask));
390 return mask;
393 static int mca_configure_serdes(struct mca_cluster *cl, int serdes_unit,
394 unsigned int mask, int slots, int nchans,
395 int slot_width, bool is_tx, int port)
397 __iomem void *serdes_base = cl->base + serdes_unit;
398 u32 serdes_conf, serdes_conf_mask;
400 serdes_conf_mask = SERDES_CONF_WIDTH_MASK | SERDES_CONF_NCHANS;
401 serdes_conf = FIELD_PREP(SERDES_CONF_NCHANS, max(slots, 1) - 1);
402 switch (slot_width) {
403 case 16:
404 serdes_conf |= SERDES_CONF_WIDTH_16BIT;
405 break;
406 case 20:
407 serdes_conf |= SERDES_CONF_WIDTH_20BIT;
408 break;
409 case 24:
410 serdes_conf |= SERDES_CONF_WIDTH_24BIT;
411 break;
412 case 32:
413 serdes_conf |= SERDES_CONF_WIDTH_32BIT;
414 break;
415 default:
416 goto err;
419 serdes_conf_mask |= SERDES_CONF_SYNC_SEL;
420 serdes_conf |= FIELD_PREP(SERDES_CONF_SYNC_SEL, cl->no + 1);
422 if (is_tx) {
423 serdes_conf_mask |= SERDES_CONF_UNK1 | SERDES_CONF_UNK2 |
424 SERDES_CONF_UNK3;
425 serdes_conf |= SERDES_CONF_UNK1 | SERDES_CONF_UNK2 |
426 SERDES_CONF_UNK3;
427 } else {
428 serdes_conf_mask |= SERDES_CONF_UNK1 | SERDES_CONF_UNK2 |
429 SERDES_CONF_UNK3 |
430 SERDES_CONF_NO_DATA_FEEDBACK;
431 serdes_conf |= SERDES_CONF_UNK1 | SERDES_CONF_UNK2 |
432 SERDES_CONF_NO_DATA_FEEDBACK;
435 mca_modify(cl,
436 serdes_unit +
437 (is_tx ? REG_TX_SERDES_CONF : REG_RX_SERDES_CONF),
438 serdes_conf_mask, serdes_conf);
440 if (is_tx) {
441 writel_relaxed(0xffffffff,
442 serdes_base + REG_TX_SERDES_SLOTMASK);
443 writel_relaxed(~((u32)mca_crop_mask(mask, nchans)),
444 serdes_base + REG_TX_SERDES_SLOTMASK + 0x4);
445 writel_relaxed(0xffffffff,
446 serdes_base + REG_TX_SERDES_SLOTMASK + 0x8);
447 writel_relaxed(~((u32)mask),
448 serdes_base + REG_TX_SERDES_SLOTMASK + 0xc);
449 } else {
450 writel_relaxed(0xffffffff,
451 serdes_base + REG_RX_SERDES_SLOTMASK);
452 writel_relaxed(~((u32)mca_crop_mask(mask, nchans)),
453 serdes_base + REG_RX_SERDES_SLOTMASK + 0x4);
454 writel_relaxed(1 << port,
455 serdes_base + REG_RX_SERDES_PORT);
458 return 0;
460 err:
461 dev_err(cl->host->dev,
462 "unsupported SERDES configuration requested (mask=0x%x slots=%d slot_width=%d)\n",
463 mask, slots, slot_width);
464 return -EINVAL;
467 static int mca_fe_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
468 unsigned int rx_mask, int slots, int slot_width)
470 struct mca_cluster *cl = mca_dai_to_cluster(dai);
472 cl->tdm_slots = slots;
473 cl->tdm_slot_width = slot_width;
474 cl->tdm_tx_mask = tx_mask;
475 cl->tdm_rx_mask = rx_mask;
477 return 0;
480 static int mca_fe_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
482 struct mca_cluster *cl = mca_dai_to_cluster(dai);
483 struct mca_data *mca = cl->host;
484 bool fpol_inv = false;
485 u32 serdes_conf = 0;
486 u32 bitstart;
488 if ((fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) !=
489 SND_SOC_DAIFMT_BP_FP)
490 goto err;
492 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
493 case SND_SOC_DAIFMT_I2S:
494 fpol_inv = 0;
495 bitstart = 1;
496 break;
497 case SND_SOC_DAIFMT_LEFT_J:
498 fpol_inv = 1;
499 bitstart = 0;
500 break;
501 default:
502 goto err;
505 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
506 case SND_SOC_DAIFMT_NB_IF:
507 case SND_SOC_DAIFMT_IB_IF:
508 fpol_inv ^= 1;
509 break;
512 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
513 case SND_SOC_DAIFMT_NB_NF:
514 case SND_SOC_DAIFMT_NB_IF:
515 serdes_conf |= SERDES_CONF_BCLK_POL;
516 break;
519 if (!fpol_inv)
520 goto err;
522 mca_modify(cl, CLUSTER_TX_OFF + REG_TX_SERDES_CONF,
523 SERDES_CONF_BCLK_POL, serdes_conf);
524 mca_modify(cl, CLUSTER_RX_OFF + REG_RX_SERDES_CONF,
525 SERDES_CONF_BCLK_POL, serdes_conf);
526 writel_relaxed(bitstart,
527 cl->base + CLUSTER_TX_OFF + REG_TX_SERDES_BITSTART);
528 writel_relaxed(bitstart,
529 cl->base + CLUSTER_RX_OFF + REG_RX_SERDES_BITSTART);
531 return 0;
533 err:
534 dev_err(mca->dev, "unsupported DAI format (0x%x) requested\n", fmt);
535 return -EINVAL;
538 static int mca_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
540 struct mca_cluster *cl = mca_dai_to_cluster(dai);
542 cl->bclk_ratio = ratio;
544 return 0;
547 static int mca_fe_get_port(struct snd_pcm_substream *substream)
549 struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream);
550 struct snd_soc_pcm_runtime *be;
551 struct snd_soc_dpcm *dpcm;
553 be = NULL;
554 for_each_dpcm_be(fe, substream->stream, dpcm) {
555 be = dpcm->be;
556 break;
559 if (!be)
560 return -EINVAL;
562 return mca_dai_to_cluster(snd_soc_rtd_to_cpu(be, 0))->no;
565 static int mca_fe_hw_params(struct snd_pcm_substream *substream,
566 struct snd_pcm_hw_params *params,
567 struct snd_soc_dai *dai)
569 struct mca_cluster *cl = mca_dai_to_cluster(dai);
570 struct mca_data *mca = cl->host;
571 struct device *dev = mca->dev;
572 unsigned int samp_rate = params_rate(params);
573 bool is_tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
574 bool refine_tdm = false;
575 unsigned long bclk_ratio;
576 unsigned int tdm_slots, tdm_slot_width, tdm_mask;
577 u32 regval, pad;
578 int ret, port, nchans_ceiled;
580 if (!cl->tdm_slot_width) {
582 * We were not given TDM settings from above, set initial
583 * guesses which will later be refined.
585 tdm_slot_width = params_width(params);
586 tdm_slots = params_channels(params);
587 refine_tdm = true;
588 } else {
589 tdm_slot_width = cl->tdm_slot_width;
590 tdm_slots = cl->tdm_slots;
591 tdm_mask = is_tx ? cl->tdm_tx_mask : cl->tdm_rx_mask;
594 if (cl->bclk_ratio)
595 bclk_ratio = cl->bclk_ratio;
596 else
597 bclk_ratio = tdm_slot_width * tdm_slots;
599 if (refine_tdm) {
600 int nchannels = params_channels(params);
602 if (nchannels > 2) {
603 dev_err(dev, "missing TDM for stream with two or more channels\n");
604 return -EINVAL;
607 if ((bclk_ratio % nchannels) != 0) {
608 dev_err(dev, "BCLK ratio (%ld) not divisible by no. of channels (%d)\n",
609 bclk_ratio, nchannels);
610 return -EINVAL;
613 tdm_slot_width = bclk_ratio / nchannels;
615 if (tdm_slot_width > 32 && nchannels == 1)
616 tdm_slot_width = 32;
618 if (tdm_slot_width < params_width(params)) {
619 dev_err(dev, "TDM slots too narrow (tdm=%u params=%d)\n",
620 tdm_slot_width, params_width(params));
621 return -EINVAL;
624 tdm_mask = (1 << tdm_slots) - 1;
627 port = mca_fe_get_port(substream);
628 if (port < 0)
629 return port;
631 ret = mca_configure_serdes(cl, is_tx ? CLUSTER_TX_OFF : CLUSTER_RX_OFF,
632 tdm_mask, tdm_slots, params_channels(params),
633 tdm_slot_width, is_tx, port);
634 if (ret)
635 return ret;
637 pad = 32 - params_width(params);
640 * TODO: Here the register semantics aren't clear.
642 nchans_ceiled = min_t(int, params_channels(params), 4);
643 regval = FIELD_PREP(DMA_ADAPTER_NCHANS, nchans_ceiled) |
644 FIELD_PREP(DMA_ADAPTER_TX_NCHANS, 0x2) |
645 FIELD_PREP(DMA_ADAPTER_RX_NCHANS, 0x2) |
646 FIELD_PREP(DMA_ADAPTER_TX_LSB_PAD, pad) |
647 FIELD_PREP(DMA_ADAPTER_RX_MSB_PAD, pad);
649 #ifndef USE_RXB_FOR_CAPTURE
650 writel_relaxed(regval, mca->switch_base + REG_DMA_ADAPTER_A(cl->no));
651 #else
652 if (is_tx)
653 writel_relaxed(regval,
654 mca->switch_base + REG_DMA_ADAPTER_A(cl->no));
655 else
656 writel_relaxed(regval,
657 mca->switch_base + REG_DMA_ADAPTER_B(cl->no));
658 #endif
660 if (!mca_fe_clocks_in_use(cl)) {
662 * Set up FSYNC duty cycle as even as possible.
664 writel_relaxed((bclk_ratio / 2) - 1,
665 cl->base + REG_SYNCGEN_HI_PERIOD);
666 writel_relaxed(((bclk_ratio + 1) / 2) - 1,
667 cl->base + REG_SYNCGEN_LO_PERIOD);
668 writel_relaxed(FIELD_PREP(MCLK_CONF_DIV, 0x1),
669 cl->base + REG_MCLK_CONF);
671 ret = clk_set_rate(cl->clk_parent, bclk_ratio * samp_rate);
672 if (ret) {
673 dev_err(mca->dev, "cluster %d: unable to set clock parent: %d\n",
674 cl->no, ret);
675 return ret;
679 return 0;
682 static const struct snd_soc_dai_ops mca_fe_ops = {
683 .set_fmt = mca_fe_set_fmt,
684 .set_bclk_ratio = mca_set_bclk_ratio,
685 .set_tdm_slot = mca_fe_set_tdm_slot,
686 .hw_params = mca_fe_hw_params,
687 .trigger = mca_fe_trigger,
690 static bool mca_be_started(struct mca_cluster *cl)
692 int stream;
694 for_each_pcm_streams(stream)
695 if (cl->port_started[stream])
696 return true;
697 return false;
700 static int mca_be_startup(struct snd_pcm_substream *substream,
701 struct snd_soc_dai *dai)
703 struct snd_soc_pcm_runtime *be = snd_soc_substream_to_rtd(substream);
704 struct snd_soc_pcm_runtime *fe;
705 struct mca_cluster *cl = mca_dai_to_cluster(dai);
706 struct mca_cluster *fe_cl;
707 struct mca_data *mca = cl->host;
708 struct snd_soc_dpcm *dpcm;
710 fe = NULL;
712 for_each_dpcm_fe(be, substream->stream, dpcm) {
713 if (fe && dpcm->fe != fe) {
714 dev_err(mca->dev, "many FE per one BE unsupported\n");
715 return -EINVAL;
718 fe = dpcm->fe;
721 if (!fe)
722 return -EINVAL;
724 fe_cl = mca_dai_to_cluster(snd_soc_rtd_to_cpu(fe, 0));
726 if (mca_be_started(cl)) {
728 * Port is already started in the other direction.
729 * Make sure there isn't a conflict with another cluster
730 * driving the port.
732 if (cl->port_driver != fe_cl->no)
733 return -EINVAL;
735 cl->port_started[substream->stream] = true;
736 return 0;
739 writel_relaxed(PORT_ENABLES_CLOCKS | PORT_ENABLES_TX_DATA,
740 cl->base + REG_PORT_ENABLES);
741 writel_relaxed(FIELD_PREP(PORT_CLOCK_SEL, fe_cl->no + 1),
742 cl->base + REG_PORT_CLOCK_SEL);
743 writel_relaxed(PORT_DATA_SEL_TXA(fe_cl->no),
744 cl->base + REG_PORT_DATA_SEL);
745 mutex_lock(&mca->port_mutex);
746 cl->port_driver = fe_cl->no;
747 mutex_unlock(&mca->port_mutex);
748 cl->port_started[substream->stream] = true;
750 return 0;
753 static void mca_be_shutdown(struct snd_pcm_substream *substream,
754 struct snd_soc_dai *dai)
756 struct mca_cluster *cl = mca_dai_to_cluster(dai);
757 struct mca_data *mca = cl->host;
759 cl->port_started[substream->stream] = false;
761 if (!mca_be_started(cl)) {
763 * Were we the last direction to shutdown?
764 * Turn off the lights.
766 writel_relaxed(0, cl->base + REG_PORT_ENABLES);
767 writel_relaxed(0, cl->base + REG_PORT_DATA_SEL);
768 mutex_lock(&mca->port_mutex);
769 cl->port_driver = -1;
770 mutex_unlock(&mca->port_mutex);
774 static const struct snd_soc_dai_ops mca_be_ops = {
775 .prepare = mca_be_prepare,
776 .hw_free = mca_be_hw_free,
777 .startup = mca_be_startup,
778 .shutdown = mca_be_shutdown,
781 static int mca_set_runtime_hwparams(struct snd_soc_component *component,
782 struct snd_pcm_substream *substream,
783 struct dma_chan *chan)
785 struct device *dma_dev = chan->device->dev;
786 struct snd_dmaengine_dai_dma_data dma_data = {};
787 int ret;
789 struct snd_pcm_hardware hw;
791 memset(&hw, 0, sizeof(hw));
793 hw.info = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
794 SNDRV_PCM_INFO_INTERLEAVED;
795 hw.periods_min = 2;
796 hw.periods_max = UINT_MAX;
797 hw.period_bytes_min = 256;
798 hw.period_bytes_max = dma_get_max_seg_size(dma_dev);
799 hw.buffer_bytes_max = SIZE_MAX;
800 hw.fifo_size = 16;
802 ret = snd_dmaengine_pcm_refine_runtime_hwparams(substream, &dma_data,
803 &hw, chan);
805 if (ret)
806 return ret;
808 return snd_soc_set_runtime_hwparams(substream, &hw);
811 static int mca_pcm_open(struct snd_soc_component *component,
812 struct snd_pcm_substream *substream)
814 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
815 struct mca_cluster *cl = mca_dai_to_cluster(snd_soc_rtd_to_cpu(rtd, 0));
816 struct dma_chan *chan = cl->dma_chans[substream->stream];
817 int ret;
819 if (rtd->dai_link->no_pcm)
820 return 0;
822 ret = mca_set_runtime_hwparams(component, substream, chan);
823 if (ret)
824 return ret;
826 return snd_dmaengine_pcm_open(substream, chan);
829 static int mca_hw_params(struct snd_soc_component *component,
830 struct snd_pcm_substream *substream,
831 struct snd_pcm_hw_params *params)
833 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
834 struct dma_chan *chan = snd_dmaengine_pcm_get_chan(substream);
835 struct dma_slave_config slave_config;
836 int ret;
838 if (rtd->dai_link->no_pcm)
839 return 0;
841 memset(&slave_config, 0, sizeof(slave_config));
842 ret = snd_hwparams_to_dma_slave_config(substream, params,
843 &slave_config);
844 if (ret < 0)
845 return ret;
847 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
848 slave_config.dst_port_window_size =
849 min_t(u32, params_channels(params), 4);
850 else
851 slave_config.src_port_window_size =
852 min_t(u32, params_channels(params), 4);
854 return dmaengine_slave_config(chan, &slave_config);
857 static int mca_close(struct snd_soc_component *component,
858 struct snd_pcm_substream *substream)
860 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
862 if (rtd->dai_link->no_pcm)
863 return 0;
865 return snd_dmaengine_pcm_close(substream);
868 static int mca_trigger(struct snd_soc_component *component,
869 struct snd_pcm_substream *substream, int cmd)
871 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
873 if (rtd->dai_link->no_pcm)
874 return 0;
877 * Before we do the PCM trigger proper, insert an opportunity
878 * to reset the frontend's SERDES.
880 mca_fe_early_trigger(substream, cmd, snd_soc_rtd_to_cpu(rtd, 0));
882 return snd_dmaengine_pcm_trigger(substream, cmd);
885 static snd_pcm_uframes_t mca_pointer(struct snd_soc_component *component,
886 struct snd_pcm_substream *substream)
888 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
890 if (rtd->dai_link->no_pcm)
891 return -ENOTSUPP;
893 return snd_dmaengine_pcm_pointer(substream);
896 static struct dma_chan *mca_request_dma_channel(struct mca_cluster *cl, unsigned int stream)
898 bool is_tx = (stream == SNDRV_PCM_STREAM_PLAYBACK);
899 #ifndef USE_RXB_FOR_CAPTURE
900 char *name = devm_kasprintf(cl->host->dev, GFP_KERNEL,
901 is_tx ? "tx%da" : "rx%da", cl->no);
902 #else
903 char *name = devm_kasprintf(cl->host->dev, GFP_KERNEL,
904 is_tx ? "tx%da" : "rx%db", cl->no);
905 #endif
906 return of_dma_request_slave_channel(cl->host->dev->of_node, name);
910 static void mca_pcm_free(struct snd_soc_component *component,
911 struct snd_pcm *pcm)
913 struct snd_soc_pcm_runtime *rtd = snd_pcm_chip(pcm);
914 struct mca_cluster *cl = mca_dai_to_cluster(snd_soc_rtd_to_cpu(rtd, 0));
915 unsigned int i;
917 if (rtd->dai_link->no_pcm)
918 return;
920 for_each_pcm_streams(i) {
921 struct snd_pcm_substream *substream =
922 rtd->pcm->streams[i].substream;
924 if (!substream || !cl->dma_chans[i])
925 continue;
927 dma_release_channel(cl->dma_chans[i]);
928 cl->dma_chans[i] = NULL;
933 static int mca_pcm_new(struct snd_soc_component *component,
934 struct snd_soc_pcm_runtime *rtd)
936 struct mca_cluster *cl = mca_dai_to_cluster(snd_soc_rtd_to_cpu(rtd, 0));
937 unsigned int i;
939 if (rtd->dai_link->no_pcm)
940 return 0;
942 for_each_pcm_streams(i) {
943 struct snd_pcm_substream *substream =
944 rtd->pcm->streams[i].substream;
945 struct dma_chan *chan;
947 if (!substream)
948 continue;
950 chan = mca_request_dma_channel(cl, i);
952 if (IS_ERR_OR_NULL(chan)) {
953 mca_pcm_free(component, rtd->pcm);
955 if (chan && PTR_ERR(chan) == -EPROBE_DEFER)
956 return PTR_ERR(chan);
958 dev_err(component->dev, "unable to obtain DMA channel (stream %d cluster %d): %pe\n",
959 i, cl->no, chan);
961 if (!chan)
962 return -EINVAL;
963 return PTR_ERR(chan);
966 cl->dma_chans[i] = chan;
967 snd_pcm_set_managed_buffer(substream, SNDRV_DMA_TYPE_DEV_IRAM,
968 chan->device->dev, 512 * 1024 * 6,
969 SIZE_MAX);
972 return 0;
975 static const struct snd_soc_component_driver mca_component = {
976 .name = "apple-mca",
977 .open = mca_pcm_open,
978 .close = mca_close,
979 .hw_params = mca_hw_params,
980 .trigger = mca_trigger,
981 .pointer = mca_pointer,
982 .pcm_construct = mca_pcm_new,
983 .pcm_destruct = mca_pcm_free,
986 static void apple_mca_release(struct mca_data *mca)
988 int i;
990 for (i = 0; i < mca->nclusters; i++) {
991 struct mca_cluster *cl = &mca->clusters[i];
993 if (!IS_ERR_OR_NULL(cl->clk_parent))
994 clk_put(cl->clk_parent);
996 if (!IS_ERR_OR_NULL(cl->pd_dev))
997 dev_pm_domain_detach(cl->pd_dev, true);
1000 if (mca->pd_link)
1001 device_link_del(mca->pd_link);
1003 if (!IS_ERR_OR_NULL(mca->pd_dev))
1004 dev_pm_domain_detach(mca->pd_dev, true);
1006 reset_control_rearm(mca->rstc);
1009 static int apple_mca_probe(struct platform_device *pdev)
1011 struct mca_data *mca;
1012 struct mca_cluster *clusters;
1013 struct snd_soc_dai_driver *dai_drivers;
1014 struct resource *res;
1015 void __iomem *base;
1016 int nclusters;
1017 int ret, i;
1019 base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
1020 if (IS_ERR(base))
1021 return PTR_ERR(base);
1023 if (resource_size(res) < CLUSTER_STRIDE)
1024 return -EINVAL;
1025 nclusters = (resource_size(res) - CLUSTER_STRIDE) / CLUSTER_STRIDE + 1;
1027 mca = devm_kzalloc(&pdev->dev, struct_size(mca, clusters, nclusters),
1028 GFP_KERNEL);
1029 if (!mca)
1030 return -ENOMEM;
1031 mca->dev = &pdev->dev;
1032 mca->nclusters = nclusters;
1033 mutex_init(&mca->port_mutex);
1034 platform_set_drvdata(pdev, mca);
1035 clusters = mca->clusters;
1037 mca->switch_base =
1038 devm_platform_ioremap_resource(pdev, 1);
1039 if (IS_ERR(mca->switch_base))
1040 return PTR_ERR(mca->switch_base);
1042 mca->rstc = devm_reset_control_get_optional_shared(&pdev->dev, NULL);
1043 if (IS_ERR(mca->rstc))
1044 return PTR_ERR(mca->rstc);
1046 dai_drivers = devm_kzalloc(
1047 &pdev->dev, sizeof(*dai_drivers) * 2 * nclusters, GFP_KERNEL);
1048 if (!dai_drivers)
1049 return -ENOMEM;
1051 mca->pd_dev = dev_pm_domain_attach_by_id(&pdev->dev, 0);
1052 if (IS_ERR(mca->pd_dev))
1053 return -EINVAL;
1055 mca->pd_link = device_link_add(&pdev->dev, mca->pd_dev,
1056 DL_FLAG_STATELESS | DL_FLAG_PM_RUNTIME |
1057 DL_FLAG_RPM_ACTIVE);
1058 if (!mca->pd_link) {
1059 ret = -EINVAL;
1060 /* Prevent an unbalanced reset rearm */
1061 mca->rstc = NULL;
1062 goto err_release;
1065 reset_control_reset(mca->rstc);
1067 for (i = 0; i < nclusters; i++) {
1068 struct mca_cluster *cl = &clusters[i];
1069 struct snd_soc_dai_driver *fe =
1070 &dai_drivers[mca->nclusters + i];
1071 struct snd_soc_dai_driver *be = &dai_drivers[i];
1073 cl->host = mca;
1074 cl->no = i;
1075 cl->base = base + CLUSTER_STRIDE * i;
1076 cl->port_driver = -1;
1077 cl->clk_parent = of_clk_get(pdev->dev.of_node, i);
1078 if (IS_ERR(cl->clk_parent)) {
1079 dev_err(&pdev->dev, "unable to obtain clock %d: %ld\n",
1080 i, PTR_ERR(cl->clk_parent));
1081 ret = PTR_ERR(cl->clk_parent);
1082 goto err_release;
1084 cl->pd_dev = dev_pm_domain_attach_by_id(&pdev->dev, i + 1);
1085 if (IS_ERR(cl->pd_dev)) {
1086 dev_err(&pdev->dev,
1087 "unable to obtain cluster %d PD: %ld\n", i,
1088 PTR_ERR(cl->pd_dev));
1089 ret = PTR_ERR(cl->pd_dev);
1090 goto err_release;
1093 fe->id = i;
1094 fe->name =
1095 devm_kasprintf(&pdev->dev, GFP_KERNEL, "mca-pcm-%d", i);
1096 if (!fe->name) {
1097 ret = -ENOMEM;
1098 goto err_release;
1100 fe->ops = &mca_fe_ops;
1101 fe->playback.channels_min = 1;
1102 fe->playback.channels_max = 32;
1103 fe->playback.rates = SNDRV_PCM_RATE_8000_192000;
1104 fe->playback.formats = APPLE_MCA_FMTBITS;
1105 fe->capture.channels_min = 1;
1106 fe->capture.channels_max = 32;
1107 fe->capture.rates = SNDRV_PCM_RATE_8000_192000;
1108 fe->capture.formats = APPLE_MCA_FMTBITS;
1109 fe->symmetric_rate = 1;
1111 fe->playback.stream_name =
1112 devm_kasprintf(&pdev->dev, GFP_KERNEL, "PCM%d TX", i);
1113 fe->capture.stream_name =
1114 devm_kasprintf(&pdev->dev, GFP_KERNEL, "PCM%d RX", i);
1116 if (!fe->playback.stream_name || !fe->capture.stream_name) {
1117 ret = -ENOMEM;
1118 goto err_release;
1121 be->id = i + nclusters;
1122 be->name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "mca-i2s-%d", i);
1123 if (!be->name) {
1124 ret = -ENOMEM;
1125 goto err_release;
1127 be->ops = &mca_be_ops;
1128 be->playback.channels_min = 1;
1129 be->playback.channels_max = 32;
1130 be->playback.rates = SNDRV_PCM_RATE_8000_192000;
1131 be->playback.formats = APPLE_MCA_FMTBITS;
1132 be->capture.channels_min = 1;
1133 be->capture.channels_max = 32;
1134 be->capture.rates = SNDRV_PCM_RATE_8000_192000;
1135 be->capture.formats = APPLE_MCA_FMTBITS;
1137 be->playback.stream_name =
1138 devm_kasprintf(&pdev->dev, GFP_KERNEL, "I2S%d TX", i);
1139 be->capture.stream_name =
1140 devm_kasprintf(&pdev->dev, GFP_KERNEL, "I2S%d RX", i);
1141 if (!be->playback.stream_name || !be->capture.stream_name) {
1142 ret = -ENOMEM;
1143 goto err_release;
1147 ret = snd_soc_register_component(&pdev->dev, &mca_component,
1148 dai_drivers, nclusters * 2);
1149 if (ret) {
1150 dev_err(&pdev->dev, "unable to register ASoC component: %d\n",
1151 ret);
1152 goto err_release;
1155 return 0;
1157 err_release:
1158 apple_mca_release(mca);
1159 return ret;
1162 static void apple_mca_remove(struct platform_device *pdev)
1164 struct mca_data *mca = platform_get_drvdata(pdev);
1166 snd_soc_unregister_component(&pdev->dev);
1167 apple_mca_release(mca);
1170 static const struct of_device_id apple_mca_of_match[] = {
1171 { .compatible = "apple,mca", },
1174 MODULE_DEVICE_TABLE(of, apple_mca_of_match);
1176 static struct platform_driver apple_mca_driver = {
1177 .driver = {
1178 .name = "apple-mca",
1179 .of_match_table = apple_mca_of_match,
1181 .probe = apple_mca_probe,
1182 .remove = apple_mca_remove,
1184 module_platform_driver(apple_mca_driver);
1186 MODULE_AUTHOR("Martin PoviĊĦer <povik+lin@cutebit.org>");
1187 MODULE_DESCRIPTION("ASoC Apple MCA driver");
1188 MODULE_LICENSE("GPL");