1261 lines · c
1// SPDX-License-Identifier: GPL-2.02//3// Renesas R-Car SSIU/SSI support4//5// Copyright (C) 2013 Renesas Solutions Corp.6// Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>7//8// Based on fsi.c9// Kuninori Morimoto <morimoto.kuninori@renesas.com>10 11/*12 * you can enable below define if you don't need13 * SSI interrupt status debug message when debugging14 * see rsnd_print_irq_status()15 *16 * #define RSND_DEBUG_NO_IRQ_STATUS 117 */18 19#include <sound/simple_card_utils.h>20#include <linux/of.h>21#include <linux/of_irq.h>22#include <linux/delay.h>23#include "rsnd.h"24#define RSND_SSI_NAME_SIZE 1625 26/*27 * SSICR28 */29#define FORCE (1u << 31) /* Fixed */30#define DMEN (1u << 28) /* DMA Enable */31#define UIEN (1u << 27) /* Underflow Interrupt Enable */32#define OIEN (1u << 26) /* Overflow Interrupt Enable */33#define IIEN (1u << 25) /* Idle Mode Interrupt Enable */34#define DIEN (1u << 24) /* Data Interrupt Enable */35#define CHNL_4 (1u << 22) /* Channels */36#define CHNL_6 (2u << 22) /* Channels */37#define CHNL_8 (3u << 22) /* Channels */38#define DWL_MASK (7u << 19) /* Data Word Length mask */39#define DWL_8 (0u << 19) /* Data Word Length */40#define DWL_16 (1u << 19) /* Data Word Length */41#define DWL_18 (2u << 19) /* Data Word Length */42#define DWL_20 (3u << 19) /* Data Word Length */43#define DWL_22 (4u << 19) /* Data Word Length */44#define DWL_24 (5u << 19) /* Data Word Length */45#define DWL_32 (6u << 19) /* Data Word Length */46 47/*48 * System word length49 */50#define SWL_16 (1 << 16) /* R/W System Word Length */51#define SWL_24 (2 << 16) /* R/W System Word Length */52#define SWL_32 (3 << 16) /* R/W System Word Length */53 54#define SCKD (1 << 15) /* Serial Bit Clock Direction */55#define SWSD (1 << 14) /* Serial WS Direction */56#define SCKP (1 << 13) /* Serial Bit Clock Polarity */57#define SWSP (1 << 12) /* Serial WS Polarity */58#define SDTA (1 << 10) /* Serial Data Alignment */59#define PDTA (1 << 9) /* Parallel Data Alignment */60#define DEL (1 << 8) /* Serial Data Delay */61#define CKDV(v) (v << 4) /* Serial Clock Division Ratio */62#define TRMD (1 << 1) /* Transmit/Receive Mode Select */63#define EN (1 << 0) /* SSI Module Enable */64 65/*66 * SSISR67 */68#define UIRQ (1 << 27) /* Underflow Error Interrupt Status */69#define OIRQ (1 << 26) /* Overflow Error Interrupt Status */70#define IIRQ (1 << 25) /* Idle Mode Interrupt Status */71#define DIRQ (1 << 24) /* Data Interrupt Status Flag */72 73/*74 * SSIWSR75 */76#define CONT (1 << 8) /* WS Continue Function */77#define WS_MODE (1 << 0) /* WS Mode */78 79#define SSI_NAME "ssi"80 81struct rsnd_ssi {82 struct rsnd_mod mod;83 84 u32 flags;85 u32 cr_own;86 u32 cr_clk;87 u32 cr_mode;88 u32 cr_en;89 u32 wsr;90 int chan;91 int rate;92 int irq;93 unsigned int usrcnt;94 95 /* for PIO */96 int byte_pos;97 int byte_per_period;98 int next_period_byte;99};100 101/* flags */102#define RSND_SSI_CLK_PIN_SHARE (1 << 0)103#define RSND_SSI_NO_BUSIF (1 << 1) /* SSI+DMA without BUSIF */104#define RSND_SSI_PROBED (1 << 2)105 106#define for_each_rsnd_ssi(pos, priv, i) \107 for (i = 0; \108 (i < rsnd_ssi_nr(priv)) && \109 ((pos) = ((struct rsnd_ssi *)(priv)->ssi + i)); \110 i++)111 112#define rsnd_ssi_get(priv, id) ((struct rsnd_ssi *)(priv->ssi) + id)113#define rsnd_ssi_nr(priv) ((priv)->ssi_nr)114#define rsnd_mod_to_ssi(_mod) container_of((_mod), struct rsnd_ssi, mod)115#define rsnd_ssi_is_parent(ssi, io) ((ssi) == rsnd_io_to_mod_ssip(io))116#define rsnd_ssi_is_multi_secondary(mod, io) \117 (rsnd_ssi_multi_secondaries(io) & (1 << rsnd_mod_id(mod)))118#define rsnd_ssi_is_run_mods(mod, io) \119 (rsnd_ssi_run_mods(io) & (1 << rsnd_mod_id(mod)))120#define rsnd_ssi_can_output_clk(mod) (!__rsnd_ssi_is_pin_sharing(mod))121 122int rsnd_ssi_use_busif(struct rsnd_dai_stream *io)123{124 struct rsnd_mod *mod = rsnd_io_to_mod_ssi(io);125 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);126 int use_busif = 0;127 128 if (!rsnd_ssi_is_dma_mode(mod))129 return 0;130 131 if (!(rsnd_flags_has(ssi, RSND_SSI_NO_BUSIF)))132 use_busif = 1;133 if (rsnd_io_to_mod_src(io))134 use_busif = 1;135 136 return use_busif;137}138 139static void rsnd_ssi_status_clear(struct rsnd_mod *mod)140{141 rsnd_mod_write(mod, SSISR, 0);142}143 144static u32 rsnd_ssi_status_get(struct rsnd_mod *mod)145{146 return rsnd_mod_read(mod, SSISR);147}148 149static void rsnd_ssi_status_check(struct rsnd_mod *mod,150 u32 bit)151{152 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);153 struct device *dev = rsnd_priv_to_dev(priv);154 u32 status;155 int i;156 157 for (i = 0; i < 1024; i++) {158 status = rsnd_ssi_status_get(mod);159 if (status & bit)160 return;161 162 udelay(5);163 }164 165 dev_warn(dev, "%s status check failed\n", rsnd_mod_name(mod));166}167 168static u32 rsnd_ssi_multi_secondaries(struct rsnd_dai_stream *io)169{170 static const enum rsnd_mod_type types[] = {171 RSND_MOD_SSIM1,172 RSND_MOD_SSIM2,173 RSND_MOD_SSIM3,174 };175 int i, mask;176 177 mask = 0;178 for (i = 0; i < ARRAY_SIZE(types); i++) {179 struct rsnd_mod *mod = rsnd_io_to_mod(io, types[i]);180 181 if (!mod)182 continue;183 184 mask |= 1 << rsnd_mod_id(mod);185 }186 187 return mask;188}189 190static u32 rsnd_ssi_run_mods(struct rsnd_dai_stream *io)191{192 struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);193 struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io);194 u32 mods;195 196 mods = rsnd_ssi_multi_secondaries_runtime(io) |197 1 << rsnd_mod_id(ssi_mod);198 199 if (ssi_parent_mod)200 mods |= 1 << rsnd_mod_id(ssi_parent_mod);201 202 return mods;203}204 205u32 rsnd_ssi_multi_secondaries_runtime(struct rsnd_dai_stream *io)206{207 if (rsnd_runtime_is_multi_ssi(io))208 return rsnd_ssi_multi_secondaries(io);209 210 return 0;211}212 213static u32 rsnd_rdai_width_to_swl(struct rsnd_dai *rdai)214{215 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);216 struct device *dev = rsnd_priv_to_dev(priv);217 int width = rsnd_rdai_width_get(rdai);218 219 switch (width) {220 case 32: return SWL_32;221 case 24: return SWL_24;222 case 16: return SWL_16;223 }224 225 dev_err(dev, "unsupported slot width value: %d\n", width);226 return 0;227}228 229unsigned int rsnd_ssi_clk_query(struct rsnd_dai *rdai,230 int param1, int param2, int *idx)231{232 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);233 static const int ssi_clk_mul_table[] = {234 1, 2, 4, 8, 16, 6, 12,235 };236 int j, ret;237 unsigned int main_rate;238 int width = rsnd_rdai_width_get(rdai);239 240 for (j = 0; j < ARRAY_SIZE(ssi_clk_mul_table); j++) {241 242 /*243 * It will set SSIWSR.CONT here, but SSICR.CKDV = 000244 * with it is not allowed. (SSIWSR.WS_MODE with245 * SSICR.CKDV = 000 is not allowed either).246 * Skip it. See SSICR.CKDV247 */248 if (j == 0)249 continue;250 251 main_rate = width * param1 * param2 * ssi_clk_mul_table[j];252 253 ret = rsnd_adg_clk_query(priv, main_rate);254 if (ret < 0)255 continue;256 257 if (idx)258 *idx = j;259 260 return main_rate;261 }262 263 return 0;264}265 266static int rsnd_ssi_master_clk_start(struct rsnd_mod *mod,267 struct rsnd_dai_stream *io)268{269 struct rsnd_priv *priv = rsnd_io_to_priv(io);270 struct device *dev = rsnd_priv_to_dev(priv);271 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);272 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);273 int chan = rsnd_runtime_channel_for_ssi(io);274 int idx, ret;275 unsigned int main_rate;276 unsigned int rate = rsnd_io_is_play(io) ?277 rsnd_src_get_out_rate(priv, io) :278 rsnd_src_get_in_rate(priv, io);279 280 if (!rsnd_rdai_is_clk_master(rdai))281 return 0;282 283 if (!rsnd_ssi_can_output_clk(mod))284 return 0;285 286 if (rsnd_ssi_is_multi_secondary(mod, io))287 return 0;288 289 if (rsnd_runtime_is_tdm_split(io))290 chan = rsnd_io_converted_chan(io);291 292 chan = rsnd_channel_normalization(chan);293 294 if (ssi->usrcnt > 0) {295 if (ssi->rate != rate) {296 dev_err(dev, "SSI parent/child should use same rate\n");297 return -EINVAL;298 }299 300 if (ssi->chan != chan) {301 dev_err(dev, "SSI parent/child should use same chan\n");302 return -EINVAL;303 }304 305 return 0;306 }307 308 ret = -EIO;309 main_rate = rsnd_ssi_clk_query(rdai, rate, chan, &idx);310 if (!main_rate)311 goto rate_err;312 313 ret = rsnd_adg_ssi_clk_try_start(mod, main_rate);314 if (ret < 0)315 goto rate_err;316 317 /*318 * SSI clock will be output contiguously319 * by below settings.320 * This means, rsnd_ssi_master_clk_start()321 * and rsnd_ssi_register_setup() are necessary322 * for SSI parent323 *324 * SSICR : FORCE, SCKD, SWSD325 * SSIWSR : CONT326 */327 ssi->cr_clk = FORCE | rsnd_rdai_width_to_swl(rdai) |328 SCKD | SWSD | CKDV(idx);329 ssi->wsr = CONT;330 ssi->rate = rate;331 ssi->chan = chan;332 333 dev_dbg(dev, "%s outputs %d chan %u Hz\n",334 rsnd_mod_name(mod), chan, rate);335 336 return 0;337 338rate_err:339 dev_err(dev, "unsupported clock rate\n");340 return ret;341}342 343static void rsnd_ssi_master_clk_stop(struct rsnd_mod *mod,344 struct rsnd_dai_stream *io)345{346 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);347 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);348 349 if (!rsnd_rdai_is_clk_master(rdai))350 return;351 352 if (!rsnd_ssi_can_output_clk(mod))353 return;354 355 if (ssi->usrcnt > 1)356 return;357 358 ssi->cr_clk = 0;359 ssi->rate = 0;360 ssi->chan = 0;361 362 rsnd_adg_ssi_clk_stop(mod);363}364 365static void rsnd_ssi_config_init(struct rsnd_mod *mod,366 struct rsnd_dai_stream *io)367{368 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);369 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);370 struct device *dev = rsnd_priv_to_dev(priv);371 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);372 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);373 u32 cr_own = ssi->cr_own;374 u32 cr_mode = ssi->cr_mode;375 u32 wsr = ssi->wsr;376 int width;377 int is_tdm, is_tdm_split;378 379 is_tdm = rsnd_runtime_is_tdm(io);380 is_tdm_split = rsnd_runtime_is_tdm_split(io);381 382 if (is_tdm)383 dev_dbg(dev, "TDM mode\n");384 if (is_tdm_split)385 dev_dbg(dev, "TDM Split mode\n");386 387 cr_own |= FORCE | rsnd_rdai_width_to_swl(rdai);388 389 if (rdai->bit_clk_inv)390 cr_own |= SCKP;391 if (rdai->frm_clk_inv && !is_tdm)392 cr_own |= SWSP;393 if (rdai->data_alignment)394 cr_own |= SDTA;395 if (rdai->sys_delay)396 cr_own |= DEL;397 398 /*399 * TDM Mode400 * see401 * rsnd_ssiu_init_gen2()402 */403 if (is_tdm || is_tdm_split) {404 wsr |= WS_MODE;405 cr_own |= CHNL_8;406 }407 408 /*409 * We shouldn't exchange SWSP after running.410 * This means, parent needs to care it.411 */412 if (rsnd_ssi_is_parent(mod, io))413 goto init_end;414 415 if (rsnd_io_is_play(io))416 cr_own |= TRMD;417 418 cr_own &= ~DWL_MASK;419 width = snd_pcm_format_width(runtime->format);420 if (is_tdm_split) {421 /*422 * The SWL and DWL bits in SSICR should be fixed at 32-bit423 * setting when TDM split mode.424 * see datasheet425 * Operation :: TDM Format Split Function (TDM Split Mode)426 */427 width = 32;428 }429 430 switch (width) {431 case 8:432 cr_own |= DWL_8;433 break;434 case 16:435 cr_own |= DWL_16;436 break;437 case 24:438 cr_own |= DWL_24;439 break;440 case 32:441 cr_own |= DWL_32;442 break;443 }444 445 if (rsnd_ssi_is_dma_mode(mod)) {446 cr_mode = UIEN | OIEN | /* over/under run */447 DMEN; /* DMA : enable DMA */448 } else {449 cr_mode = DIEN; /* PIO : enable Data interrupt */450 }451 452init_end:453 ssi->cr_own = cr_own;454 ssi->cr_mode = cr_mode;455 ssi->wsr = wsr;456}457 458static void rsnd_ssi_register_setup(struct rsnd_mod *mod)459{460 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);461 462 rsnd_mod_write(mod, SSIWSR, ssi->wsr);463 rsnd_mod_write(mod, SSICR, ssi->cr_own |464 ssi->cr_clk |465 ssi->cr_mode |466 ssi->cr_en);467}468 469/*470 * SSI mod common functions471 */472static int rsnd_ssi_init(struct rsnd_mod *mod,473 struct rsnd_dai_stream *io,474 struct rsnd_priv *priv)475{476 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);477 int ret;478 479 if (!rsnd_ssi_is_run_mods(mod, io))480 return 0;481 482 ret = rsnd_ssi_master_clk_start(mod, io);483 if (ret < 0)484 return ret;485 486 ssi->usrcnt++;487 488 ret = rsnd_mod_power_on(mod);489 if (ret < 0)490 return ret;491 492 rsnd_ssi_config_init(mod, io);493 494 rsnd_ssi_register_setup(mod);495 496 /* clear error status */497 rsnd_ssi_status_clear(mod);498 499 return 0;500}501 502static int rsnd_ssi_quit(struct rsnd_mod *mod,503 struct rsnd_dai_stream *io,504 struct rsnd_priv *priv)505{506 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);507 struct device *dev = rsnd_priv_to_dev(priv);508 509 if (!rsnd_ssi_is_run_mods(mod, io))510 return 0;511 512 if (!ssi->usrcnt) {513 dev_err(dev, "%s usrcnt error\n", rsnd_mod_name(mod));514 return -EIO;515 }516 517 rsnd_ssi_master_clk_stop(mod, io);518 519 rsnd_mod_power_off(mod);520 521 ssi->usrcnt--;522 523 if (!ssi->usrcnt) {524 ssi->cr_own = 0;525 ssi->cr_mode = 0;526 ssi->wsr = 0;527 }528 529 return 0;530}531 532static int rsnd_ssi_hw_params(struct rsnd_mod *mod,533 struct rsnd_dai_stream *io,534 struct snd_pcm_substream *substream,535 struct snd_pcm_hw_params *params)536{537 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);538 unsigned int fmt_width = snd_pcm_format_width(params_format(params));539 540 if (fmt_width > rdai->chan_width) {541 struct rsnd_priv *priv = rsnd_io_to_priv(io);542 struct device *dev = rsnd_priv_to_dev(priv);543 544 dev_err(dev, "invalid combination of slot-width and format-data-width\n");545 return -EINVAL;546 }547 548 return 0;549}550 551static int rsnd_ssi_start(struct rsnd_mod *mod,552 struct rsnd_dai_stream *io,553 struct rsnd_priv *priv)554{555 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);556 557 if (!rsnd_ssi_is_run_mods(mod, io))558 return 0;559 560 /*561 * EN will be set via SSIU :: SSI_CONTROL562 * if Multi channel mode563 */564 if (rsnd_ssi_multi_secondaries_runtime(io))565 return 0;566 567 /*568 * EN is for data output.569 * SSI parent EN is not needed.570 */571 if (rsnd_ssi_is_parent(mod, io))572 return 0;573 574 ssi->cr_en = EN;575 576 rsnd_mod_write(mod, SSICR, ssi->cr_own |577 ssi->cr_clk |578 ssi->cr_mode |579 ssi->cr_en);580 581 return 0;582}583 584static int rsnd_ssi_stop(struct rsnd_mod *mod,585 struct rsnd_dai_stream *io,586 struct rsnd_priv *priv)587{588 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);589 u32 cr;590 591 if (!rsnd_ssi_is_run_mods(mod, io))592 return 0;593 594 if (rsnd_ssi_is_parent(mod, io))595 return 0;596 597 cr = ssi->cr_own |598 ssi->cr_clk;599 600 /*601 * disable all IRQ,602 * Playback: Wait all data was sent603 * Capture: It might not receave data. Do nothing604 */605 if (rsnd_io_is_play(io)) {606 rsnd_mod_write(mod, SSICR, cr | ssi->cr_en);607 rsnd_ssi_status_check(mod, DIRQ);608 }609 610 /* In multi-SSI mode, stop is performed by setting ssi0129 in611 * SSI_CONTROL to 0 (in rsnd_ssio_stop_gen2). Do nothing here.612 */613 if (rsnd_ssi_multi_secondaries_runtime(io))614 return 0;615 616 /*617 * disable SSI,618 * and, wait idle state619 */620 rsnd_mod_write(mod, SSICR, cr); /* disabled all */621 rsnd_ssi_status_check(mod, IIRQ);622 623 ssi->cr_en = 0;624 625 return 0;626}627 628static int rsnd_ssi_irq(struct rsnd_mod *mod,629 struct rsnd_dai_stream *io,630 struct rsnd_priv *priv,631 int enable)632{633 u32 val = 0;634 int is_tdm, is_tdm_split;635 int id = rsnd_mod_id(mod);636 637 is_tdm = rsnd_runtime_is_tdm(io);638 is_tdm_split = rsnd_runtime_is_tdm_split(io);639 640 if (rsnd_is_gen1(priv))641 return 0;642 643 if (rsnd_ssi_is_parent(mod, io))644 return 0;645 646 if (!rsnd_ssi_is_run_mods(mod, io))647 return 0;648 649 if (enable)650 val = rsnd_ssi_is_dma_mode(mod) ? 0x0e000000 : 0x0f000000;651 652 if (is_tdm || is_tdm_split) {653 switch (id) {654 case 0:655 case 1:656 case 2:657 case 3:658 case 4:659 case 9:660 val |= 0x0000ff00;661 break;662 }663 }664 665 rsnd_mod_write(mod, SSI_INT_ENABLE, val);666 667 return 0;668}669 670static bool rsnd_ssi_pio_interrupt(struct rsnd_mod *mod,671 struct rsnd_dai_stream *io);672static void __rsnd_ssi_interrupt(struct rsnd_mod *mod,673 struct rsnd_dai_stream *io)674{675 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);676 struct device *dev = rsnd_priv_to_dev(priv);677 int is_dma = rsnd_ssi_is_dma_mode(mod);678 u32 status;679 bool elapsed = false;680 bool stop = false;681 682 spin_lock(&priv->lock);683 684 /* ignore all cases if not working */685 if (!rsnd_io_is_working(io))686 goto rsnd_ssi_interrupt_out;687 688 status = rsnd_ssi_status_get(mod);689 690 /* PIO only */691 if (!is_dma && (status & DIRQ))692 elapsed = rsnd_ssi_pio_interrupt(mod, io);693 694 /* DMA only */695 if (is_dma && (status & (UIRQ | OIRQ))) {696 rsnd_print_irq_status(dev, "%s err status : 0x%08x\n",697 rsnd_mod_name(mod), status);698 699 stop = true;700 }701 702 stop |= rsnd_ssiu_busif_err_status_clear(mod);703 704 rsnd_ssi_status_clear(mod);705rsnd_ssi_interrupt_out:706 spin_unlock(&priv->lock);707 708 if (elapsed)709 snd_pcm_period_elapsed(io->substream);710 711 if (stop)712 snd_pcm_stop_xrun(io->substream);713 714}715 716static irqreturn_t rsnd_ssi_interrupt(int irq, void *data)717{718 struct rsnd_mod *mod = data;719 720 rsnd_mod_interrupt(mod, __rsnd_ssi_interrupt);721 722 return IRQ_HANDLED;723}724 725static u32 *rsnd_ssi_get_status(struct rsnd_mod *mod,726 struct rsnd_dai_stream *io,727 enum rsnd_mod_type type)728{729 /*730 * SSIP (= SSI parent) needs to be special, otherwise,731 * 2nd SSI might doesn't start. see also rsnd_mod_call()732 *733 * We can't include parent SSI status on SSI, because we don't know734 * how many SSI requests parent SSI. Thus, it is localed on "io" now.735 * ex) trouble case736 * Playback: SSI0737 * Capture : SSI1 (needs SSI0)738 *739 * 1) start Capture -> SSI0/SSI1 are started.740 * 2) start Playback -> SSI0 doesn't work, because it is already741 * marked as "started" on 1)742 *743 * OTOH, using each mod's status is good for MUX case.744 * It doesn't need to start in 2nd start745 * ex)746 * IO-0: SRC0 -> CTU1 -+-> MUX -> DVC -> SSIU -> SSI0747 * |748 * IO-1: SRC1 -> CTU2 -+749 *750 * 1) start IO-0 -> start SSI0751 * 2) start IO-1 -> SSI0 doesn't need to start, because it is752 * already started on 1)753 */754 if (type == RSND_MOD_SSIP)755 return &io->parent_ssi_status;756 757 return rsnd_mod_get_status(mod, io, type);758}759 760/*761 * SSI PIO762 */763static void rsnd_ssi_parent_attach(struct rsnd_mod *mod,764 struct rsnd_dai_stream *io)765{766 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);767 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);768 769 if (!__rsnd_ssi_is_pin_sharing(mod))770 return;771 772 if (!rsnd_rdai_is_clk_master(rdai))773 return;774 775 if (rsnd_ssi_is_multi_secondary(mod, io))776 return;777 778 switch (rsnd_mod_id(mod)) {779 case 1:780 case 2:781 case 9:782 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 0), io, RSND_MOD_SSIP);783 break;784 case 4:785 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 3), io, RSND_MOD_SSIP);786 break;787 case 8:788 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 7), io, RSND_MOD_SSIP);789 break;790 }791}792 793static int rsnd_ssi_pcm_new(struct rsnd_mod *mod,794 struct rsnd_dai_stream *io,795 struct snd_soc_pcm_runtime *rtd)796{797 /*798 * rsnd_rdai_is_clk_master() will be enabled after set_fmt,799 * and, pcm_new will be called after it.800 * This function reuse pcm_new at this point.801 */802 rsnd_ssi_parent_attach(mod, io);803 804 return 0;805}806 807static int rsnd_ssi_common_probe(struct rsnd_mod *mod,808 struct rsnd_dai_stream *io,809 struct rsnd_priv *priv)810{811 struct device *dev = rsnd_priv_to_dev(priv);812 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);813 int ret = 0;814 815 /*816 * SSIP/SSIU/IRQ are not needed on817 * SSI Multi secondaries818 */819 if (rsnd_ssi_is_multi_secondary(mod, io))820 return 0;821 822 /*823 * It can't judge ssi parent at this point824 * see rsnd_ssi_pcm_new()825 */826 827 /*828 * SSI might be called again as PIO fallback829 * It is easy to manual handling for IRQ request/free830 *831 * OTOH, this function might be called many times if platform is832 * using MIX. It needs xxx_attach() many times on xxx_probe().833 * Because of it, we can't control .probe/.remove calling count by834 * mod->status.835 * But it don't need to call request_irq() many times.836 * Let's control it by RSND_SSI_PROBED flag.837 */838 if (!rsnd_flags_has(ssi, RSND_SSI_PROBED)) {839 ret = request_irq(ssi->irq,840 rsnd_ssi_interrupt,841 IRQF_SHARED,842 dev_name(dev), mod);843 844 rsnd_flags_set(ssi, RSND_SSI_PROBED);845 }846 847 return ret;848}849 850static int rsnd_ssi_common_remove(struct rsnd_mod *mod,851 struct rsnd_dai_stream *io,852 struct rsnd_priv *priv)853{854 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);855 struct rsnd_mod *pure_ssi_mod = rsnd_io_to_mod_ssi(io);856 857 /* Do nothing if non SSI (= SSI parent, multi SSI) mod */858 if (pure_ssi_mod != mod)859 return 0;860 861 /* PIO will request IRQ again */862 if (rsnd_flags_has(ssi, RSND_SSI_PROBED)) {863 free_irq(ssi->irq, mod);864 865 rsnd_flags_del(ssi, RSND_SSI_PROBED);866 }867 868 return 0;869}870 871/*872 * SSI PIO functions873 */874static bool rsnd_ssi_pio_interrupt(struct rsnd_mod *mod,875 struct rsnd_dai_stream *io)876{877 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);878 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);879 u32 *buf = (u32 *)(runtime->dma_area + ssi->byte_pos);880 int shift = 0;881 int byte_pos;882 bool elapsed = false;883 884 if (snd_pcm_format_width(runtime->format) == 24)885 shift = 8;886 887 /*888 * 8/16/32 data can be assesse to TDR/RDR register889 * directly as 32bit data890 * see rsnd_ssi_init()891 */892 if (rsnd_io_is_play(io))893 rsnd_mod_write(mod, SSITDR, (*buf) << shift);894 else895 *buf = (rsnd_mod_read(mod, SSIRDR) >> shift);896 897 byte_pos = ssi->byte_pos + sizeof(*buf);898 899 if (byte_pos >= ssi->next_period_byte) {900 int period_pos = byte_pos / ssi->byte_per_period;901 902 if (period_pos >= runtime->periods) {903 byte_pos = 0;904 period_pos = 0;905 }906 907 ssi->next_period_byte = (period_pos + 1) * ssi->byte_per_period;908 909 elapsed = true;910 }911 912 WRITE_ONCE(ssi->byte_pos, byte_pos);913 914 return elapsed;915}916 917static int rsnd_ssi_pio_init(struct rsnd_mod *mod,918 struct rsnd_dai_stream *io,919 struct rsnd_priv *priv)920{921 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);922 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);923 924 if (!rsnd_ssi_is_parent(mod, io)) {925 ssi->byte_pos = 0;926 ssi->byte_per_period = runtime->period_size *927 runtime->channels *928 samples_to_bytes(runtime, 1);929 ssi->next_period_byte = ssi->byte_per_period;930 }931 932 return rsnd_ssi_init(mod, io, priv);933}934 935static int rsnd_ssi_pio_pointer(struct rsnd_mod *mod,936 struct rsnd_dai_stream *io,937 snd_pcm_uframes_t *pointer)938{939 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);940 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);941 942 *pointer = bytes_to_frames(runtime, READ_ONCE(ssi->byte_pos));943 944 return 0;945}946 947static struct rsnd_mod_ops rsnd_ssi_pio_ops = {948 .name = SSI_NAME,949 .probe = rsnd_ssi_common_probe,950 .remove = rsnd_ssi_common_remove,951 .init = rsnd_ssi_pio_init,952 .quit = rsnd_ssi_quit,953 .start = rsnd_ssi_start,954 .stop = rsnd_ssi_stop,955 .irq = rsnd_ssi_irq,956 .pointer = rsnd_ssi_pio_pointer,957 .pcm_new = rsnd_ssi_pcm_new,958 .hw_params = rsnd_ssi_hw_params,959 .get_status = rsnd_ssi_get_status,960};961 962static int rsnd_ssi_dma_probe(struct rsnd_mod *mod,963 struct rsnd_dai_stream *io,964 struct rsnd_priv *priv)965{966 int ret;967 968 /*969 * SSIP/SSIU/IRQ/DMA are not needed on970 * SSI Multi secondaries971 */972 if (rsnd_ssi_is_multi_secondary(mod, io))973 return 0;974 975 ret = rsnd_ssi_common_probe(mod, io, priv);976 if (ret)977 return ret;978 979 /* SSI probe might be called many times in MUX multi path */980 ret = rsnd_dma_attach(io, mod, &io->dma);981 982 return ret;983}984 985static int rsnd_ssi_fallback(struct rsnd_mod *mod,986 struct rsnd_dai_stream *io,987 struct rsnd_priv *priv)988{989 struct device *dev = rsnd_priv_to_dev(priv);990 991 /*992 * fallback to PIO993 *994 * SSI .probe might be called again.995 * see996 * rsnd_rdai_continuance_probe()997 */998 mod->ops = &rsnd_ssi_pio_ops;999 1000 dev_info(dev, "%s fallback to PIO mode\n", rsnd_mod_name(mod));1001 1002 return 0;1003}1004 1005static struct dma_chan *rsnd_ssi_dma_req(struct rsnd_dai_stream *io,1006 struct rsnd_mod *mod)1007{1008 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);1009 int is_play = rsnd_io_is_play(io);1010 char *name;1011 1012 /*1013 * It should use "rcar_sound,ssiu" on DT.1014 * But, we need to keep compatibility for old version.1015 *1016 * If it has "rcar_sound.ssiu", it will be used.1017 * If not, "rcar_sound.ssi" will be used.1018 * see1019 * rsnd_ssiu_dma_req()1020 * rsnd_dma_of_path()1021 */1022 1023 if (rsnd_ssi_use_busif(io))1024 name = is_play ? "rxu" : "txu";1025 else1026 name = is_play ? "rx" : "tx";1027 1028 return rsnd_dma_request_channel(rsnd_ssi_of_node(priv),1029 SSI_NAME, mod, name);1030}1031 1032#ifdef CONFIG_DEBUG_FS1033static void rsnd_ssi_debug_info(struct seq_file *m,1034 struct rsnd_dai_stream *io,1035 struct rsnd_mod *mod)1036{1037 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);1038 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);1039 1040 seq_printf(m, "clock: %s\n", rsnd_rdai_is_clk_master(rdai) ?1041 "provider" : "consumer");1042 seq_printf(m, "bit_clk_inv: %d\n", rdai->bit_clk_inv);1043 seq_printf(m, "frm_clk_inv: %d\n", rdai->frm_clk_inv);1044 seq_printf(m, "pin share: %d\n", __rsnd_ssi_is_pin_sharing(mod));1045 seq_printf(m, "can out clk: %d\n", rsnd_ssi_can_output_clk(mod));1046 seq_printf(m, "multi secondary: %d\n", rsnd_ssi_is_multi_secondary(mod, io));1047 seq_printf(m, "tdm: %d, %d\n", rsnd_runtime_is_tdm(io),1048 rsnd_runtime_is_tdm_split(io));1049 seq_printf(m, "chan: %d\n", ssi->chan);1050 seq_printf(m, "user: %d\n", ssi->usrcnt);1051 1052 rsnd_debugfs_mod_reg_show(m, mod, RSND_BASE_SSI,1053 rsnd_mod_id(mod) * 0x40, 0x40);1054}1055#define DEBUG_INFO .debug_info = rsnd_ssi_debug_info1056#else1057#define DEBUG_INFO1058#endif1059 1060static struct rsnd_mod_ops rsnd_ssi_dma_ops = {1061 .name = SSI_NAME,1062 .dma_req = rsnd_ssi_dma_req,1063 .probe = rsnd_ssi_dma_probe,1064 .remove = rsnd_ssi_common_remove,1065 .init = rsnd_ssi_init,1066 .quit = rsnd_ssi_quit,1067 .start = rsnd_ssi_start,1068 .stop = rsnd_ssi_stop,1069 .irq = rsnd_ssi_irq,1070 .pcm_new = rsnd_ssi_pcm_new,1071 .fallback = rsnd_ssi_fallback,1072 .hw_params = rsnd_ssi_hw_params,1073 .get_status = rsnd_ssi_get_status,1074 DEBUG_INFO1075};1076 1077int rsnd_ssi_is_dma_mode(struct rsnd_mod *mod)1078{1079 return mod->ops == &rsnd_ssi_dma_ops;1080}1081 1082/*1083 * ssi mod function1084 */1085static void rsnd_ssi_connect(struct rsnd_mod *mod,1086 struct rsnd_dai_stream *io)1087{1088 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);1089 static const enum rsnd_mod_type types[] = {1090 RSND_MOD_SSI,1091 RSND_MOD_SSIM1,1092 RSND_MOD_SSIM2,1093 RSND_MOD_SSIM3,1094 };1095 enum rsnd_mod_type type;1096 int i;1097 1098 /* try SSI -> SSIM1 -> SSIM2 -> SSIM3 */1099 for (i = 0; i < ARRAY_SIZE(types); i++) {1100 type = types[i];1101 if (!rsnd_io_to_mod(io, type)) {1102 rsnd_dai_connect(mod, io, type);1103 rsnd_rdai_channels_set(rdai, (i + 1) * 2);1104 rsnd_rdai_ssi_lane_set(rdai, (i + 1));1105 return;1106 }1107 }1108}1109 1110void rsnd_parse_connect_ssi(struct rsnd_dai *rdai,1111 struct device_node *playback,1112 struct device_node *capture)1113{1114 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);1115 struct device *dev = rsnd_priv_to_dev(priv);1116 struct device_node *node;1117 struct device_node *np;1118 int i;1119 1120 node = rsnd_ssi_of_node(priv);1121 if (!node)1122 return;1123 1124 i = 0;1125 for_each_child_of_node(node, np) {1126 struct rsnd_mod *mod;1127 1128 i = rsnd_node_fixed_index(dev, np, SSI_NAME, i);1129 if (i < 0) {1130 of_node_put(np);1131 break;1132 }1133 1134 mod = rsnd_ssi_mod_get(priv, i);1135 1136 if (np == playback)1137 rsnd_ssi_connect(mod, &rdai->playback);1138 if (np == capture)1139 rsnd_ssi_connect(mod, &rdai->capture);1140 i++;1141 }1142 1143 of_node_put(node);1144}1145 1146struct rsnd_mod *rsnd_ssi_mod_get(struct rsnd_priv *priv, int id)1147{1148 if (WARN_ON(id < 0 || id >= rsnd_ssi_nr(priv)))1149 id = 0;1150 1151 return rsnd_mod_get(rsnd_ssi_get(priv, id));1152}1153 1154int __rsnd_ssi_is_pin_sharing(struct rsnd_mod *mod)1155{1156 if (!mod)1157 return 0;1158 1159 return !!(rsnd_flags_has(rsnd_mod_to_ssi(mod), RSND_SSI_CLK_PIN_SHARE));1160}1161 1162int rsnd_ssi_probe(struct rsnd_priv *priv)1163{1164 struct device_node *node;1165 struct device_node *np;1166 struct device *dev = rsnd_priv_to_dev(priv);1167 struct rsnd_mod_ops *ops;1168 struct clk *clk;1169 struct rsnd_ssi *ssi;1170 char name[RSND_SSI_NAME_SIZE];1171 int i, nr, ret;1172 1173 node = rsnd_ssi_of_node(priv);1174 if (!node)1175 return -EINVAL;1176 1177 nr = rsnd_node_count(priv, node, SSI_NAME);1178 if (!nr) {1179 ret = -EINVAL;1180 goto rsnd_ssi_probe_done;1181 }1182 1183 ssi = devm_kcalloc(dev, nr, sizeof(*ssi), GFP_KERNEL);1184 if (!ssi) {1185 ret = -ENOMEM;1186 goto rsnd_ssi_probe_done;1187 }1188 1189 priv->ssi = ssi;1190 priv->ssi_nr = nr;1191 1192 i = 0;1193 for_each_child_of_node(node, np) {1194 if (!of_device_is_available(np))1195 goto skip;1196 1197 i = rsnd_node_fixed_index(dev, np, SSI_NAME, i);1198 if (i < 0) {1199 ret = -EINVAL;1200 of_node_put(np);1201 goto rsnd_ssi_probe_done;1202 }1203 1204 ssi = rsnd_ssi_get(priv, i);1205 1206 snprintf(name, RSND_SSI_NAME_SIZE, "%s.%d",1207 SSI_NAME, i);1208 1209 clk = devm_clk_get(dev, name);1210 if (IS_ERR(clk)) {1211 ret = PTR_ERR(clk);1212 of_node_put(np);1213 goto rsnd_ssi_probe_done;1214 }1215 1216 if (of_property_read_bool(np, "shared-pin"))1217 rsnd_flags_set(ssi, RSND_SSI_CLK_PIN_SHARE);1218 1219 if (of_property_read_bool(np, "no-busif"))1220 rsnd_flags_set(ssi, RSND_SSI_NO_BUSIF);1221 1222 ssi->irq = irq_of_parse_and_map(np, 0);1223 if (!ssi->irq) {1224 ret = -EINVAL;1225 of_node_put(np);1226 goto rsnd_ssi_probe_done;1227 }1228 1229 if (of_property_read_bool(np, "pio-transfer"))1230 ops = &rsnd_ssi_pio_ops;1231 else1232 ops = &rsnd_ssi_dma_ops;1233 1234 ret = rsnd_mod_init(priv, rsnd_mod_get(ssi), ops, clk,1235 RSND_MOD_SSI, i);1236 if (ret) {1237 of_node_put(np);1238 goto rsnd_ssi_probe_done;1239 }1240skip:1241 i++;1242 }1243 1244 ret = 0;1245 1246rsnd_ssi_probe_done:1247 of_node_put(node);1248 1249 return ret;1250}1251 1252void rsnd_ssi_remove(struct rsnd_priv *priv)1253{1254 struct rsnd_ssi *ssi;1255 int i;1256 1257 for_each_rsnd_ssi(ssi, priv, i) {1258 rsnd_mod_quit(rsnd_mod_get(ssi));1259 }1260}1261