18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci// 38c2ecf20Sopenharmony_ci// Driver for Microchip I2S Multi-channel controller 48c2ecf20Sopenharmony_ci// 58c2ecf20Sopenharmony_ci// Copyright (C) 2018 Microchip Technology Inc. and its subsidiaries 68c2ecf20Sopenharmony_ci// 78c2ecf20Sopenharmony_ci// Author: Codrin Ciubotariu <codrin.ciubotariu@microchip.com> 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ci#include <linux/init.h> 108c2ecf20Sopenharmony_ci#include <linux/module.h> 118c2ecf20Sopenharmony_ci#include <linux/device.h> 128c2ecf20Sopenharmony_ci#include <linux/slab.h> 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci#include <linux/delay.h> 158c2ecf20Sopenharmony_ci#include <linux/io.h> 168c2ecf20Sopenharmony_ci#include <linux/clk.h> 178c2ecf20Sopenharmony_ci#include <linux/mfd/syscon.h> 188c2ecf20Sopenharmony_ci#include <linux/lcm.h> 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_ci#include <sound/core.h> 218c2ecf20Sopenharmony_ci#include <sound/pcm.h> 228c2ecf20Sopenharmony_ci#include <sound/pcm_params.h> 238c2ecf20Sopenharmony_ci#include <sound/initval.h> 248c2ecf20Sopenharmony_ci#include <sound/soc.h> 258c2ecf20Sopenharmony_ci#include <sound/dmaengine_pcm.h> 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ci/* 288c2ecf20Sopenharmony_ci * ---- I2S Controller Register map ---- 298c2ecf20Sopenharmony_ci */ 308c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_CR 0x0000 /* Control Register */ 318c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRA 0x0004 /* Mode Register A */ 328c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRB 0x0008 /* Mode Register B */ 338c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_SR 0x000C /* Status Register */ 348c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_IERA 0x0010 /* Interrupt Enable Register A */ 358c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_IDRA 0x0014 /* Interrupt Disable Register A */ 368c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_IMRA 0x0018 /* Interrupt Mask Register A */ 378c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_ISRA 0X001C /* Interrupt Status Register A */ 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_IERB 0x0020 /* Interrupt Enable Register B */ 408c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_IDRB 0x0024 /* Interrupt Disable Register B */ 418c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_IMRB 0x0028 /* Interrupt Mask Register B */ 428c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_ISRB 0X002C /* Interrupt Status Register B */ 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_RHR 0x0030 /* Receiver Holding Register */ 458c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_THR 0x0034 /* Transmitter Holding Register */ 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_RHL0R 0x0040 /* Receiver Holding Left 0 Register */ 488c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_RHR0R 0x0044 /* Receiver Holding Right 0 Register */ 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_RHL1R 0x0048 /* Receiver Holding Left 1 Register */ 518c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_RHR1R 0x004C /* Receiver Holding Right 1 Register */ 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_RHL2R 0x0050 /* Receiver Holding Left 2 Register */ 548c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_RHR2R 0x0054 /* Receiver Holding Right 2 Register */ 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_RHL3R 0x0058 /* Receiver Holding Left 3 Register */ 578c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_RHR3R 0x005C /* Receiver Holding Right 3 Register */ 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_THL0R 0x0060 /* Transmitter Holding Left 0 Register */ 608c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_THR0R 0x0064 /* Transmitter Holding Right 0 Register */ 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_THL1R 0x0068 /* Transmitter Holding Left 1 Register */ 638c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_THR1R 0x006C /* Transmitter Holding Right 1 Register */ 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_THL2R 0x0070 /* Transmitter Holding Left 2 Register */ 668c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_THR2R 0x0074 /* Transmitter Holding Right 2 Register */ 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_THL3R 0x0078 /* Transmitter Holding Left 3 Register */ 698c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_THR3R 0x007C /* Transmitter Holding Right 3 Register */ 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_VERSION 0x00FC /* Version Register */ 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci/* 748c2ecf20Sopenharmony_ci * ---- Control Register (Write-only) ---- 758c2ecf20Sopenharmony_ci */ 768c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_CR_RXEN BIT(0) /* Receiver Enable */ 778c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_CR_RXDIS BIT(1) /* Receiver Disable */ 788c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_CR_CKEN BIT(2) /* Clock Enable */ 798c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_CR_CKDIS BIT(3) /* Clock Disable */ 808c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_CR_TXEN BIT(4) /* Transmitter Enable */ 818c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_CR_TXDIS BIT(5) /* Transmitter Disable */ 828c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_CR_SWRST BIT(7) /* Software Reset */ 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci/* 858c2ecf20Sopenharmony_ci * ---- Mode Register A (Read/Write) ---- 868c2ecf20Sopenharmony_ci */ 878c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRA_MODE_MASK GENMASK(0, 0) 888c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRA_MODE_SLAVE (0 << 0) 898c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRA_MODE_MASTER (1 << 0) 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRA_DATALENGTH_MASK GENMASK(3, 1) 928c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRA_DATALENGTH_32_BITS (0 << 1) 938c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRA_DATALENGTH_24_BITS (1 << 1) 948c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRA_DATALENGTH_20_BITS (2 << 1) 958c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRA_DATALENGTH_18_BITS (3 << 1) 968c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRA_DATALENGTH_16_BITS (4 << 1) 978c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRA_DATALENGTH_16_BITS_COMPACT (5 << 1) 988c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRA_DATALENGTH_8_BITS (6 << 1) 998c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRA_DATALENGTH_8_BITS_COMPACT (7 << 1) 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRA_WIRECFG_MASK GENMASK(5, 4) 1028c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRA_WIRECFG_I2S_1_TDM_0 (0 << 4) 1038c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRA_WIRECFG_I2S_2_TDM_1 (1 << 4) 1048c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRA_WIRECFG_I2S_4_TDM_2 (2 << 4) 1058c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRA_WIRECFG_TDM_3 (3 << 4) 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRA_FORMAT_MASK GENMASK(7, 6) 1088c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRA_FORMAT_I2S (0 << 6) 1098c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRA_FORMAT_LJ (1 << 6) /* Left Justified */ 1108c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRA_FORMAT_TDM (2 << 6) 1118c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRA_FORMAT_TDMLJ (3 << 6) 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci/* Transmitter uses one DMA channel ... */ 1148c2ecf20Sopenharmony_ci/* Left audio samples duplicated to right audio channel */ 1158c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRA_RXMONO BIT(8) 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_ci/* I2SDO output of I2SC is internally connected to I2SDI input */ 1188c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRA_RXLOOP BIT(9) 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci/* Receiver uses one DMA channel ... */ 1218c2ecf20Sopenharmony_ci/* Left audio samples duplicated to right audio channel */ 1228c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRA_TXMONO BIT(10) 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci/* x sample transmitted when underrun */ 1258c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRA_TXSAME_ZERO (0 << 11) /* Zero sample */ 1268c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRA_TXSAME_PREVIOUS (1 << 11) /* Previous sample */ 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ci/* select between peripheral clock and generated clock */ 1298c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRA_SRCCLK_PCLK (0 << 12) 1308c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRA_SRCCLK_GCLK (1 << 12) 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci/* Number of TDM Channels - 1 */ 1338c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRA_NBCHAN_MASK GENMASK(15, 13) 1348c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRA_NBCHAN(ch) \ 1358c2ecf20Sopenharmony_ci ((((ch) - 1) << 13) & MCHP_I2SMCC_MRA_NBCHAN_MASK) 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ci/* Selected Clock to I2SMCC Master Clock ratio */ 1388c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRA_IMCKDIV_MASK GENMASK(21, 16) 1398c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRA_IMCKDIV(div) \ 1408c2ecf20Sopenharmony_ci (((div) << 16) & MCHP_I2SMCC_MRA_IMCKDIV_MASK) 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ci/* TDM Frame Synchronization */ 1438c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRA_TDMFS_MASK GENMASK(23, 22) 1448c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRA_TDMFS_SLOT (0 << 22) 1458c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRA_TDMFS_HALF (1 << 22) 1468c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRA_TDMFS_BIT (2 << 22) 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci/* Selected Clock to I2SMC Serial Clock ratio */ 1498c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRA_ISCKDIV_MASK GENMASK(29, 24) 1508c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRA_ISCKDIV(div) \ 1518c2ecf20Sopenharmony_ci (((div) << 24) & MCHP_I2SMCC_MRA_ISCKDIV_MASK) 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_ci/* Master Clock mode */ 1548c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRA_IMCKMODE_MASK GENMASK(30, 30) 1558c2ecf20Sopenharmony_ci/* 0: No master clock generated*/ 1568c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRA_IMCKMODE_NONE (0 << 30) 1578c2ecf20Sopenharmony_ci/* 1: master clock generated (internally generated clock drives I2SMCK pin) */ 1588c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRA_IMCKMODE_GEN (1 << 30) 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci/* Slot Width */ 1618c2ecf20Sopenharmony_ci/* 0: slot is 32 bits wide for DATALENGTH = 18/20/24 bits. */ 1628c2ecf20Sopenharmony_ci/* 1: slot is 24 bits wide for DATALENGTH = 18/20/24 bits. */ 1638c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRA_IWS BIT(31) 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ci/* 1668c2ecf20Sopenharmony_ci * ---- Mode Register B (Read/Write) ---- 1678c2ecf20Sopenharmony_ci */ 1688c2ecf20Sopenharmony_ci/* all enabled I2S left channels are filled first, then I2S right channels */ 1698c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRB_CRAMODE_LEFT_FIRST (0 << 0) 1708c2ecf20Sopenharmony_ci/* 1718c2ecf20Sopenharmony_ci * an enabled I2S left channel is filled, then the corresponding right 1728c2ecf20Sopenharmony_ci * channel, until all channels are filled 1738c2ecf20Sopenharmony_ci */ 1748c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRB_CRAMODE_REGULAR (1 << 0) 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRB_FIFOEN BIT(1) 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRB_DMACHUNK_MASK GENMASK(9, 8) 1798c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRB_DMACHUNK(no_words) \ 1808c2ecf20Sopenharmony_ci (((fls(no_words) - 1) << 8) & MCHP_I2SMCC_MRB_DMACHUNK_MASK) 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRB_CLKSEL_MASK GENMASK(16, 16) 1838c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRB_CLKSEL_EXT (0 << 16) 1848c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MRB_CLKSEL_INT (1 << 16) 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_ci/* 1878c2ecf20Sopenharmony_ci * ---- Status Registers (Read-only) ---- 1888c2ecf20Sopenharmony_ci */ 1898c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_SR_RXEN BIT(0) /* Receiver Enabled */ 1908c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_SR_TXEN BIT(4) /* Transmitter Enabled */ 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ci/* 1938c2ecf20Sopenharmony_ci * ---- Interrupt Enable/Disable/Mask/Status Registers A ---- 1948c2ecf20Sopenharmony_ci */ 1958c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_INT_TXRDY_MASK(ch) GENMASK((ch) - 1, 0) 1968c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_INT_TXRDYCH(ch) BIT(ch) 1978c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_INT_TXUNF_MASK(ch) GENMASK((ch) + 7, 8) 1988c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_INT_TXUNFCH(ch) BIT((ch) + 8) 1998c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_INT_RXRDY_MASK(ch) GENMASK((ch) + 15, 16) 2008c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_INT_RXRDYCH(ch) BIT((ch) + 16) 2018c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_INT_RXOVF_MASK(ch) GENMASK((ch) + 23, 24) 2028c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_INT_RXOVFCH(ch) BIT((ch) + 24) 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_ci/* 2058c2ecf20Sopenharmony_ci * ---- Interrupt Enable/Disable/Mask/Status Registers B ---- 2068c2ecf20Sopenharmony_ci */ 2078c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_INT_WERR BIT(0) 2088c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_INT_TXFFRDY BIT(8) 2098c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_INT_TXFFEMP BIT(9) 2108c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_INT_RXFFRDY BIT(12) 2118c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_INT_RXFFFUL BIT(13) 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_ci/* 2148c2ecf20Sopenharmony_ci * ---- Version Register (Read-only) ---- 2158c2ecf20Sopenharmony_ci */ 2168c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_VERSION_MASK GENMASK(11, 0) 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_MAX_CHANNELS 8 2198c2ecf20Sopenharmony_ci#define MCHP_I2MCC_TDM_SLOT_WIDTH 32 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_cistatic const struct regmap_config mchp_i2s_mcc_regmap_config = { 2228c2ecf20Sopenharmony_ci .reg_bits = 32, 2238c2ecf20Sopenharmony_ci .reg_stride = 4, 2248c2ecf20Sopenharmony_ci .val_bits = 32, 2258c2ecf20Sopenharmony_ci .max_register = MCHP_I2SMCC_VERSION, 2268c2ecf20Sopenharmony_ci}; 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_cistruct mchp_i2s_mcc_dev { 2298c2ecf20Sopenharmony_ci struct wait_queue_head wq_txrdy; 2308c2ecf20Sopenharmony_ci struct wait_queue_head wq_rxrdy; 2318c2ecf20Sopenharmony_ci struct device *dev; 2328c2ecf20Sopenharmony_ci struct regmap *regmap; 2338c2ecf20Sopenharmony_ci struct clk *pclk; 2348c2ecf20Sopenharmony_ci struct clk *gclk; 2358c2ecf20Sopenharmony_ci struct snd_dmaengine_dai_dma_data playback; 2368c2ecf20Sopenharmony_ci struct snd_dmaengine_dai_dma_data capture; 2378c2ecf20Sopenharmony_ci unsigned int fmt; 2388c2ecf20Sopenharmony_ci unsigned int sysclk; 2398c2ecf20Sopenharmony_ci unsigned int frame_length; 2408c2ecf20Sopenharmony_ci int tdm_slots; 2418c2ecf20Sopenharmony_ci int channels; 2428c2ecf20Sopenharmony_ci unsigned int gclk_use:1; 2438c2ecf20Sopenharmony_ci unsigned int gclk_running:1; 2448c2ecf20Sopenharmony_ci unsigned int tx_rdy:1; 2458c2ecf20Sopenharmony_ci unsigned int rx_rdy:1; 2468c2ecf20Sopenharmony_ci}; 2478c2ecf20Sopenharmony_ci 2488c2ecf20Sopenharmony_cistatic irqreturn_t mchp_i2s_mcc_interrupt(int irq, void *dev_id) 2498c2ecf20Sopenharmony_ci{ 2508c2ecf20Sopenharmony_ci struct mchp_i2s_mcc_dev *dev = dev_id; 2518c2ecf20Sopenharmony_ci u32 sra, imra, srb, imrb, pendinga, pendingb, idra = 0; 2528c2ecf20Sopenharmony_ci irqreturn_t ret = IRQ_NONE; 2538c2ecf20Sopenharmony_ci 2548c2ecf20Sopenharmony_ci regmap_read(dev->regmap, MCHP_I2SMCC_IMRA, &imra); 2558c2ecf20Sopenharmony_ci regmap_read(dev->regmap, MCHP_I2SMCC_ISRA, &sra); 2568c2ecf20Sopenharmony_ci pendinga = imra & sra; 2578c2ecf20Sopenharmony_ci 2588c2ecf20Sopenharmony_ci regmap_read(dev->regmap, MCHP_I2SMCC_IMRB, &imrb); 2598c2ecf20Sopenharmony_ci regmap_read(dev->regmap, MCHP_I2SMCC_ISRB, &srb); 2608c2ecf20Sopenharmony_ci pendingb = imrb & srb; 2618c2ecf20Sopenharmony_ci 2628c2ecf20Sopenharmony_ci if (!pendinga && !pendingb) 2638c2ecf20Sopenharmony_ci return IRQ_NONE; 2648c2ecf20Sopenharmony_ci 2658c2ecf20Sopenharmony_ci /* 2668c2ecf20Sopenharmony_ci * Tx/Rx ready interrupts are enabled when stopping only, to assure 2678c2ecf20Sopenharmony_ci * availability and to disable clocks if necessary 2688c2ecf20Sopenharmony_ci */ 2698c2ecf20Sopenharmony_ci idra |= pendinga & (MCHP_I2SMCC_INT_TXRDY_MASK(dev->channels) | 2708c2ecf20Sopenharmony_ci MCHP_I2SMCC_INT_RXRDY_MASK(dev->channels)); 2718c2ecf20Sopenharmony_ci if (idra) 2728c2ecf20Sopenharmony_ci ret = IRQ_HANDLED; 2738c2ecf20Sopenharmony_ci 2748c2ecf20Sopenharmony_ci if ((imra & MCHP_I2SMCC_INT_TXRDY_MASK(dev->channels)) && 2758c2ecf20Sopenharmony_ci (imra & MCHP_I2SMCC_INT_TXRDY_MASK(dev->channels)) == 2768c2ecf20Sopenharmony_ci (idra & MCHP_I2SMCC_INT_TXRDY_MASK(dev->channels))) { 2778c2ecf20Sopenharmony_ci dev->tx_rdy = 1; 2788c2ecf20Sopenharmony_ci wake_up_interruptible(&dev->wq_txrdy); 2798c2ecf20Sopenharmony_ci } 2808c2ecf20Sopenharmony_ci if ((imra & MCHP_I2SMCC_INT_RXRDY_MASK(dev->channels)) && 2818c2ecf20Sopenharmony_ci (imra & MCHP_I2SMCC_INT_RXRDY_MASK(dev->channels)) == 2828c2ecf20Sopenharmony_ci (idra & MCHP_I2SMCC_INT_RXRDY_MASK(dev->channels))) { 2838c2ecf20Sopenharmony_ci dev->rx_rdy = 1; 2848c2ecf20Sopenharmony_ci wake_up_interruptible(&dev->wq_rxrdy); 2858c2ecf20Sopenharmony_ci } 2868c2ecf20Sopenharmony_ci regmap_write(dev->regmap, MCHP_I2SMCC_IDRA, idra); 2878c2ecf20Sopenharmony_ci 2888c2ecf20Sopenharmony_ci return ret; 2898c2ecf20Sopenharmony_ci} 2908c2ecf20Sopenharmony_ci 2918c2ecf20Sopenharmony_cistatic int mchp_i2s_mcc_set_sysclk(struct snd_soc_dai *dai, 2928c2ecf20Sopenharmony_ci int clk_id, unsigned int freq, int dir) 2938c2ecf20Sopenharmony_ci{ 2948c2ecf20Sopenharmony_ci struct mchp_i2s_mcc_dev *dev = snd_soc_dai_get_drvdata(dai); 2958c2ecf20Sopenharmony_ci 2968c2ecf20Sopenharmony_ci dev_dbg(dev->dev, "%s() clk_id=%d freq=%u dir=%d\n", 2978c2ecf20Sopenharmony_ci __func__, clk_id, freq, dir); 2988c2ecf20Sopenharmony_ci 2998c2ecf20Sopenharmony_ci /* We do not need SYSCLK */ 3008c2ecf20Sopenharmony_ci if (dir == SND_SOC_CLOCK_IN) 3018c2ecf20Sopenharmony_ci return 0; 3028c2ecf20Sopenharmony_ci 3038c2ecf20Sopenharmony_ci dev->sysclk = freq; 3048c2ecf20Sopenharmony_ci 3058c2ecf20Sopenharmony_ci return 0; 3068c2ecf20Sopenharmony_ci} 3078c2ecf20Sopenharmony_ci 3088c2ecf20Sopenharmony_cistatic int mchp_i2s_mcc_set_bclk_ratio(struct snd_soc_dai *dai, 3098c2ecf20Sopenharmony_ci unsigned int ratio) 3108c2ecf20Sopenharmony_ci{ 3118c2ecf20Sopenharmony_ci struct mchp_i2s_mcc_dev *dev = snd_soc_dai_get_drvdata(dai); 3128c2ecf20Sopenharmony_ci 3138c2ecf20Sopenharmony_ci dev_dbg(dev->dev, "%s() ratio=%u\n", __func__, ratio); 3148c2ecf20Sopenharmony_ci 3158c2ecf20Sopenharmony_ci dev->frame_length = ratio; 3168c2ecf20Sopenharmony_ci 3178c2ecf20Sopenharmony_ci return 0; 3188c2ecf20Sopenharmony_ci} 3198c2ecf20Sopenharmony_ci 3208c2ecf20Sopenharmony_cistatic int mchp_i2s_mcc_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt) 3218c2ecf20Sopenharmony_ci{ 3228c2ecf20Sopenharmony_ci struct mchp_i2s_mcc_dev *dev = snd_soc_dai_get_drvdata(dai); 3238c2ecf20Sopenharmony_ci 3248c2ecf20Sopenharmony_ci dev_dbg(dev->dev, "%s() fmt=%#x\n", __func__, fmt); 3258c2ecf20Sopenharmony_ci 3268c2ecf20Sopenharmony_ci /* We don't support any kind of clock inversion */ 3278c2ecf20Sopenharmony_ci if ((fmt & SND_SOC_DAIFMT_INV_MASK) != SND_SOC_DAIFMT_NB_NF) 3288c2ecf20Sopenharmony_ci return -EINVAL; 3298c2ecf20Sopenharmony_ci 3308c2ecf20Sopenharmony_ci /* We can't generate only FSYNC */ 3318c2ecf20Sopenharmony_ci if ((fmt & SND_SOC_DAIFMT_MASTER_MASK) == SND_SOC_DAIFMT_CBM_CFS) 3328c2ecf20Sopenharmony_ci return -EINVAL; 3338c2ecf20Sopenharmony_ci 3348c2ecf20Sopenharmony_ci /* We can only reconfigure the IP when it's stopped */ 3358c2ecf20Sopenharmony_ci if (fmt & SND_SOC_DAIFMT_CONT) 3368c2ecf20Sopenharmony_ci return -EINVAL; 3378c2ecf20Sopenharmony_ci 3388c2ecf20Sopenharmony_ci dev->fmt = fmt; 3398c2ecf20Sopenharmony_ci 3408c2ecf20Sopenharmony_ci return 0; 3418c2ecf20Sopenharmony_ci} 3428c2ecf20Sopenharmony_ci 3438c2ecf20Sopenharmony_cistatic int mchp_i2s_mcc_set_dai_tdm_slot(struct snd_soc_dai *dai, 3448c2ecf20Sopenharmony_ci unsigned int tx_mask, 3458c2ecf20Sopenharmony_ci unsigned int rx_mask, 3468c2ecf20Sopenharmony_ci int slots, int slot_width) 3478c2ecf20Sopenharmony_ci{ 3488c2ecf20Sopenharmony_ci struct mchp_i2s_mcc_dev *dev = snd_soc_dai_get_drvdata(dai); 3498c2ecf20Sopenharmony_ci 3508c2ecf20Sopenharmony_ci dev_dbg(dev->dev, 3518c2ecf20Sopenharmony_ci "%s() tx_mask=0x%08x rx_mask=0x%08x slots=%d width=%d\n", 3528c2ecf20Sopenharmony_ci __func__, tx_mask, rx_mask, slots, slot_width); 3538c2ecf20Sopenharmony_ci 3548c2ecf20Sopenharmony_ci if (slots < 0 || slots > MCHP_I2SMCC_MAX_CHANNELS || 3558c2ecf20Sopenharmony_ci slot_width != MCHP_I2MCC_TDM_SLOT_WIDTH) 3568c2ecf20Sopenharmony_ci return -EINVAL; 3578c2ecf20Sopenharmony_ci 3588c2ecf20Sopenharmony_ci if (slots) { 3598c2ecf20Sopenharmony_ci /* We do not support daisy chain */ 3608c2ecf20Sopenharmony_ci if (rx_mask != GENMASK(slots - 1, 0) || 3618c2ecf20Sopenharmony_ci rx_mask != tx_mask) 3628c2ecf20Sopenharmony_ci return -EINVAL; 3638c2ecf20Sopenharmony_ci } 3648c2ecf20Sopenharmony_ci 3658c2ecf20Sopenharmony_ci dev->tdm_slots = slots; 3668c2ecf20Sopenharmony_ci dev->frame_length = slots * MCHP_I2MCC_TDM_SLOT_WIDTH; 3678c2ecf20Sopenharmony_ci 3688c2ecf20Sopenharmony_ci return 0; 3698c2ecf20Sopenharmony_ci} 3708c2ecf20Sopenharmony_ci 3718c2ecf20Sopenharmony_cistatic int mchp_i2s_mcc_clk_get_rate_diff(struct clk *clk, 3728c2ecf20Sopenharmony_ci unsigned long rate, 3738c2ecf20Sopenharmony_ci struct clk **best_clk, 3748c2ecf20Sopenharmony_ci unsigned long *best_rate, 3758c2ecf20Sopenharmony_ci unsigned long *best_diff_rate) 3768c2ecf20Sopenharmony_ci{ 3778c2ecf20Sopenharmony_ci long round_rate; 3788c2ecf20Sopenharmony_ci unsigned int diff_rate; 3798c2ecf20Sopenharmony_ci 3808c2ecf20Sopenharmony_ci round_rate = clk_round_rate(clk, rate); 3818c2ecf20Sopenharmony_ci if (round_rate < 0) 3828c2ecf20Sopenharmony_ci return (int)round_rate; 3838c2ecf20Sopenharmony_ci 3848c2ecf20Sopenharmony_ci diff_rate = abs(rate - round_rate); 3858c2ecf20Sopenharmony_ci if (diff_rate < *best_diff_rate) { 3868c2ecf20Sopenharmony_ci *best_clk = clk; 3878c2ecf20Sopenharmony_ci *best_diff_rate = diff_rate; 3888c2ecf20Sopenharmony_ci *best_rate = rate; 3898c2ecf20Sopenharmony_ci } 3908c2ecf20Sopenharmony_ci 3918c2ecf20Sopenharmony_ci return 0; 3928c2ecf20Sopenharmony_ci} 3938c2ecf20Sopenharmony_ci 3948c2ecf20Sopenharmony_cistatic int mchp_i2s_mcc_config_divs(struct mchp_i2s_mcc_dev *dev, 3958c2ecf20Sopenharmony_ci unsigned int bclk, unsigned int *mra, 3968c2ecf20Sopenharmony_ci unsigned long *best_rate) 3978c2ecf20Sopenharmony_ci{ 3988c2ecf20Sopenharmony_ci unsigned long clk_rate; 3998c2ecf20Sopenharmony_ci unsigned long lcm_rate; 4008c2ecf20Sopenharmony_ci unsigned long best_diff_rate = ~0; 4018c2ecf20Sopenharmony_ci unsigned int sysclk; 4028c2ecf20Sopenharmony_ci struct clk *best_clk = NULL; 4038c2ecf20Sopenharmony_ci int ret; 4048c2ecf20Sopenharmony_ci 4058c2ecf20Sopenharmony_ci /* For code simplification */ 4068c2ecf20Sopenharmony_ci if (!dev->sysclk) 4078c2ecf20Sopenharmony_ci sysclk = bclk; 4088c2ecf20Sopenharmony_ci else 4098c2ecf20Sopenharmony_ci sysclk = dev->sysclk; 4108c2ecf20Sopenharmony_ci 4118c2ecf20Sopenharmony_ci /* 4128c2ecf20Sopenharmony_ci * MCLK is Selected CLK / (2 * IMCKDIV), 4138c2ecf20Sopenharmony_ci * BCLK is Selected CLK / (2 * ISCKDIV); 4148c2ecf20Sopenharmony_ci * if IMCKDIV or ISCKDIV are 0, MCLK or BCLK = Selected CLK 4158c2ecf20Sopenharmony_ci */ 4168c2ecf20Sopenharmony_ci lcm_rate = lcm(sysclk, bclk); 4178c2ecf20Sopenharmony_ci if ((lcm_rate / sysclk % 2 == 1 && lcm_rate / sysclk > 2) || 4188c2ecf20Sopenharmony_ci (lcm_rate / bclk % 2 == 1 && lcm_rate / bclk > 2)) 4198c2ecf20Sopenharmony_ci lcm_rate *= 2; 4208c2ecf20Sopenharmony_ci 4218c2ecf20Sopenharmony_ci for (clk_rate = lcm_rate; 4228c2ecf20Sopenharmony_ci (clk_rate == sysclk || clk_rate / (sysclk * 2) <= GENMASK(5, 0)) && 4238c2ecf20Sopenharmony_ci (clk_rate == bclk || clk_rate / (bclk * 2) <= GENMASK(5, 0)); 4248c2ecf20Sopenharmony_ci clk_rate += lcm_rate) { 4258c2ecf20Sopenharmony_ci ret = mchp_i2s_mcc_clk_get_rate_diff(dev->gclk, clk_rate, 4268c2ecf20Sopenharmony_ci &best_clk, best_rate, 4278c2ecf20Sopenharmony_ci &best_diff_rate); 4288c2ecf20Sopenharmony_ci if (ret) { 4298c2ecf20Sopenharmony_ci dev_err(dev->dev, "gclk error for rate %lu: %d", 4308c2ecf20Sopenharmony_ci clk_rate, ret); 4318c2ecf20Sopenharmony_ci } else { 4328c2ecf20Sopenharmony_ci if (!best_diff_rate) { 4338c2ecf20Sopenharmony_ci dev_dbg(dev->dev, "found perfect rate on gclk: %lu\n", 4348c2ecf20Sopenharmony_ci clk_rate); 4358c2ecf20Sopenharmony_ci break; 4368c2ecf20Sopenharmony_ci } 4378c2ecf20Sopenharmony_ci } 4388c2ecf20Sopenharmony_ci 4398c2ecf20Sopenharmony_ci ret = mchp_i2s_mcc_clk_get_rate_diff(dev->pclk, clk_rate, 4408c2ecf20Sopenharmony_ci &best_clk, best_rate, 4418c2ecf20Sopenharmony_ci &best_diff_rate); 4428c2ecf20Sopenharmony_ci if (ret) { 4438c2ecf20Sopenharmony_ci dev_err(dev->dev, "pclk error for rate %lu: %d", 4448c2ecf20Sopenharmony_ci clk_rate, ret); 4458c2ecf20Sopenharmony_ci } else { 4468c2ecf20Sopenharmony_ci if (!best_diff_rate) { 4478c2ecf20Sopenharmony_ci dev_dbg(dev->dev, "found perfect rate on pclk: %lu\n", 4488c2ecf20Sopenharmony_ci clk_rate); 4498c2ecf20Sopenharmony_ci break; 4508c2ecf20Sopenharmony_ci } 4518c2ecf20Sopenharmony_ci } 4528c2ecf20Sopenharmony_ci } 4538c2ecf20Sopenharmony_ci 4548c2ecf20Sopenharmony_ci /* check if clocks returned only errors */ 4558c2ecf20Sopenharmony_ci if (!best_clk) { 4568c2ecf20Sopenharmony_ci dev_err(dev->dev, "unable to change rate to clocks\n"); 4578c2ecf20Sopenharmony_ci return -EINVAL; 4588c2ecf20Sopenharmony_ci } 4598c2ecf20Sopenharmony_ci 4608c2ecf20Sopenharmony_ci dev_dbg(dev->dev, "source CLK is %s with rate %lu, diff %lu\n", 4618c2ecf20Sopenharmony_ci best_clk == dev->pclk ? "pclk" : "gclk", 4628c2ecf20Sopenharmony_ci *best_rate, best_diff_rate); 4638c2ecf20Sopenharmony_ci 4648c2ecf20Sopenharmony_ci /* Configure divisors */ 4658c2ecf20Sopenharmony_ci if (dev->sysclk) 4668c2ecf20Sopenharmony_ci *mra |= MCHP_I2SMCC_MRA_IMCKDIV(*best_rate / (2 * sysclk)); 4678c2ecf20Sopenharmony_ci *mra |= MCHP_I2SMCC_MRA_ISCKDIV(*best_rate / (2 * bclk)); 4688c2ecf20Sopenharmony_ci 4698c2ecf20Sopenharmony_ci if (best_clk == dev->gclk) 4708c2ecf20Sopenharmony_ci *mra |= MCHP_I2SMCC_MRA_SRCCLK_GCLK; 4718c2ecf20Sopenharmony_ci else 4728c2ecf20Sopenharmony_ci *mra |= MCHP_I2SMCC_MRA_SRCCLK_PCLK; 4738c2ecf20Sopenharmony_ci 4748c2ecf20Sopenharmony_ci return 0; 4758c2ecf20Sopenharmony_ci} 4768c2ecf20Sopenharmony_ci 4778c2ecf20Sopenharmony_cistatic int mchp_i2s_mcc_is_running(struct mchp_i2s_mcc_dev *dev) 4788c2ecf20Sopenharmony_ci{ 4798c2ecf20Sopenharmony_ci u32 sr; 4808c2ecf20Sopenharmony_ci 4818c2ecf20Sopenharmony_ci regmap_read(dev->regmap, MCHP_I2SMCC_SR, &sr); 4828c2ecf20Sopenharmony_ci return !!(sr & (MCHP_I2SMCC_SR_TXEN | MCHP_I2SMCC_SR_RXEN)); 4838c2ecf20Sopenharmony_ci} 4848c2ecf20Sopenharmony_ci 4858c2ecf20Sopenharmony_cistatic int mchp_i2s_mcc_hw_params(struct snd_pcm_substream *substream, 4868c2ecf20Sopenharmony_ci struct snd_pcm_hw_params *params, 4878c2ecf20Sopenharmony_ci struct snd_soc_dai *dai) 4888c2ecf20Sopenharmony_ci{ 4898c2ecf20Sopenharmony_ci unsigned long rate = 0; 4908c2ecf20Sopenharmony_ci struct mchp_i2s_mcc_dev *dev = snd_soc_dai_get_drvdata(dai); 4918c2ecf20Sopenharmony_ci u32 mra = 0; 4928c2ecf20Sopenharmony_ci u32 mrb = 0; 4938c2ecf20Sopenharmony_ci unsigned int channels = params_channels(params); 4948c2ecf20Sopenharmony_ci unsigned int frame_length = dev->frame_length; 4958c2ecf20Sopenharmony_ci unsigned int bclk_rate; 4968c2ecf20Sopenharmony_ci int set_divs = 0; 4978c2ecf20Sopenharmony_ci int ret; 4988c2ecf20Sopenharmony_ci bool is_playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK); 4998c2ecf20Sopenharmony_ci 5008c2ecf20Sopenharmony_ci dev_dbg(dev->dev, "%s() rate=%u format=%#x width=%u channels=%u\n", 5018c2ecf20Sopenharmony_ci __func__, params_rate(params), params_format(params), 5028c2ecf20Sopenharmony_ci params_width(params), params_channels(params)); 5038c2ecf20Sopenharmony_ci 5048c2ecf20Sopenharmony_ci switch (dev->fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 5058c2ecf20Sopenharmony_ci case SND_SOC_DAIFMT_I2S: 5068c2ecf20Sopenharmony_ci if (dev->tdm_slots) { 5078c2ecf20Sopenharmony_ci dev_err(dev->dev, "I2S with TDM is not supported\n"); 5088c2ecf20Sopenharmony_ci return -EINVAL; 5098c2ecf20Sopenharmony_ci } 5108c2ecf20Sopenharmony_ci mra |= MCHP_I2SMCC_MRA_FORMAT_I2S; 5118c2ecf20Sopenharmony_ci break; 5128c2ecf20Sopenharmony_ci case SND_SOC_DAIFMT_LEFT_J: 5138c2ecf20Sopenharmony_ci if (dev->tdm_slots) { 5148c2ecf20Sopenharmony_ci dev_err(dev->dev, "Left-Justified with TDM is not supported\n"); 5158c2ecf20Sopenharmony_ci return -EINVAL; 5168c2ecf20Sopenharmony_ci } 5178c2ecf20Sopenharmony_ci mra |= MCHP_I2SMCC_MRA_FORMAT_LJ; 5188c2ecf20Sopenharmony_ci break; 5198c2ecf20Sopenharmony_ci case SND_SOC_DAIFMT_DSP_A: 5208c2ecf20Sopenharmony_ci mra |= MCHP_I2SMCC_MRA_FORMAT_TDM; 5218c2ecf20Sopenharmony_ci break; 5228c2ecf20Sopenharmony_ci default: 5238c2ecf20Sopenharmony_ci dev_err(dev->dev, "unsupported bus format\n"); 5248c2ecf20Sopenharmony_ci return -EINVAL; 5258c2ecf20Sopenharmony_ci } 5268c2ecf20Sopenharmony_ci 5278c2ecf20Sopenharmony_ci switch (dev->fmt & SND_SOC_DAIFMT_MASTER_MASK) { 5288c2ecf20Sopenharmony_ci case SND_SOC_DAIFMT_CBS_CFS: 5298c2ecf20Sopenharmony_ci /* cpu is BCLK and LRC master */ 5308c2ecf20Sopenharmony_ci mra |= MCHP_I2SMCC_MRA_MODE_MASTER; 5318c2ecf20Sopenharmony_ci if (dev->sysclk) 5328c2ecf20Sopenharmony_ci mra |= MCHP_I2SMCC_MRA_IMCKMODE_GEN; 5338c2ecf20Sopenharmony_ci set_divs = 1; 5348c2ecf20Sopenharmony_ci break; 5358c2ecf20Sopenharmony_ci case SND_SOC_DAIFMT_CBS_CFM: 5368c2ecf20Sopenharmony_ci /* cpu is BCLK master */ 5378c2ecf20Sopenharmony_ci mrb |= MCHP_I2SMCC_MRB_CLKSEL_INT; 5388c2ecf20Sopenharmony_ci set_divs = 1; 5398c2ecf20Sopenharmony_ci fallthrough; 5408c2ecf20Sopenharmony_ci case SND_SOC_DAIFMT_CBM_CFM: 5418c2ecf20Sopenharmony_ci /* cpu is slave */ 5428c2ecf20Sopenharmony_ci mra |= MCHP_I2SMCC_MRA_MODE_SLAVE; 5438c2ecf20Sopenharmony_ci if (dev->sysclk) 5448c2ecf20Sopenharmony_ci dev_warn(dev->dev, "Unable to generate MCLK in Slave mode\n"); 5458c2ecf20Sopenharmony_ci break; 5468c2ecf20Sopenharmony_ci default: 5478c2ecf20Sopenharmony_ci dev_err(dev->dev, "unsupported master/slave mode\n"); 5488c2ecf20Sopenharmony_ci return -EINVAL; 5498c2ecf20Sopenharmony_ci } 5508c2ecf20Sopenharmony_ci 5518c2ecf20Sopenharmony_ci if (dev->fmt & (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_LEFT_J)) { 5528c2ecf20Sopenharmony_ci switch (channels) { 5538c2ecf20Sopenharmony_ci case 1: 5548c2ecf20Sopenharmony_ci if (is_playback) 5558c2ecf20Sopenharmony_ci mra |= MCHP_I2SMCC_MRA_TXMONO; 5568c2ecf20Sopenharmony_ci else 5578c2ecf20Sopenharmony_ci mra |= MCHP_I2SMCC_MRA_RXMONO; 5588c2ecf20Sopenharmony_ci break; 5598c2ecf20Sopenharmony_ci case 2: 5608c2ecf20Sopenharmony_ci break; 5618c2ecf20Sopenharmony_ci default: 5628c2ecf20Sopenharmony_ci dev_err(dev->dev, "unsupported number of audio channels\n"); 5638c2ecf20Sopenharmony_ci return -EINVAL; 5648c2ecf20Sopenharmony_ci } 5658c2ecf20Sopenharmony_ci 5668c2ecf20Sopenharmony_ci if (!frame_length) 5678c2ecf20Sopenharmony_ci frame_length = 2 * params_physical_width(params); 5688c2ecf20Sopenharmony_ci } else if (dev->fmt & SND_SOC_DAIFMT_DSP_A) { 5698c2ecf20Sopenharmony_ci if (dev->tdm_slots) { 5708c2ecf20Sopenharmony_ci if (channels % 2 && channels * 2 <= dev->tdm_slots) { 5718c2ecf20Sopenharmony_ci /* 5728c2ecf20Sopenharmony_ci * Duplicate data for even-numbered channels 5738c2ecf20Sopenharmony_ci * to odd-numbered channels 5748c2ecf20Sopenharmony_ci */ 5758c2ecf20Sopenharmony_ci if (is_playback) 5768c2ecf20Sopenharmony_ci mra |= MCHP_I2SMCC_MRA_TXMONO; 5778c2ecf20Sopenharmony_ci else 5788c2ecf20Sopenharmony_ci mra |= MCHP_I2SMCC_MRA_RXMONO; 5798c2ecf20Sopenharmony_ci } 5808c2ecf20Sopenharmony_ci channels = dev->tdm_slots; 5818c2ecf20Sopenharmony_ci } 5828c2ecf20Sopenharmony_ci 5838c2ecf20Sopenharmony_ci mra |= MCHP_I2SMCC_MRA_NBCHAN(channels); 5848c2ecf20Sopenharmony_ci if (!frame_length) 5858c2ecf20Sopenharmony_ci frame_length = channels * MCHP_I2MCC_TDM_SLOT_WIDTH; 5868c2ecf20Sopenharmony_ci } 5878c2ecf20Sopenharmony_ci 5888c2ecf20Sopenharmony_ci /* 5898c2ecf20Sopenharmony_ci * We must have the same burst size configured 5908c2ecf20Sopenharmony_ci * in the DMA transfer and in out IP 5918c2ecf20Sopenharmony_ci */ 5928c2ecf20Sopenharmony_ci mrb |= MCHP_I2SMCC_MRB_DMACHUNK(channels); 5938c2ecf20Sopenharmony_ci if (is_playback) 5948c2ecf20Sopenharmony_ci dev->playback.maxburst = 1 << (fls(channels) - 1); 5958c2ecf20Sopenharmony_ci else 5968c2ecf20Sopenharmony_ci dev->capture.maxburst = 1 << (fls(channels) - 1); 5978c2ecf20Sopenharmony_ci 5988c2ecf20Sopenharmony_ci switch (params_format(params)) { 5998c2ecf20Sopenharmony_ci case SNDRV_PCM_FORMAT_S8: 6008c2ecf20Sopenharmony_ci mra |= MCHP_I2SMCC_MRA_DATALENGTH_8_BITS; 6018c2ecf20Sopenharmony_ci break; 6028c2ecf20Sopenharmony_ci case SNDRV_PCM_FORMAT_S16_LE: 6038c2ecf20Sopenharmony_ci mra |= MCHP_I2SMCC_MRA_DATALENGTH_16_BITS; 6048c2ecf20Sopenharmony_ci break; 6058c2ecf20Sopenharmony_ci case SNDRV_PCM_FORMAT_S18_3LE: 6068c2ecf20Sopenharmony_ci mra |= MCHP_I2SMCC_MRA_DATALENGTH_18_BITS | 6078c2ecf20Sopenharmony_ci MCHP_I2SMCC_MRA_IWS; 6088c2ecf20Sopenharmony_ci break; 6098c2ecf20Sopenharmony_ci case SNDRV_PCM_FORMAT_S20_3LE: 6108c2ecf20Sopenharmony_ci mra |= MCHP_I2SMCC_MRA_DATALENGTH_20_BITS | 6118c2ecf20Sopenharmony_ci MCHP_I2SMCC_MRA_IWS; 6128c2ecf20Sopenharmony_ci break; 6138c2ecf20Sopenharmony_ci case SNDRV_PCM_FORMAT_S24_3LE: 6148c2ecf20Sopenharmony_ci mra |= MCHP_I2SMCC_MRA_DATALENGTH_24_BITS | 6158c2ecf20Sopenharmony_ci MCHP_I2SMCC_MRA_IWS; 6168c2ecf20Sopenharmony_ci break; 6178c2ecf20Sopenharmony_ci case SNDRV_PCM_FORMAT_S24_LE: 6188c2ecf20Sopenharmony_ci mra |= MCHP_I2SMCC_MRA_DATALENGTH_24_BITS; 6198c2ecf20Sopenharmony_ci break; 6208c2ecf20Sopenharmony_ci case SNDRV_PCM_FORMAT_S32_LE: 6218c2ecf20Sopenharmony_ci mra |= MCHP_I2SMCC_MRA_DATALENGTH_32_BITS; 6228c2ecf20Sopenharmony_ci break; 6238c2ecf20Sopenharmony_ci default: 6248c2ecf20Sopenharmony_ci dev_err(dev->dev, "unsupported size/endianness for audio samples\n"); 6258c2ecf20Sopenharmony_ci return -EINVAL; 6268c2ecf20Sopenharmony_ci } 6278c2ecf20Sopenharmony_ci 6288c2ecf20Sopenharmony_ci if (set_divs) { 6298c2ecf20Sopenharmony_ci bclk_rate = frame_length * params_rate(params); 6308c2ecf20Sopenharmony_ci ret = mchp_i2s_mcc_config_divs(dev, bclk_rate, &mra, 6318c2ecf20Sopenharmony_ci &rate); 6328c2ecf20Sopenharmony_ci if (ret) { 6338c2ecf20Sopenharmony_ci dev_err(dev->dev, 6348c2ecf20Sopenharmony_ci "unable to configure the divisors: %d\n", ret); 6358c2ecf20Sopenharmony_ci return ret; 6368c2ecf20Sopenharmony_ci } 6378c2ecf20Sopenharmony_ci } 6388c2ecf20Sopenharmony_ci 6398c2ecf20Sopenharmony_ci /* 6408c2ecf20Sopenharmony_ci * If we are already running, the wanted setup must be 6418c2ecf20Sopenharmony_ci * the same with the one that's currently ongoing 6428c2ecf20Sopenharmony_ci */ 6438c2ecf20Sopenharmony_ci if (mchp_i2s_mcc_is_running(dev)) { 6448c2ecf20Sopenharmony_ci u32 mra_cur; 6458c2ecf20Sopenharmony_ci u32 mrb_cur; 6468c2ecf20Sopenharmony_ci 6478c2ecf20Sopenharmony_ci regmap_read(dev->regmap, MCHP_I2SMCC_MRA, &mra_cur); 6488c2ecf20Sopenharmony_ci regmap_read(dev->regmap, MCHP_I2SMCC_MRB, &mrb_cur); 6498c2ecf20Sopenharmony_ci if (mra != mra_cur || mrb != mrb_cur) 6508c2ecf20Sopenharmony_ci return -EINVAL; 6518c2ecf20Sopenharmony_ci 6528c2ecf20Sopenharmony_ci return 0; 6538c2ecf20Sopenharmony_ci } 6548c2ecf20Sopenharmony_ci 6558c2ecf20Sopenharmony_ci if (mra & MCHP_I2SMCC_MRA_SRCCLK_GCLK && !dev->gclk_use) { 6568c2ecf20Sopenharmony_ci /* set the rate */ 6578c2ecf20Sopenharmony_ci ret = clk_set_rate(dev->gclk, rate); 6588c2ecf20Sopenharmony_ci if (ret) { 6598c2ecf20Sopenharmony_ci dev_err(dev->dev, 6608c2ecf20Sopenharmony_ci "unable to set rate %lu to GCLK: %d\n", 6618c2ecf20Sopenharmony_ci rate, ret); 6628c2ecf20Sopenharmony_ci return ret; 6638c2ecf20Sopenharmony_ci } 6648c2ecf20Sopenharmony_ci 6658c2ecf20Sopenharmony_ci ret = clk_prepare(dev->gclk); 6668c2ecf20Sopenharmony_ci if (ret < 0) { 6678c2ecf20Sopenharmony_ci dev_err(dev->dev, "unable to prepare GCLK: %d\n", ret); 6688c2ecf20Sopenharmony_ci return ret; 6698c2ecf20Sopenharmony_ci } 6708c2ecf20Sopenharmony_ci dev->gclk_use = 1; 6718c2ecf20Sopenharmony_ci } 6728c2ecf20Sopenharmony_ci 6738c2ecf20Sopenharmony_ci /* Save the number of channels to know what interrupts to enable */ 6748c2ecf20Sopenharmony_ci dev->channels = channels; 6758c2ecf20Sopenharmony_ci 6768c2ecf20Sopenharmony_ci ret = regmap_write(dev->regmap, MCHP_I2SMCC_MRA, mra); 6778c2ecf20Sopenharmony_ci if (ret < 0) { 6788c2ecf20Sopenharmony_ci if (dev->gclk_use) { 6798c2ecf20Sopenharmony_ci clk_unprepare(dev->gclk); 6808c2ecf20Sopenharmony_ci dev->gclk_use = 0; 6818c2ecf20Sopenharmony_ci } 6828c2ecf20Sopenharmony_ci return ret; 6838c2ecf20Sopenharmony_ci } 6848c2ecf20Sopenharmony_ci return regmap_write(dev->regmap, MCHP_I2SMCC_MRB, mrb); 6858c2ecf20Sopenharmony_ci} 6868c2ecf20Sopenharmony_ci 6878c2ecf20Sopenharmony_cistatic int mchp_i2s_mcc_hw_free(struct snd_pcm_substream *substream, 6888c2ecf20Sopenharmony_ci struct snd_soc_dai *dai) 6898c2ecf20Sopenharmony_ci{ 6908c2ecf20Sopenharmony_ci struct mchp_i2s_mcc_dev *dev = snd_soc_dai_get_drvdata(dai); 6918c2ecf20Sopenharmony_ci bool is_playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK); 6928c2ecf20Sopenharmony_ci long err; 6938c2ecf20Sopenharmony_ci 6948c2ecf20Sopenharmony_ci if (is_playback) { 6958c2ecf20Sopenharmony_ci err = wait_event_interruptible_timeout(dev->wq_txrdy, 6968c2ecf20Sopenharmony_ci dev->tx_rdy, 6978c2ecf20Sopenharmony_ci msecs_to_jiffies(500)); 6988c2ecf20Sopenharmony_ci if (err == 0) { 6998c2ecf20Sopenharmony_ci dev_warn_once(dev->dev, 7008c2ecf20Sopenharmony_ci "Timeout waiting for Tx ready\n"); 7018c2ecf20Sopenharmony_ci regmap_write(dev->regmap, MCHP_I2SMCC_IDRA, 7028c2ecf20Sopenharmony_ci MCHP_I2SMCC_INT_TXRDY_MASK(dev->channels)); 7038c2ecf20Sopenharmony_ci dev->tx_rdy = 1; 7048c2ecf20Sopenharmony_ci } 7058c2ecf20Sopenharmony_ci } else { 7068c2ecf20Sopenharmony_ci err = wait_event_interruptible_timeout(dev->wq_rxrdy, 7078c2ecf20Sopenharmony_ci dev->rx_rdy, 7088c2ecf20Sopenharmony_ci msecs_to_jiffies(500)); 7098c2ecf20Sopenharmony_ci if (err == 0) { 7108c2ecf20Sopenharmony_ci dev_warn_once(dev->dev, 7118c2ecf20Sopenharmony_ci "Timeout waiting for Rx ready\n"); 7128c2ecf20Sopenharmony_ci regmap_write(dev->regmap, MCHP_I2SMCC_IDRA, 7138c2ecf20Sopenharmony_ci MCHP_I2SMCC_INT_RXRDY_MASK(dev->channels)); 7148c2ecf20Sopenharmony_ci dev->rx_rdy = 1; 7158c2ecf20Sopenharmony_ci } 7168c2ecf20Sopenharmony_ci } 7178c2ecf20Sopenharmony_ci 7188c2ecf20Sopenharmony_ci if (!mchp_i2s_mcc_is_running(dev)) { 7198c2ecf20Sopenharmony_ci regmap_write(dev->regmap, MCHP_I2SMCC_CR, MCHP_I2SMCC_CR_CKDIS); 7208c2ecf20Sopenharmony_ci 7218c2ecf20Sopenharmony_ci if (dev->gclk_running) { 7228c2ecf20Sopenharmony_ci clk_disable(dev->gclk); 7238c2ecf20Sopenharmony_ci dev->gclk_running = 0; 7248c2ecf20Sopenharmony_ci } 7258c2ecf20Sopenharmony_ci if (dev->gclk_use) { 7268c2ecf20Sopenharmony_ci clk_unprepare(dev->gclk); 7278c2ecf20Sopenharmony_ci dev->gclk_use = 0; 7288c2ecf20Sopenharmony_ci } 7298c2ecf20Sopenharmony_ci } 7308c2ecf20Sopenharmony_ci 7318c2ecf20Sopenharmony_ci return 0; 7328c2ecf20Sopenharmony_ci} 7338c2ecf20Sopenharmony_ci 7348c2ecf20Sopenharmony_cistatic int mchp_i2s_mcc_trigger(struct snd_pcm_substream *substream, int cmd, 7358c2ecf20Sopenharmony_ci struct snd_soc_dai *dai) 7368c2ecf20Sopenharmony_ci{ 7378c2ecf20Sopenharmony_ci struct mchp_i2s_mcc_dev *dev = snd_soc_dai_get_drvdata(dai); 7388c2ecf20Sopenharmony_ci bool is_playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK); 7398c2ecf20Sopenharmony_ci u32 cr = 0; 7408c2ecf20Sopenharmony_ci u32 iera = 0; 7418c2ecf20Sopenharmony_ci u32 sr; 7428c2ecf20Sopenharmony_ci int err; 7438c2ecf20Sopenharmony_ci 7448c2ecf20Sopenharmony_ci switch (cmd) { 7458c2ecf20Sopenharmony_ci case SNDRV_PCM_TRIGGER_START: 7468c2ecf20Sopenharmony_ci case SNDRV_PCM_TRIGGER_RESUME: 7478c2ecf20Sopenharmony_ci case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 7488c2ecf20Sopenharmony_ci if (is_playback) 7498c2ecf20Sopenharmony_ci cr = MCHP_I2SMCC_CR_TXEN | MCHP_I2SMCC_CR_CKEN; 7508c2ecf20Sopenharmony_ci else 7518c2ecf20Sopenharmony_ci cr = MCHP_I2SMCC_CR_RXEN | MCHP_I2SMCC_CR_CKEN; 7528c2ecf20Sopenharmony_ci break; 7538c2ecf20Sopenharmony_ci case SNDRV_PCM_TRIGGER_STOP: 7548c2ecf20Sopenharmony_ci case SNDRV_PCM_TRIGGER_SUSPEND: 7558c2ecf20Sopenharmony_ci case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 7568c2ecf20Sopenharmony_ci regmap_read(dev->regmap, MCHP_I2SMCC_SR, &sr); 7578c2ecf20Sopenharmony_ci if (is_playback && (sr & MCHP_I2SMCC_SR_TXEN)) { 7588c2ecf20Sopenharmony_ci cr = MCHP_I2SMCC_CR_TXDIS; 7598c2ecf20Sopenharmony_ci dev->tx_rdy = 0; 7608c2ecf20Sopenharmony_ci /* 7618c2ecf20Sopenharmony_ci * Enable Tx Ready interrupts on all channels 7628c2ecf20Sopenharmony_ci * to assure all data is sent 7638c2ecf20Sopenharmony_ci */ 7648c2ecf20Sopenharmony_ci iera = MCHP_I2SMCC_INT_TXRDY_MASK(dev->channels); 7658c2ecf20Sopenharmony_ci } else if (!is_playback && (sr & MCHP_I2SMCC_SR_RXEN)) { 7668c2ecf20Sopenharmony_ci cr = MCHP_I2SMCC_CR_RXDIS; 7678c2ecf20Sopenharmony_ci dev->rx_rdy = 0; 7688c2ecf20Sopenharmony_ci /* 7698c2ecf20Sopenharmony_ci * Enable Rx Ready interrupts on all channels 7708c2ecf20Sopenharmony_ci * to assure all data is received 7718c2ecf20Sopenharmony_ci */ 7728c2ecf20Sopenharmony_ci iera = MCHP_I2SMCC_INT_RXRDY_MASK(dev->channels); 7738c2ecf20Sopenharmony_ci } 7748c2ecf20Sopenharmony_ci break; 7758c2ecf20Sopenharmony_ci default: 7768c2ecf20Sopenharmony_ci return -EINVAL; 7778c2ecf20Sopenharmony_ci } 7788c2ecf20Sopenharmony_ci 7798c2ecf20Sopenharmony_ci if ((cr & MCHP_I2SMCC_CR_CKEN) && dev->gclk_use && 7808c2ecf20Sopenharmony_ci !dev->gclk_running) { 7818c2ecf20Sopenharmony_ci err = clk_enable(dev->gclk); 7828c2ecf20Sopenharmony_ci if (err) { 7838c2ecf20Sopenharmony_ci dev_err_once(dev->dev, "failed to enable GCLK: %d\n", 7848c2ecf20Sopenharmony_ci err); 7858c2ecf20Sopenharmony_ci } else { 7868c2ecf20Sopenharmony_ci dev->gclk_running = 1; 7878c2ecf20Sopenharmony_ci } 7888c2ecf20Sopenharmony_ci } 7898c2ecf20Sopenharmony_ci 7908c2ecf20Sopenharmony_ci regmap_write(dev->regmap, MCHP_I2SMCC_IERA, iera); 7918c2ecf20Sopenharmony_ci regmap_write(dev->regmap, MCHP_I2SMCC_CR, cr); 7928c2ecf20Sopenharmony_ci 7938c2ecf20Sopenharmony_ci return 0; 7948c2ecf20Sopenharmony_ci} 7958c2ecf20Sopenharmony_ci 7968c2ecf20Sopenharmony_cistatic int mchp_i2s_mcc_startup(struct snd_pcm_substream *substream, 7978c2ecf20Sopenharmony_ci struct snd_soc_dai *dai) 7988c2ecf20Sopenharmony_ci{ 7998c2ecf20Sopenharmony_ci struct mchp_i2s_mcc_dev *dev = snd_soc_dai_get_drvdata(dai); 8008c2ecf20Sopenharmony_ci 8018c2ecf20Sopenharmony_ci /* Software reset the IP if it's not running */ 8028c2ecf20Sopenharmony_ci if (!mchp_i2s_mcc_is_running(dev)) { 8038c2ecf20Sopenharmony_ci return regmap_write(dev->regmap, MCHP_I2SMCC_CR, 8048c2ecf20Sopenharmony_ci MCHP_I2SMCC_CR_SWRST); 8058c2ecf20Sopenharmony_ci } 8068c2ecf20Sopenharmony_ci 8078c2ecf20Sopenharmony_ci return 0; 8088c2ecf20Sopenharmony_ci} 8098c2ecf20Sopenharmony_ci 8108c2ecf20Sopenharmony_cistatic const struct snd_soc_dai_ops mchp_i2s_mcc_dai_ops = { 8118c2ecf20Sopenharmony_ci .set_sysclk = mchp_i2s_mcc_set_sysclk, 8128c2ecf20Sopenharmony_ci .set_bclk_ratio = mchp_i2s_mcc_set_bclk_ratio, 8138c2ecf20Sopenharmony_ci .startup = mchp_i2s_mcc_startup, 8148c2ecf20Sopenharmony_ci .trigger = mchp_i2s_mcc_trigger, 8158c2ecf20Sopenharmony_ci .hw_params = mchp_i2s_mcc_hw_params, 8168c2ecf20Sopenharmony_ci .hw_free = mchp_i2s_mcc_hw_free, 8178c2ecf20Sopenharmony_ci .set_fmt = mchp_i2s_mcc_set_dai_fmt, 8188c2ecf20Sopenharmony_ci .set_tdm_slot = mchp_i2s_mcc_set_dai_tdm_slot, 8198c2ecf20Sopenharmony_ci}; 8208c2ecf20Sopenharmony_ci 8218c2ecf20Sopenharmony_cistatic int mchp_i2s_mcc_dai_probe(struct snd_soc_dai *dai) 8228c2ecf20Sopenharmony_ci{ 8238c2ecf20Sopenharmony_ci struct mchp_i2s_mcc_dev *dev = snd_soc_dai_get_drvdata(dai); 8248c2ecf20Sopenharmony_ci 8258c2ecf20Sopenharmony_ci init_waitqueue_head(&dev->wq_txrdy); 8268c2ecf20Sopenharmony_ci init_waitqueue_head(&dev->wq_rxrdy); 8278c2ecf20Sopenharmony_ci dev->tx_rdy = 1; 8288c2ecf20Sopenharmony_ci dev->rx_rdy = 1; 8298c2ecf20Sopenharmony_ci 8308c2ecf20Sopenharmony_ci snd_soc_dai_init_dma_data(dai, &dev->playback, &dev->capture); 8318c2ecf20Sopenharmony_ci 8328c2ecf20Sopenharmony_ci return 0; 8338c2ecf20Sopenharmony_ci} 8348c2ecf20Sopenharmony_ci 8358c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_RATES SNDRV_PCM_RATE_8000_192000 8368c2ecf20Sopenharmony_ci 8378c2ecf20Sopenharmony_ci#define MCHP_I2SMCC_FORMATS (SNDRV_PCM_FMTBIT_S8 | \ 8388c2ecf20Sopenharmony_ci SNDRV_PCM_FMTBIT_S16_LE | \ 8398c2ecf20Sopenharmony_ci SNDRV_PCM_FMTBIT_S18_3LE | \ 8408c2ecf20Sopenharmony_ci SNDRV_PCM_FMTBIT_S20_3LE | \ 8418c2ecf20Sopenharmony_ci SNDRV_PCM_FMTBIT_S24_3LE | \ 8428c2ecf20Sopenharmony_ci SNDRV_PCM_FMTBIT_S24_LE | \ 8438c2ecf20Sopenharmony_ci SNDRV_PCM_FMTBIT_S32_LE) 8448c2ecf20Sopenharmony_ci 8458c2ecf20Sopenharmony_cistatic struct snd_soc_dai_driver mchp_i2s_mcc_dai = { 8468c2ecf20Sopenharmony_ci .probe = mchp_i2s_mcc_dai_probe, 8478c2ecf20Sopenharmony_ci .playback = { 8488c2ecf20Sopenharmony_ci .stream_name = "I2SMCC-Playback", 8498c2ecf20Sopenharmony_ci .channels_min = 1, 8508c2ecf20Sopenharmony_ci .channels_max = 8, 8518c2ecf20Sopenharmony_ci .rates = MCHP_I2SMCC_RATES, 8528c2ecf20Sopenharmony_ci .formats = MCHP_I2SMCC_FORMATS, 8538c2ecf20Sopenharmony_ci }, 8548c2ecf20Sopenharmony_ci .capture = { 8558c2ecf20Sopenharmony_ci .stream_name = "I2SMCC-Capture", 8568c2ecf20Sopenharmony_ci .channels_min = 1, 8578c2ecf20Sopenharmony_ci .channels_max = 8, 8588c2ecf20Sopenharmony_ci .rates = MCHP_I2SMCC_RATES, 8598c2ecf20Sopenharmony_ci .formats = MCHP_I2SMCC_FORMATS, 8608c2ecf20Sopenharmony_ci }, 8618c2ecf20Sopenharmony_ci .ops = &mchp_i2s_mcc_dai_ops, 8628c2ecf20Sopenharmony_ci .symmetric_rates = 1, 8638c2ecf20Sopenharmony_ci .symmetric_samplebits = 1, 8648c2ecf20Sopenharmony_ci .symmetric_channels = 1, 8658c2ecf20Sopenharmony_ci}; 8668c2ecf20Sopenharmony_ci 8678c2ecf20Sopenharmony_cistatic const struct snd_soc_component_driver mchp_i2s_mcc_component = { 8688c2ecf20Sopenharmony_ci .name = "mchp-i2s-mcc", 8698c2ecf20Sopenharmony_ci}; 8708c2ecf20Sopenharmony_ci 8718c2ecf20Sopenharmony_ci#ifdef CONFIG_OF 8728c2ecf20Sopenharmony_cistatic const struct of_device_id mchp_i2s_mcc_dt_ids[] = { 8738c2ecf20Sopenharmony_ci { 8748c2ecf20Sopenharmony_ci .compatible = "microchip,sam9x60-i2smcc", 8758c2ecf20Sopenharmony_ci }, 8768c2ecf20Sopenharmony_ci { /* sentinel */ } 8778c2ecf20Sopenharmony_ci}; 8788c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, mchp_i2s_mcc_dt_ids); 8798c2ecf20Sopenharmony_ci#endif 8808c2ecf20Sopenharmony_ci 8818c2ecf20Sopenharmony_cistatic int mchp_i2s_mcc_probe(struct platform_device *pdev) 8828c2ecf20Sopenharmony_ci{ 8838c2ecf20Sopenharmony_ci struct mchp_i2s_mcc_dev *dev; 8848c2ecf20Sopenharmony_ci struct resource *mem; 8858c2ecf20Sopenharmony_ci struct regmap *regmap; 8868c2ecf20Sopenharmony_ci void __iomem *base; 8878c2ecf20Sopenharmony_ci u32 version; 8888c2ecf20Sopenharmony_ci int irq; 8898c2ecf20Sopenharmony_ci int err; 8908c2ecf20Sopenharmony_ci 8918c2ecf20Sopenharmony_ci dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL); 8928c2ecf20Sopenharmony_ci if (!dev) 8938c2ecf20Sopenharmony_ci return -ENOMEM; 8948c2ecf20Sopenharmony_ci 8958c2ecf20Sopenharmony_ci mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); 8968c2ecf20Sopenharmony_ci base = devm_ioremap_resource(&pdev->dev, mem); 8978c2ecf20Sopenharmony_ci if (IS_ERR(base)) 8988c2ecf20Sopenharmony_ci return PTR_ERR(base); 8998c2ecf20Sopenharmony_ci 9008c2ecf20Sopenharmony_ci regmap = devm_regmap_init_mmio(&pdev->dev, base, 9018c2ecf20Sopenharmony_ci &mchp_i2s_mcc_regmap_config); 9028c2ecf20Sopenharmony_ci if (IS_ERR(regmap)) 9038c2ecf20Sopenharmony_ci return PTR_ERR(regmap); 9048c2ecf20Sopenharmony_ci 9058c2ecf20Sopenharmony_ci irq = platform_get_irq(pdev, 0); 9068c2ecf20Sopenharmony_ci if (irq < 0) 9078c2ecf20Sopenharmony_ci return irq; 9088c2ecf20Sopenharmony_ci 9098c2ecf20Sopenharmony_ci err = devm_request_irq(&pdev->dev, irq, mchp_i2s_mcc_interrupt, 0, 9108c2ecf20Sopenharmony_ci dev_name(&pdev->dev), dev); 9118c2ecf20Sopenharmony_ci if (err) 9128c2ecf20Sopenharmony_ci return err; 9138c2ecf20Sopenharmony_ci 9148c2ecf20Sopenharmony_ci dev->pclk = devm_clk_get(&pdev->dev, "pclk"); 9158c2ecf20Sopenharmony_ci if (IS_ERR(dev->pclk)) { 9168c2ecf20Sopenharmony_ci err = PTR_ERR(dev->pclk); 9178c2ecf20Sopenharmony_ci dev_err(&pdev->dev, 9188c2ecf20Sopenharmony_ci "failed to get the peripheral clock: %d\n", err); 9198c2ecf20Sopenharmony_ci return err; 9208c2ecf20Sopenharmony_ci } 9218c2ecf20Sopenharmony_ci 9228c2ecf20Sopenharmony_ci /* Get the optional generated clock */ 9238c2ecf20Sopenharmony_ci dev->gclk = devm_clk_get(&pdev->dev, "gclk"); 9248c2ecf20Sopenharmony_ci if (IS_ERR(dev->gclk)) { 9258c2ecf20Sopenharmony_ci if (PTR_ERR(dev->gclk) == -EPROBE_DEFER) 9268c2ecf20Sopenharmony_ci return -EPROBE_DEFER; 9278c2ecf20Sopenharmony_ci dev_warn(&pdev->dev, 9288c2ecf20Sopenharmony_ci "generated clock not found: %d\n", err); 9298c2ecf20Sopenharmony_ci dev->gclk = NULL; 9308c2ecf20Sopenharmony_ci } 9318c2ecf20Sopenharmony_ci 9328c2ecf20Sopenharmony_ci dev->dev = &pdev->dev; 9338c2ecf20Sopenharmony_ci dev->regmap = regmap; 9348c2ecf20Sopenharmony_ci platform_set_drvdata(pdev, dev); 9358c2ecf20Sopenharmony_ci 9368c2ecf20Sopenharmony_ci err = clk_prepare_enable(dev->pclk); 9378c2ecf20Sopenharmony_ci if (err) { 9388c2ecf20Sopenharmony_ci dev_err(&pdev->dev, 9398c2ecf20Sopenharmony_ci "failed to enable the peripheral clock: %d\n", err); 9408c2ecf20Sopenharmony_ci return err; 9418c2ecf20Sopenharmony_ci } 9428c2ecf20Sopenharmony_ci 9438c2ecf20Sopenharmony_ci err = devm_snd_soc_register_component(&pdev->dev, 9448c2ecf20Sopenharmony_ci &mchp_i2s_mcc_component, 9458c2ecf20Sopenharmony_ci &mchp_i2s_mcc_dai, 1); 9468c2ecf20Sopenharmony_ci if (err) { 9478c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "failed to register DAI: %d\n", err); 9488c2ecf20Sopenharmony_ci clk_disable_unprepare(dev->pclk); 9498c2ecf20Sopenharmony_ci return err; 9508c2ecf20Sopenharmony_ci } 9518c2ecf20Sopenharmony_ci 9528c2ecf20Sopenharmony_ci dev->playback.addr = (dma_addr_t)mem->start + MCHP_I2SMCC_THR; 9538c2ecf20Sopenharmony_ci dev->capture.addr = (dma_addr_t)mem->start + MCHP_I2SMCC_RHR; 9548c2ecf20Sopenharmony_ci 9558c2ecf20Sopenharmony_ci err = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0); 9568c2ecf20Sopenharmony_ci if (err) { 9578c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "failed to register PCM: %d\n", err); 9588c2ecf20Sopenharmony_ci clk_disable_unprepare(dev->pclk); 9598c2ecf20Sopenharmony_ci return err; 9608c2ecf20Sopenharmony_ci } 9618c2ecf20Sopenharmony_ci 9628c2ecf20Sopenharmony_ci /* Get IP version. */ 9638c2ecf20Sopenharmony_ci regmap_read(dev->regmap, MCHP_I2SMCC_VERSION, &version); 9648c2ecf20Sopenharmony_ci dev_info(&pdev->dev, "hw version: %#lx\n", 9658c2ecf20Sopenharmony_ci version & MCHP_I2SMCC_VERSION_MASK); 9668c2ecf20Sopenharmony_ci 9678c2ecf20Sopenharmony_ci return 0; 9688c2ecf20Sopenharmony_ci} 9698c2ecf20Sopenharmony_ci 9708c2ecf20Sopenharmony_cistatic int mchp_i2s_mcc_remove(struct platform_device *pdev) 9718c2ecf20Sopenharmony_ci{ 9728c2ecf20Sopenharmony_ci struct mchp_i2s_mcc_dev *dev = platform_get_drvdata(pdev); 9738c2ecf20Sopenharmony_ci 9748c2ecf20Sopenharmony_ci clk_disable_unprepare(dev->pclk); 9758c2ecf20Sopenharmony_ci 9768c2ecf20Sopenharmony_ci return 0; 9778c2ecf20Sopenharmony_ci} 9788c2ecf20Sopenharmony_ci 9798c2ecf20Sopenharmony_cistatic struct platform_driver mchp_i2s_mcc_driver = { 9808c2ecf20Sopenharmony_ci .driver = { 9818c2ecf20Sopenharmony_ci .name = "mchp_i2s_mcc", 9828c2ecf20Sopenharmony_ci .of_match_table = of_match_ptr(mchp_i2s_mcc_dt_ids), 9838c2ecf20Sopenharmony_ci }, 9848c2ecf20Sopenharmony_ci .probe = mchp_i2s_mcc_probe, 9858c2ecf20Sopenharmony_ci .remove = mchp_i2s_mcc_remove, 9868c2ecf20Sopenharmony_ci}; 9878c2ecf20Sopenharmony_cimodule_platform_driver(mchp_i2s_mcc_driver); 9888c2ecf20Sopenharmony_ci 9898c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Microchip I2S Multi-Channel Controller driver"); 9908c2ecf20Sopenharmony_ciMODULE_AUTHOR("Codrin Ciubotariu <codrin.ciubotariu@microchip.com>"); 9918c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 992