Linux Audio

Check our new training course

Loading...
v6.13.7
   1// SPDX-License-Identifier: GPL-2.0-only
   2// Copyright (C) 2014-2015 Broadcom Corporation
 
 
 
 
 
 
 
 
 
 
   3#include <linux/clk.h>
   4#include <linux/delay.h>
   5#include <linux/init.h>
   6#include <linux/io.h>
   7#include <linux/module.h>
   8#include <linux/of.h>
   9#include <linux/slab.h>
  10#include <sound/core.h>
  11#include <sound/pcm.h>
  12#include <sound/pcm_params.h>
  13#include <sound/soc.h>
  14#include <sound/soc-dai.h>
  15
  16#include "cygnus-ssp.h"
  17
  18#define DEFAULT_VCO    1354750204
  19
  20#define CAPTURE_FCI_ID_BASE 0x180
  21#define CYGNUS_SSP_TRISTATE_MASK 0x001fff
  22#define CYGNUS_PLLCLKSEL_MASK 0xf
  23
  24/* Used with stream_on field to indicate which streams are active */
  25#define  PLAYBACK_STREAM_MASK   BIT(0)
  26#define  CAPTURE_STREAM_MASK    BIT(1)
  27
  28#define I2S_STREAM_CFG_MASK      0xff003ff
  29#define I2S_CAP_STREAM_CFG_MASK  0xf0
  30#define SPDIF_STREAM_CFG_MASK    0x3ff
  31#define CH_GRP_STEREO            0x1
  32
  33/* Begin register offset defines */
  34#define AUD_MISC_SEROUT_OE_REG_BASE  0x01c
  35#define AUD_MISC_SEROUT_SPDIF_OE  12
  36#define AUD_MISC_SEROUT_MCLK_OE   3
  37#define AUD_MISC_SEROUT_LRCK_OE   2
  38#define AUD_MISC_SEROUT_SCLK_OE   1
  39#define AUD_MISC_SEROUT_SDAT_OE   0
  40
  41/* AUD_FMM_BF_CTRL_xxx regs */
  42#define BF_DST_CFG0_OFFSET  0x100
  43#define BF_DST_CFG1_OFFSET  0x104
  44#define BF_DST_CFG2_OFFSET  0x108
  45
  46#define BF_DST_CTRL0_OFFSET 0x130
  47#define BF_DST_CTRL1_OFFSET 0x134
  48#define BF_DST_CTRL2_OFFSET 0x138
  49
  50#define BF_SRC_CFG0_OFFSET  0x148
  51#define BF_SRC_CFG1_OFFSET  0x14c
  52#define BF_SRC_CFG2_OFFSET  0x150
  53#define BF_SRC_CFG3_OFFSET  0x154
  54
  55#define BF_SRC_CTRL0_OFFSET 0x1c0
  56#define BF_SRC_CTRL1_OFFSET 0x1c4
  57#define BF_SRC_CTRL2_OFFSET 0x1c8
  58#define BF_SRC_CTRL3_OFFSET 0x1cc
  59
  60#define BF_SRC_GRP0_OFFSET  0x1fc
  61#define BF_SRC_GRP1_OFFSET  0x200
  62#define BF_SRC_GRP2_OFFSET  0x204
  63#define BF_SRC_GRP3_OFFSET  0x208
  64
  65#define BF_SRC_GRP_EN_OFFSET        0x320
  66#define BF_SRC_GRP_FLOWON_OFFSET    0x324
  67#define BF_SRC_GRP_SYNC_DIS_OFFSET  0x328
  68
  69/* AUD_FMM_IOP_OUT_I2S_xxx regs */
  70#define OUT_I2S_0_STREAM_CFG_OFFSET 0xa00
  71#define OUT_I2S_0_CFG_OFFSET        0xa04
  72#define OUT_I2S_0_MCLK_CFG_OFFSET   0xa0c
  73
  74#define OUT_I2S_1_STREAM_CFG_OFFSET 0xa40
  75#define OUT_I2S_1_CFG_OFFSET        0xa44
  76#define OUT_I2S_1_MCLK_CFG_OFFSET   0xa4c
  77
  78#define OUT_I2S_2_STREAM_CFG_OFFSET 0xa80
  79#define OUT_I2S_2_CFG_OFFSET        0xa84
  80#define OUT_I2S_2_MCLK_CFG_OFFSET   0xa8c
  81
  82/* AUD_FMM_IOP_OUT_SPDIF_xxx regs */
  83#define SPDIF_STREAM_CFG_OFFSET  0xac0
  84#define SPDIF_CTRL_OFFSET        0xac4
  85#define SPDIF_FORMAT_CFG_OFFSET  0xad8
  86#define SPDIF_MCLK_CFG_OFFSET    0xadc
  87
  88/* AUD_FMM_IOP_PLL_0_xxx regs */
  89#define IOP_PLL_0_MACRO_OFFSET    0xb00
  90#define IOP_PLL_0_MDIV_Ch0_OFFSET 0xb14
  91#define IOP_PLL_0_MDIV_Ch1_OFFSET 0xb18
  92#define IOP_PLL_0_MDIV_Ch2_OFFSET 0xb1c
  93
  94#define IOP_PLL_0_ACTIVE_MDIV_Ch0_OFFSET 0xb30
  95#define IOP_PLL_0_ACTIVE_MDIV_Ch1_OFFSET 0xb34
  96#define IOP_PLL_0_ACTIVE_MDIV_Ch2_OFFSET 0xb38
  97
  98/* AUD_FMM_IOP_xxx regs */
  99#define IOP_PLL_0_CONTROL_OFFSET     0xb04
 100#define IOP_PLL_0_USER_NDIV_OFFSET   0xb08
 101#define IOP_PLL_0_ACTIVE_NDIV_OFFSET 0xb20
 102#define IOP_PLL_0_RESET_OFFSET       0xb5c
 103
 104/* AUD_FMM_IOP_IN_I2S_xxx regs */
 105#define IN_I2S_0_STREAM_CFG_OFFSET 0x00
 106#define IN_I2S_0_CFG_OFFSET        0x04
 107#define IN_I2S_1_STREAM_CFG_OFFSET 0x40
 108#define IN_I2S_1_CFG_OFFSET        0x44
 109#define IN_I2S_2_STREAM_CFG_OFFSET 0x80
 110#define IN_I2S_2_CFG_OFFSET        0x84
 111
 112/* AUD_FMM_IOP_MISC_xxx regs */
 113#define IOP_SW_INIT_LOGIC          0x1c0
 114
 115/* End register offset defines */
 116
 117
 118/* AUD_FMM_IOP_OUT_I2S_x_MCLK_CFG_0_REG */
 119#define I2S_OUT_MCLKRATE_SHIFT 16
 120
 121/* AUD_FMM_IOP_OUT_I2S_x_MCLK_CFG_REG */
 122#define I2S_OUT_PLLCLKSEL_SHIFT  0
 123
 124/* AUD_FMM_IOP_OUT_I2S_x_STREAM_CFG */
 125#define I2S_OUT_STREAM_ENA  31
 126#define I2S_OUT_STREAM_CFG_GROUP_ID  20
 127#define I2S_OUT_STREAM_CFG_CHANNEL_GROUPING  24
 128
 129/* AUD_FMM_IOP_IN_I2S_x_CAP */
 130#define I2S_IN_STREAM_CFG_CAP_ENA   31
 131#define I2S_IN_STREAM_CFG_0_GROUP_ID 4
 132
 133/* AUD_FMM_IOP_OUT_I2S_x_I2S_CFG_REG */
 134#define I2S_OUT_CFGX_CLK_ENA         0
 135#define I2S_OUT_CFGX_DATA_ENABLE     1
 136#define I2S_OUT_CFGX_DATA_ALIGNMENT  6
 137#define I2S_OUT_CFGX_BITS_PER_SLOT  13
 138#define I2S_OUT_CFGX_VALID_SLOT     14
 139#define I2S_OUT_CFGX_FSYNC_WIDTH    18
 140#define I2S_OUT_CFGX_SCLKS_PER_1FS_DIV32 26
 141#define I2S_OUT_CFGX_SLAVE_MODE     30
 142#define I2S_OUT_CFGX_TDM_MODE       31
 143
 144/* AUD_FMM_BF_CTRL_SOURCECH_CFGx_REG */
 145#define BF_SRC_CFGX_SFIFO_ENA              0
 146#define BF_SRC_CFGX_BUFFER_PAIR_ENABLE     1
 147#define BF_SRC_CFGX_SAMPLE_CH_MODE         2
 148#define BF_SRC_CFGX_SFIFO_SZ_DOUBLE        5
 149#define BF_SRC_CFGX_NOT_PAUSE_WHEN_EMPTY  10
 150#define BF_SRC_CFGX_BIT_RES               20
 151#define BF_SRC_CFGX_PROCESS_SEQ_ID_VALID  31
 152
 153/* AUD_FMM_BF_CTRL_DESTCH_CFGx_REG */
 154#define BF_DST_CFGX_CAP_ENA              0
 155#define BF_DST_CFGX_BUFFER_PAIR_ENABLE   1
 156#define BF_DST_CFGX_DFIFO_SZ_DOUBLE      2
 157#define BF_DST_CFGX_NOT_PAUSE_WHEN_FULL 11
 158#define BF_DST_CFGX_FCI_ID              12
 159#define BF_DST_CFGX_CAP_MODE            24
 160#define BF_DST_CFGX_PROC_SEQ_ID_VALID   31
 161
 162/* AUD_FMM_IOP_OUT_SPDIF_xxx */
 163#define SPDIF_0_OUT_DITHER_ENA     3
 164#define SPDIF_0_OUT_STREAM_ENA    31
 165
 166/* AUD_FMM_IOP_PLL_0_USER */
 167#define IOP_PLL_0_USER_NDIV_FRAC   10
 168
 169/* AUD_FMM_IOP_PLL_0_ACTIVE */
 170#define IOP_PLL_0_ACTIVE_NDIV_FRAC 10
 171
 172
 173#define INIT_SSP_REGS(num) (struct cygnus_ssp_regs){ \
 174		.i2s_stream_cfg = OUT_I2S_ ##num## _STREAM_CFG_OFFSET, \
 175		.i2s_cap_stream_cfg = IN_I2S_ ##num## _STREAM_CFG_OFFSET, \
 176		.i2s_cfg = OUT_I2S_ ##num## _CFG_OFFSET, \
 177		.i2s_cap_cfg = IN_I2S_ ##num## _CFG_OFFSET, \
 178		.i2s_mclk_cfg = OUT_I2S_ ##num## _MCLK_CFG_OFFSET, \
 179		.bf_destch_ctrl = BF_DST_CTRL ##num## _OFFSET, \
 180		.bf_destch_cfg = BF_DST_CFG ##num## _OFFSET, \
 181		.bf_sourcech_ctrl = BF_SRC_CTRL ##num## _OFFSET, \
 182		.bf_sourcech_cfg = BF_SRC_CFG ##num## _OFFSET, \
 183		.bf_sourcech_grp = BF_SRC_GRP ##num## _OFFSET \
 184}
 185
 186struct pll_macro_entry {
 187	u32 mclk;
 188	u32 pll_ch_num;
 189};
 190
 191/*
 192 * PLL has 3 output channels (1x, 2x, and 4x). Below are
 193 * the common MCLK frequencies used by audio driver
 194 */
 195static const struct pll_macro_entry pll_predef_mclk[] = {
 196	{ 4096000, 0},
 197	{ 8192000, 1},
 198	{16384000, 2},
 199
 200	{ 5644800, 0},
 201	{11289600, 1},
 202	{22579200, 2},
 203
 204	{ 6144000, 0},
 205	{12288000, 1},
 206	{24576000, 2},
 207
 208	{12288000, 0},
 209	{24576000, 1},
 210	{49152000, 2},
 211
 212	{22579200, 0},
 213	{45158400, 1},
 214	{90316800, 2},
 215
 216	{24576000, 0},
 217	{49152000, 1},
 218	{98304000, 2},
 219};
 220
 221#define CYGNUS_RATE_MIN     8000
 222#define CYGNUS_RATE_MAX   384000
 223
 224/* List of valid frame sizes for tdm mode */
 225static const int ssp_valid_tdm_framesize[] = {32, 64, 128, 256, 512};
 226
 227static const unsigned int cygnus_rates[] = {
 228	 8000, 11025,  16000,  22050,  32000,  44100, 48000,
 229	88200, 96000, 176400, 192000, 352800, 384000
 230};
 231
 232static const struct snd_pcm_hw_constraint_list cygnus_rate_constraint = {
 233	.count = ARRAY_SIZE(cygnus_rates),
 234	.list = cygnus_rates,
 235};
 236
 237static struct cygnus_aio_port *cygnus_dai_get_portinfo(struct snd_soc_dai *dai)
 238{
 239	struct cygnus_audio *cygaud = snd_soc_dai_get_drvdata(dai);
 240
 241	return &cygaud->portinfo[dai->id];
 242}
 243
 244static int audio_ssp_init_portregs(struct cygnus_aio_port *aio)
 245{
 246	u32 value, fci_id;
 247	int status = 0;
 248
 249	switch (aio->port_type) {
 250	case PORT_TDM:
 251		value = readl(aio->cygaud->audio + aio->regs.i2s_stream_cfg);
 252		value &= ~I2S_STREAM_CFG_MASK;
 253
 254		/* Set Group ID */
 255		writel(aio->portnum,
 256			aio->cygaud->audio + aio->regs.bf_sourcech_grp);
 257
 258		/* Configure the AUD_FMM_IOP_OUT_I2S_x_STREAM_CFG reg */
 259		value |= aio->portnum << I2S_OUT_STREAM_CFG_GROUP_ID;
 260		value |= aio->portnum; /* FCI ID is the port num */
 261		value |= CH_GRP_STEREO << I2S_OUT_STREAM_CFG_CHANNEL_GROUPING;
 262		writel(value, aio->cygaud->audio + aio->regs.i2s_stream_cfg);
 263
 264		/* Configure the AUD_FMM_BF_CTRL_SOURCECH_CFGX reg */
 265		value = readl(aio->cygaud->audio + aio->regs.bf_sourcech_cfg);
 266		value &= ~BIT(BF_SRC_CFGX_NOT_PAUSE_WHEN_EMPTY);
 267		value |= BIT(BF_SRC_CFGX_SFIFO_SZ_DOUBLE);
 268		value |= BIT(BF_SRC_CFGX_PROCESS_SEQ_ID_VALID);
 269		writel(value, aio->cygaud->audio + aio->regs.bf_sourcech_cfg);
 270
 271		/* Configure the AUD_FMM_IOP_IN_I2S_x_CAP_STREAM_CFG_0 reg */
 272		value = readl(aio->cygaud->i2s_in +
 273			aio->regs.i2s_cap_stream_cfg);
 274		value &= ~I2S_CAP_STREAM_CFG_MASK;
 275		value |= aio->portnum << I2S_IN_STREAM_CFG_0_GROUP_ID;
 276		writel(value, aio->cygaud->i2s_in +
 277			aio->regs.i2s_cap_stream_cfg);
 278
 279		/* Configure the AUD_FMM_BF_CTRL_DESTCH_CFGX_REG_BASE reg */
 280		fci_id = CAPTURE_FCI_ID_BASE + aio->portnum;
 281
 282		value = readl(aio->cygaud->audio + aio->regs.bf_destch_cfg);
 283		value |= BIT(BF_DST_CFGX_DFIFO_SZ_DOUBLE);
 284		value &= ~BIT(BF_DST_CFGX_NOT_PAUSE_WHEN_FULL);
 285		value |= (fci_id << BF_DST_CFGX_FCI_ID);
 286		value |= BIT(BF_DST_CFGX_PROC_SEQ_ID_VALID);
 287		writel(value, aio->cygaud->audio + aio->regs.bf_destch_cfg);
 288
 289		/* Enable the transmit pin for this port */
 290		value = readl(aio->cygaud->audio + AUD_MISC_SEROUT_OE_REG_BASE);
 291		value &= ~BIT((aio->portnum * 4) + AUD_MISC_SEROUT_SDAT_OE);
 292		writel(value, aio->cygaud->audio + AUD_MISC_SEROUT_OE_REG_BASE);
 293		break;
 294	case PORT_SPDIF:
 295		writel(aio->portnum, aio->cygaud->audio + BF_SRC_GRP3_OFFSET);
 296
 297		value = readl(aio->cygaud->audio + SPDIF_CTRL_OFFSET);
 298		value |= BIT(SPDIF_0_OUT_DITHER_ENA);
 299		writel(value, aio->cygaud->audio + SPDIF_CTRL_OFFSET);
 300
 301		/* Enable and set the FCI ID for the SPDIF channel */
 302		value = readl(aio->cygaud->audio + SPDIF_STREAM_CFG_OFFSET);
 303		value &= ~SPDIF_STREAM_CFG_MASK;
 304		value |= aio->portnum; /* FCI ID is the port num */
 305		value |= BIT(SPDIF_0_OUT_STREAM_ENA);
 306		writel(value, aio->cygaud->audio + SPDIF_STREAM_CFG_OFFSET);
 307
 308		value = readl(aio->cygaud->audio + aio->regs.bf_sourcech_cfg);
 309		value &= ~BIT(BF_SRC_CFGX_NOT_PAUSE_WHEN_EMPTY);
 310		value |= BIT(BF_SRC_CFGX_SFIFO_SZ_DOUBLE);
 311		value |= BIT(BF_SRC_CFGX_PROCESS_SEQ_ID_VALID);
 312		writel(value, aio->cygaud->audio + aio->regs.bf_sourcech_cfg);
 313
 314		/* Enable the spdif output pin */
 315		value = readl(aio->cygaud->audio + AUD_MISC_SEROUT_OE_REG_BASE);
 316		value &= ~BIT(AUD_MISC_SEROUT_SPDIF_OE);
 317		writel(value, aio->cygaud->audio + AUD_MISC_SEROUT_OE_REG_BASE);
 318		break;
 319	default:
 320		dev_err(aio->cygaud->dev, "Port not supported\n");
 321		status = -EINVAL;
 322	}
 323
 324	return status;
 325}
 326
 327static void audio_ssp_in_enable(struct cygnus_aio_port *aio)
 328{
 329	u32 value;
 330
 331	value = readl(aio->cygaud->audio + aio->regs.bf_destch_cfg);
 332	value |= BIT(BF_DST_CFGX_CAP_ENA);
 333	writel(value, aio->cygaud->audio + aio->regs.bf_destch_cfg);
 334
 335	writel(0x1, aio->cygaud->audio + aio->regs.bf_destch_ctrl);
 336
 337	value = readl(aio->cygaud->audio + aio->regs.i2s_cfg);
 338	value |= BIT(I2S_OUT_CFGX_CLK_ENA);
 339	value |= BIT(I2S_OUT_CFGX_DATA_ENABLE);
 340	writel(value, aio->cygaud->audio + aio->regs.i2s_cfg);
 341
 342	value = readl(aio->cygaud->i2s_in + aio->regs.i2s_cap_stream_cfg);
 343	value |= BIT(I2S_IN_STREAM_CFG_CAP_ENA);
 344	writel(value, aio->cygaud->i2s_in + aio->regs.i2s_cap_stream_cfg);
 345
 346	aio->streams_on |= CAPTURE_STREAM_MASK;
 347}
 348
 349static void audio_ssp_in_disable(struct cygnus_aio_port *aio)
 350{
 351	u32 value;
 352
 353	value = readl(aio->cygaud->i2s_in + aio->regs.i2s_cap_stream_cfg);
 354	value &= ~BIT(I2S_IN_STREAM_CFG_CAP_ENA);
 355	writel(value, aio->cygaud->i2s_in + aio->regs.i2s_cap_stream_cfg);
 356
 357	aio->streams_on &= ~CAPTURE_STREAM_MASK;
 358
 359	/* If both playback and capture are off */
 360	if (!aio->streams_on) {
 361		value = readl(aio->cygaud->audio + aio->regs.i2s_cfg);
 362		value &= ~BIT(I2S_OUT_CFGX_CLK_ENA);
 363		value &= ~BIT(I2S_OUT_CFGX_DATA_ENABLE);
 364		writel(value, aio->cygaud->audio + aio->regs.i2s_cfg);
 365	}
 366
 367	writel(0x0, aio->cygaud->audio + aio->regs.bf_destch_ctrl);
 368
 369	value = readl(aio->cygaud->audio + aio->regs.bf_destch_cfg);
 370	value &= ~BIT(BF_DST_CFGX_CAP_ENA);
 371	writel(value, aio->cygaud->audio + aio->regs.bf_destch_cfg);
 372}
 373
 374static int audio_ssp_out_enable(struct cygnus_aio_port *aio)
 375{
 376	u32 value;
 377	int status = 0;
 378
 379	switch (aio->port_type) {
 380	case PORT_TDM:
 381		value = readl(aio->cygaud->audio + aio->regs.i2s_stream_cfg);
 382		value |= BIT(I2S_OUT_STREAM_ENA);
 383		writel(value, aio->cygaud->audio + aio->regs.i2s_stream_cfg);
 384
 385		writel(1, aio->cygaud->audio + aio->regs.bf_sourcech_ctrl);
 386
 387		value = readl(aio->cygaud->audio + aio->regs.i2s_cfg);
 388		value |= BIT(I2S_OUT_CFGX_CLK_ENA);
 389		value |= BIT(I2S_OUT_CFGX_DATA_ENABLE);
 390		writel(value, aio->cygaud->audio + aio->regs.i2s_cfg);
 391
 392		value = readl(aio->cygaud->audio + aio->regs.bf_sourcech_cfg);
 393		value |= BIT(BF_SRC_CFGX_SFIFO_ENA);
 394		writel(value, aio->cygaud->audio + aio->regs.bf_sourcech_cfg);
 395
 396		aio->streams_on |= PLAYBACK_STREAM_MASK;
 397		break;
 398	case PORT_SPDIF:
 399		value = readl(aio->cygaud->audio + SPDIF_FORMAT_CFG_OFFSET);
 400		value |= 0x3;
 401		writel(value, aio->cygaud->audio + SPDIF_FORMAT_CFG_OFFSET);
 402
 403		writel(1, aio->cygaud->audio + aio->regs.bf_sourcech_ctrl);
 404
 405		value = readl(aio->cygaud->audio + aio->regs.bf_sourcech_cfg);
 406		value |= BIT(BF_SRC_CFGX_SFIFO_ENA);
 407		writel(value, aio->cygaud->audio + aio->regs.bf_sourcech_cfg);
 408		break;
 409	default:
 410		dev_err(aio->cygaud->dev,
 411			"Port not supported %d\n", aio->portnum);
 412		status = -EINVAL;
 413	}
 414
 415	return status;
 416}
 417
 418static int audio_ssp_out_disable(struct cygnus_aio_port *aio)
 419{
 420	u32 value;
 421	int status = 0;
 422
 423	switch (aio->port_type) {
 424	case PORT_TDM:
 425		aio->streams_on &= ~PLAYBACK_STREAM_MASK;
 426
 427		/* If both playback and capture are off */
 428		if (!aio->streams_on) {
 429			value = readl(aio->cygaud->audio + aio->regs.i2s_cfg);
 430			value &= ~BIT(I2S_OUT_CFGX_CLK_ENA);
 431			value &= ~BIT(I2S_OUT_CFGX_DATA_ENABLE);
 432			writel(value, aio->cygaud->audio + aio->regs.i2s_cfg);
 433		}
 434
 435		/* set group_sync_dis = 1 */
 436		value = readl(aio->cygaud->audio + BF_SRC_GRP_SYNC_DIS_OFFSET);
 437		value |= BIT(aio->portnum);
 438		writel(value, aio->cygaud->audio + BF_SRC_GRP_SYNC_DIS_OFFSET);
 439
 440		writel(0, aio->cygaud->audio + aio->regs.bf_sourcech_ctrl);
 441
 442		value = readl(aio->cygaud->audio + aio->regs.bf_sourcech_cfg);
 443		value &= ~BIT(BF_SRC_CFGX_SFIFO_ENA);
 444		writel(value, aio->cygaud->audio + aio->regs.bf_sourcech_cfg);
 445
 446		/* set group_sync_dis = 0 */
 447		value = readl(aio->cygaud->audio + BF_SRC_GRP_SYNC_DIS_OFFSET);
 448		value &= ~BIT(aio->portnum);
 449		writel(value, aio->cygaud->audio + BF_SRC_GRP_SYNC_DIS_OFFSET);
 450
 451		value = readl(aio->cygaud->audio + aio->regs.i2s_stream_cfg);
 452		value &= ~BIT(I2S_OUT_STREAM_ENA);
 453		writel(value, aio->cygaud->audio + aio->regs.i2s_stream_cfg);
 454
 455		/* IOP SW INIT on OUT_I2S_x */
 456		value = readl(aio->cygaud->i2s_in + IOP_SW_INIT_LOGIC);
 457		value |= BIT(aio->portnum);
 458		writel(value, aio->cygaud->i2s_in + IOP_SW_INIT_LOGIC);
 459		value &= ~BIT(aio->portnum);
 460		writel(value, aio->cygaud->i2s_in + IOP_SW_INIT_LOGIC);
 461		break;
 462	case PORT_SPDIF:
 463		value = readl(aio->cygaud->audio + SPDIF_FORMAT_CFG_OFFSET);
 464		value &= ~0x3;
 465		writel(value, aio->cygaud->audio + SPDIF_FORMAT_CFG_OFFSET);
 466		writel(0, aio->cygaud->audio + aio->regs.bf_sourcech_ctrl);
 467
 468		value = readl(aio->cygaud->audio + aio->regs.bf_sourcech_cfg);
 469		value &= ~BIT(BF_SRC_CFGX_SFIFO_ENA);
 470		writel(value, aio->cygaud->audio + aio->regs.bf_sourcech_cfg);
 471		break;
 472	default:
 473		dev_err(aio->cygaud->dev,
 474			"Port not supported %d\n", aio->portnum);
 475		status = -EINVAL;
 476	}
 477
 478	return status;
 479}
 480
 481static int pll_configure_mclk(struct cygnus_audio *cygaud, u32 mclk,
 482	struct cygnus_aio_port *aio)
 483{
 484	int i = 0, error;
 485	bool found = false;
 486	const struct pll_macro_entry *p_entry;
 487	struct clk *ch_clk;
 488
 489	for (i = 0; i < ARRAY_SIZE(pll_predef_mclk); i++) {
 490		p_entry = &pll_predef_mclk[i];
 491		if (p_entry->mclk == mclk) {
 492			found = true;
 493			break;
 494		}
 495	}
 496	if (!found) {
 497		dev_err(cygaud->dev,
 498			"%s No valid mclk freq (%u) found!\n", __func__, mclk);
 499		return -EINVAL;
 500	}
 501
 502	ch_clk = cygaud->audio_clk[p_entry->pll_ch_num];
 503
 504	if ((aio->clk_trace.cap_en) && (!aio->clk_trace.cap_clk_en)) {
 505		error = clk_prepare_enable(ch_clk);
 506		if (error) {
 507			dev_err(cygaud->dev, "%s clk_prepare_enable failed %d\n",
 508				__func__, error);
 509			return error;
 510		}
 511		aio->clk_trace.cap_clk_en = true;
 512	}
 513
 514	if ((aio->clk_trace.play_en) && (!aio->clk_trace.play_clk_en)) {
 515		error = clk_prepare_enable(ch_clk);
 516		if (error) {
 517			dev_err(cygaud->dev, "%s clk_prepare_enable failed %d\n",
 518				__func__, error);
 519			return error;
 520		}
 521		aio->clk_trace.play_clk_en = true;
 522	}
 523
 524	error = clk_set_rate(ch_clk, mclk);
 525	if (error) {
 526		dev_err(cygaud->dev, "%s Set MCLK rate failed: %d\n",
 527			__func__, error);
 528		return error;
 529	}
 530
 531	return p_entry->pll_ch_num;
 532}
 533
 534static int cygnus_ssp_set_clocks(struct cygnus_aio_port *aio)
 535{
 536	u32 value;
 537	u32 mask = 0xf;
 538	u32 sclk;
 539	u32 mclk_rate;
 540	unsigned int bit_rate;
 541	unsigned int ratio;
 542
 543	bit_rate = aio->bit_per_frame * aio->lrclk;
 544
 545	/*
 546	 * Check if the bit clock can be generated from the given MCLK.
 547	 * MCLK must be a perfect multiple of bit clock and must be one of the
 548	 * following values... (2,4,6,8,10,12,14)
 549	 */
 550	if ((aio->mclk % bit_rate) != 0)
 551		return -EINVAL;
 552
 553	ratio = aio->mclk / bit_rate;
 554	switch (ratio) {
 555	case 2:
 556	case 4:
 557	case 6:
 558	case 8:
 559	case 10:
 560	case 12:
 561	case 14:
 562		mclk_rate = ratio / 2;
 563		break;
 564
 565	default:
 566		dev_err(aio->cygaud->dev,
 567			"Invalid combination of MCLK and BCLK\n");
 568		dev_err(aio->cygaud->dev, "lrclk = %u, bits/frame = %u, mclk = %u\n",
 569			aio->lrclk, aio->bit_per_frame, aio->mclk);
 570		return -EINVAL;
 571	}
 572
 573	/* Set sclk rate */
 574	switch (aio->port_type) {
 575	case PORT_TDM:
 576		sclk = aio->bit_per_frame;
 577		if (sclk == 512)
 578			sclk = 0;
 579
 580		/* sclks_per_1fs_div = sclk cycles/32 */
 581		sclk /= 32;
 582
 583		/* Set number of bitclks per frame */
 584		value = readl(aio->cygaud->audio + aio->regs.i2s_cfg);
 585		value &= ~(mask << I2S_OUT_CFGX_SCLKS_PER_1FS_DIV32);
 586		value |= sclk << I2S_OUT_CFGX_SCLKS_PER_1FS_DIV32;
 587		writel(value, aio->cygaud->audio + aio->regs.i2s_cfg);
 588		dev_dbg(aio->cygaud->dev,
 589			"SCLKS_PER_1FS_DIV32 = 0x%x\n", value);
 590		break;
 591	case PORT_SPDIF:
 592		break;
 593	default:
 594		dev_err(aio->cygaud->dev, "Unknown port type\n");
 595		return -EINVAL;
 596	}
 597
 598	/* Set MCLK_RATE ssp port (spdif and ssp are the same) */
 599	value = readl(aio->cygaud->audio + aio->regs.i2s_mclk_cfg);
 600	value &= ~(0xf << I2S_OUT_MCLKRATE_SHIFT);
 601	value |= (mclk_rate << I2S_OUT_MCLKRATE_SHIFT);
 602	writel(value, aio->cygaud->audio + aio->regs.i2s_mclk_cfg);
 603
 604	dev_dbg(aio->cygaud->dev, "mclk cfg reg = 0x%x\n", value);
 605	dev_dbg(aio->cygaud->dev, "bits per frame = %u, mclk = %u Hz, lrclk = %u Hz\n",
 606			aio->bit_per_frame, aio->mclk, aio->lrclk);
 607	return 0;
 608}
 609
 610static int cygnus_ssp_hw_params(struct snd_pcm_substream *substream,
 611				 struct snd_pcm_hw_params *params,
 612				 struct snd_soc_dai *dai)
 613{
 614	struct cygnus_aio_port *aio = cygnus_dai_get_portinfo(dai);
 615	int rate, bitres;
 616	u32 value;
 617	u32 mask = 0x1f;
 618	int ret = 0;
 619
 620	dev_dbg(aio->cygaud->dev, "%s port = %d\n", __func__, aio->portnum);
 621	dev_dbg(aio->cygaud->dev, "params_channels %d\n",
 622			params_channels(params));
 623	dev_dbg(aio->cygaud->dev, "rate %d\n", params_rate(params));
 624	dev_dbg(aio->cygaud->dev, "format %d\n", params_format(params));
 625
 626	rate = params_rate(params);
 627
 628	switch (aio->mode) {
 629	case CYGNUS_SSPMODE_TDM:
 630		if ((rate == 192000) && (params_channels(params) > 4)) {
 631			dev_err(aio->cygaud->dev, "Cannot run %d channels at %dHz\n",
 632				params_channels(params), rate);
 633			return -EINVAL;
 634		}
 635		break;
 636	case CYGNUS_SSPMODE_I2S:
 637		aio->bit_per_frame = 64; /* I2S must be 64 bit per frame */
 638		break;
 639	default:
 640		dev_err(aio->cygaud->dev,
 641			"%s port running in unknown mode\n", __func__);
 642		return -EINVAL;
 643	}
 644
 645	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
 646		value = readl(aio->cygaud->audio + aio->regs.bf_sourcech_cfg);
 647		value &= ~BIT(BF_SRC_CFGX_BUFFER_PAIR_ENABLE);
 648		value &= ~BIT(BF_SRC_CFGX_SAMPLE_CH_MODE);
 649		writel(value, aio->cygaud->audio + aio->regs.bf_sourcech_cfg);
 650
 651		switch (params_format(params)) {
 652		case SNDRV_PCM_FORMAT_S16_LE:
 653			bitres = 16;
 654			break;
 655
 656		case SNDRV_PCM_FORMAT_S32_LE:
 657			/* 32 bit mode is coded as 0 */
 658			bitres = 0;
 659			break;
 660
 661		default:
 662			return -EINVAL;
 663		}
 664
 665		value = readl(aio->cygaud->audio + aio->regs.bf_sourcech_cfg);
 666		value &= ~(mask << BF_SRC_CFGX_BIT_RES);
 667		value |= (bitres << BF_SRC_CFGX_BIT_RES);
 668		writel(value, aio->cygaud->audio + aio->regs.bf_sourcech_cfg);
 669
 670	} else {
 671
 672		switch (params_format(params)) {
 673		case SNDRV_PCM_FORMAT_S16_LE:
 674			value = readl(aio->cygaud->audio +
 675					aio->regs.bf_destch_cfg);
 676			value |= BIT(BF_DST_CFGX_CAP_MODE);
 677			writel(value, aio->cygaud->audio +
 678					aio->regs.bf_destch_cfg);
 679			break;
 680
 681		case SNDRV_PCM_FORMAT_S32_LE:
 682			value = readl(aio->cygaud->audio +
 683					aio->regs.bf_destch_cfg);
 684			value &= ~BIT(BF_DST_CFGX_CAP_MODE);
 685			writel(value, aio->cygaud->audio +
 686					aio->regs.bf_destch_cfg);
 687			break;
 688
 689		default:
 690			return -EINVAL;
 691		}
 692	}
 693
 694	aio->lrclk = rate;
 695
 696	if (!aio->is_slave)
 697		ret = cygnus_ssp_set_clocks(aio);
 698
 699	return ret;
 700}
 701
 702/*
 703 * This function sets the mclk frequency for pll clock
 704 */
 705static int cygnus_ssp_set_sysclk(struct snd_soc_dai *dai,
 706			int clk_id, unsigned int freq, int dir)
 707{
 708	int sel;
 709	u32 value;
 710	struct cygnus_aio_port *aio = cygnus_dai_get_portinfo(dai);
 711	struct cygnus_audio *cygaud = snd_soc_dai_get_drvdata(dai);
 712
 713	dev_dbg(aio->cygaud->dev,
 714		"%s Enter port = %d\n", __func__, aio->portnum);
 715	sel = pll_configure_mclk(cygaud, freq, aio);
 716	if (sel < 0) {
 717		dev_err(aio->cygaud->dev,
 718			"%s Setting mclk failed.\n", __func__);
 719		return -EINVAL;
 720	}
 721
 722	aio->mclk = freq;
 723
 724	dev_dbg(aio->cygaud->dev, "%s Setting MCLKSEL to %d\n", __func__, sel);
 725	value = readl(aio->cygaud->audio + aio->regs.i2s_mclk_cfg);
 726	value &= ~(0xf << I2S_OUT_PLLCLKSEL_SHIFT);
 727	value |= (sel << I2S_OUT_PLLCLKSEL_SHIFT);
 728	writel(value, aio->cygaud->audio + aio->regs.i2s_mclk_cfg);
 729
 730	return 0;
 731}
 732
 733static int cygnus_ssp_startup(struct snd_pcm_substream *substream,
 734			       struct snd_soc_dai *dai)
 735{
 736	struct cygnus_aio_port *aio = cygnus_dai_get_portinfo(dai);
 737
 738	snd_soc_dai_set_dma_data(dai, substream, aio);
 739	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
 740		aio->clk_trace.play_en = true;
 741	else
 742		aio->clk_trace.cap_en = true;
 743
 744	substream->runtime->hw.rate_min = CYGNUS_RATE_MIN;
 745	substream->runtime->hw.rate_max = CYGNUS_RATE_MAX;
 746
 747	snd_pcm_hw_constraint_list(substream->runtime, 0,
 748			SNDRV_PCM_HW_PARAM_RATE, &cygnus_rate_constraint);
 749	return 0;
 750}
 751
 752static void cygnus_ssp_shutdown(struct snd_pcm_substream *substream,
 753			       struct snd_soc_dai *dai)
 754{
 755	struct cygnus_aio_port *aio = cygnus_dai_get_portinfo(dai);
 756
 757	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
 758		aio->clk_trace.play_en = false;
 759	else
 760		aio->clk_trace.cap_en = false;
 761
 762	if (!aio->is_slave) {
 763		u32 val;
 764
 765		val = readl(aio->cygaud->audio + aio->regs.i2s_mclk_cfg);
 766		val &= CYGNUS_PLLCLKSEL_MASK;
 767		if (val >= ARRAY_SIZE(aio->cygaud->audio_clk)) {
 768			dev_err(aio->cygaud->dev, "Clk index %u is out of bounds\n",
 769				val);
 770			return;
 771		}
 772
 773		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
 774			if (aio->clk_trace.play_clk_en) {
 775				clk_disable_unprepare(aio->cygaud->
 776						audio_clk[val]);
 777				aio->clk_trace.play_clk_en = false;
 778			}
 779		} else {
 780			if (aio->clk_trace.cap_clk_en) {
 781				clk_disable_unprepare(aio->cygaud->
 782						audio_clk[val]);
 783				aio->clk_trace.cap_clk_en = false;
 784			}
 785		}
 786	}
 787}
 788
 789/*
 790 * Bit    Update  Notes
 791 * 31     Yes     TDM Mode        (1 = TDM, 0 = i2s)
 792 * 30     Yes     Slave Mode	  (1 = Slave, 0 = Master)
 793 * 29:26  No      Sclks per frame
 794 * 25:18  Yes     FS Width
 795 * 17:14  No      Valid Slots
 796 * 13     No      Bits		  (1 = 16 bits, 0 = 32 bits)
 797 * 12:08  No     Bits per samp
 798 * 07     Yes     Justifcation    (1 = LSB, 0 = MSB)
 799 * 06     Yes     Alignment       (1 = Delay 1 clk, 0 = no delay
 800 * 05     Yes     SCLK polarity   (1 = Rising, 0 = Falling)
 801 * 04     Yes     LRCLK Polarity  (1 = High for left, 0 = Low for left)
 802 * 03:02  Yes     Reserved - write as zero
 803 * 01     No      Data Enable
 804 * 00     No      CLK Enable
 805 */
 806#define I2S_OUT_CFG_REG_UPDATE_MASK   0x3C03FF03
 807
 808/* Input cfg is same as output, but the FS width is not a valid field */
 809#define I2S_IN_CFG_REG_UPDATE_MASK  (I2S_OUT_CFG_REG_UPDATE_MASK | 0x03FC0000)
 810
 811int cygnus_ssp_set_custom_fsync_width(struct snd_soc_dai *cpu_dai, int len)
 812{
 813	struct cygnus_aio_port *aio = cygnus_dai_get_portinfo(cpu_dai);
 814
 815	if ((len > 0) && (len < 256)) {
 816		aio->fsync_width = len;
 817		return 0;
 818	} else {
 819		return -EINVAL;
 820	}
 821}
 822EXPORT_SYMBOL_GPL(cygnus_ssp_set_custom_fsync_width);
 823
 824static int cygnus_ssp_set_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
 825{
 826	struct cygnus_aio_port *aio = cygnus_dai_get_portinfo(cpu_dai);
 827	u32 ssp_curcfg;
 828	u32 ssp_newcfg;
 829	u32 ssp_outcfg;
 830	u32 ssp_incfg;
 831	u32 val;
 832	u32 mask;
 833
 834	dev_dbg(aio->cygaud->dev, "%s Enter  fmt: %x\n", __func__, fmt);
 835
 836	if (aio->port_type == PORT_SPDIF)
 837		return -EINVAL;
 838
 839	ssp_newcfg = 0;
 840
 841	switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
 842	case SND_SOC_DAIFMT_BC_FC:
 843		ssp_newcfg |= BIT(I2S_OUT_CFGX_SLAVE_MODE);
 844		aio->is_slave = 1;
 845		break;
 846	case SND_SOC_DAIFMT_BP_FP:
 847		ssp_newcfg &= ~BIT(I2S_OUT_CFGX_SLAVE_MODE);
 848		aio->is_slave = 0;
 849		break;
 850	default:
 851		return -EINVAL;
 852	}
 853
 854	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
 855	case SND_SOC_DAIFMT_I2S:
 856		ssp_newcfg |= BIT(I2S_OUT_CFGX_DATA_ALIGNMENT);
 857		ssp_newcfg |= BIT(I2S_OUT_CFGX_FSYNC_WIDTH);
 858		aio->mode = CYGNUS_SSPMODE_I2S;
 859		break;
 860
 861	case SND_SOC_DAIFMT_DSP_A:
 862	case SND_SOC_DAIFMT_DSP_B:
 863		ssp_newcfg |= BIT(I2S_OUT_CFGX_TDM_MODE);
 864
 865		/* DSP_A = data after FS, DSP_B = data during FS */
 866		if ((fmt & SND_SOC_DAIFMT_FORMAT_MASK) == SND_SOC_DAIFMT_DSP_A)
 867			ssp_newcfg |= BIT(I2S_OUT_CFGX_DATA_ALIGNMENT);
 868
 869		if ((aio->fsync_width > 0) && (aio->fsync_width < 256))
 870			ssp_newcfg |=
 871				(aio->fsync_width << I2S_OUT_CFGX_FSYNC_WIDTH);
 872		else
 873			ssp_newcfg |= BIT(I2S_OUT_CFGX_FSYNC_WIDTH);
 874
 875		aio->mode = CYGNUS_SSPMODE_TDM;
 876		break;
 877
 878	default:
 879		return -EINVAL;
 880	}
 881
 882	/*
 883	 * SSP out cfg.
 884	 * Retain bits we do not want to update, then OR in new bits
 885	 */
 886	ssp_curcfg = readl(aio->cygaud->audio + aio->regs.i2s_cfg);
 887	ssp_outcfg = (ssp_curcfg & I2S_OUT_CFG_REG_UPDATE_MASK) | ssp_newcfg;
 888	writel(ssp_outcfg, aio->cygaud->audio + aio->regs.i2s_cfg);
 889
 890	/*
 891	 * SSP in cfg.
 892	 * Retain bits we do not want to update, then OR in new bits
 893	 */
 894	ssp_curcfg = readl(aio->cygaud->i2s_in + aio->regs.i2s_cap_cfg);
 895	ssp_incfg = (ssp_curcfg & I2S_IN_CFG_REG_UPDATE_MASK) | ssp_newcfg;
 896	writel(ssp_incfg, aio->cygaud->i2s_in + aio->regs.i2s_cap_cfg);
 897
 898	val = readl(aio->cygaud->audio + AUD_MISC_SEROUT_OE_REG_BASE);
 899
 900	/*
 901	 * Configure the word clk and bit clk as output or tristate
 902	 * Each port has 4 bits for controlling its pins.
 903	 * Shift the mask based upon port number.
 904	 */
 905	mask = BIT(AUD_MISC_SEROUT_LRCK_OE)
 906			| BIT(AUD_MISC_SEROUT_SCLK_OE)
 907			| BIT(AUD_MISC_SEROUT_MCLK_OE);
 908	mask = mask << (aio->portnum * 4);
 909	if (aio->is_slave)
 910		/* Set bit for tri-state */
 911		val |= mask;
 912	else
 913		/* Clear bit for drive */
 914		val &= ~mask;
 915
 916	dev_dbg(aio->cygaud->dev, "%s  Set OE bits 0x%x\n", __func__, val);
 917	writel(val, aio->cygaud->audio + AUD_MISC_SEROUT_OE_REG_BASE);
 918
 919	return 0;
 920}
 921
 922static int cygnus_ssp_trigger(struct snd_pcm_substream *substream, int cmd,
 923			       struct snd_soc_dai *dai)
 924{
 925	struct cygnus_aio_port *aio = cygnus_dai_get_portinfo(dai);
 926	struct cygnus_audio *cygaud = snd_soc_dai_get_drvdata(dai);
 927
 928	dev_dbg(aio->cygaud->dev,
 929		"%s cmd %d at port = %d\n", __func__, cmd, aio->portnum);
 930
 931	switch (cmd) {
 932	case SNDRV_PCM_TRIGGER_START:
 933	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
 934	case SNDRV_PCM_TRIGGER_RESUME:
 935		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
 936			audio_ssp_out_enable(aio);
 937		else
 938			audio_ssp_in_enable(aio);
 939		cygaud->active_ports++;
 940
 941		break;
 942
 943	case SNDRV_PCM_TRIGGER_STOP:
 944	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
 945	case SNDRV_PCM_TRIGGER_SUSPEND:
 946		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
 947			audio_ssp_out_disable(aio);
 948		else
 949			audio_ssp_in_disable(aio);
 950		cygaud->active_ports--;
 951		break;
 952
 953	default:
 954		return -EINVAL;
 955	}
 956
 957	return 0;
 958}
 959
 960static int cygnus_set_dai_tdm_slot(struct snd_soc_dai *cpu_dai,
 961	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
 962{
 963	struct cygnus_aio_port *aio = cygnus_dai_get_portinfo(cpu_dai);
 964	u32 value;
 965	int bits_per_slot = 0;     /* default to 32-bits per slot */
 966	int frame_bits;
 967	unsigned int active_slots;
 968	bool found = false;
 969	int i;
 970
 971	if (tx_mask != rx_mask) {
 972		dev_err(aio->cygaud->dev,
 973			"%s tx_mask must equal rx_mask\n", __func__);
 974		return -EINVAL;
 975	}
 976
 977	active_slots = hweight32(tx_mask);
 978
 979	if (active_slots > 16)
 980		return -EINVAL;
 981
 982	/* Slot value must be even */
 983	if (active_slots % 2)
 984		return -EINVAL;
 985
 986	/* We encode 16 slots as 0 in the reg */
 987	if (active_slots == 16)
 988		active_slots = 0;
 989
 990	/* Slot Width is either 16 or 32 */
 991	switch (slot_width) {
 992	case 16:
 993		bits_per_slot = 1;
 994		break;
 995	case 32:
 996		bits_per_slot = 0;
 997		break;
 998	default:
 999		bits_per_slot = 0;
1000		dev_warn(aio->cygaud->dev,
1001			"%s Defaulting Slot Width to 32\n", __func__);
1002	}
1003
1004	frame_bits = slots * slot_width;
1005
1006	for (i = 0; i < ARRAY_SIZE(ssp_valid_tdm_framesize); i++) {
1007		if (ssp_valid_tdm_framesize[i] == frame_bits) {
1008			found = true;
1009			break;
1010		}
1011	}
1012
1013	if (!found) {
1014		dev_err(aio->cygaud->dev,
1015			"%s In TDM mode, frame bits INVALID (%d)\n",
1016			__func__, frame_bits);
1017		return -EINVAL;
1018	}
1019
1020	aio->bit_per_frame = frame_bits;
1021
1022	dev_dbg(aio->cygaud->dev, "%s active_slots %u, bits per frame %d\n",
1023			__func__, active_slots, frame_bits);
1024
1025	/* Set capture side of ssp port */
1026	value = readl(aio->cygaud->i2s_in + aio->regs.i2s_cap_cfg);
1027	value &= ~(0xf << I2S_OUT_CFGX_VALID_SLOT);
1028	value |= (active_slots << I2S_OUT_CFGX_VALID_SLOT);
1029	value &= ~BIT(I2S_OUT_CFGX_BITS_PER_SLOT);
1030	value |= (bits_per_slot << I2S_OUT_CFGX_BITS_PER_SLOT);
1031	writel(value, aio->cygaud->i2s_in + aio->regs.i2s_cap_cfg);
1032
1033	/* Set playback side of ssp port */
1034	value = readl(aio->cygaud->audio + aio->regs.i2s_cfg);
1035	value &= ~(0xf << I2S_OUT_CFGX_VALID_SLOT);
1036	value |= (active_slots << I2S_OUT_CFGX_VALID_SLOT);
1037	value &= ~BIT(I2S_OUT_CFGX_BITS_PER_SLOT);
1038	value |= (bits_per_slot << I2S_OUT_CFGX_BITS_PER_SLOT);
1039	writel(value, aio->cygaud->audio + aio->regs.i2s_cfg);
1040
1041	return 0;
1042}
1043
1044#ifdef CONFIG_PM_SLEEP
1045static int __cygnus_ssp_suspend(struct snd_soc_dai *cpu_dai)
1046{
1047	struct cygnus_aio_port *aio = cygnus_dai_get_portinfo(cpu_dai);
1048
1049	if (!snd_soc_dai_active(cpu_dai))
1050		return 0;
1051
1052	if (!aio->is_slave) {
1053		u32 val;
1054
1055		val = readl(aio->cygaud->audio + aio->regs.i2s_mclk_cfg);
1056		val &= CYGNUS_PLLCLKSEL_MASK;
1057		if (val >= ARRAY_SIZE(aio->cygaud->audio_clk)) {
1058			dev_err(aio->cygaud->dev, "Clk index %u is out of bounds\n",
1059				val);
1060			return -EINVAL;
1061		}
1062
1063		if (aio->clk_trace.cap_clk_en)
1064			clk_disable_unprepare(aio->cygaud->audio_clk[val]);
1065		if (aio->clk_trace.play_clk_en)
1066			clk_disable_unprepare(aio->cygaud->audio_clk[val]);
1067
1068		aio->pll_clk_num = val;
1069	}
1070
1071	return 0;
1072}
1073
1074static int cygnus_ssp_suspend(struct snd_soc_component *component)
1075{
1076	struct snd_soc_dai *dai;
1077	int ret = 0;
1078
1079	for_each_component_dais(component, dai)
1080		ret |= __cygnus_ssp_suspend(dai);
1081
1082	return ret;
1083}
1084
1085static int __cygnus_ssp_resume(struct snd_soc_dai *cpu_dai)
1086{
1087	struct cygnus_aio_port *aio = cygnus_dai_get_portinfo(cpu_dai);
1088	int error;
1089
1090	if (!snd_soc_dai_active(cpu_dai))
1091		return 0;
1092
1093	if (!aio->is_slave) {
1094		if (aio->clk_trace.cap_clk_en) {
1095			error = clk_prepare_enable(aio->cygaud->
1096					audio_clk[aio->pll_clk_num]);
1097			if (error) {
1098				dev_err(aio->cygaud->dev, "%s clk_prepare_enable failed\n",
1099					__func__);
1100				return -EINVAL;
1101			}
1102		}
1103		if (aio->clk_trace.play_clk_en) {
1104			error = clk_prepare_enable(aio->cygaud->
1105					audio_clk[aio->pll_clk_num]);
1106			if (error) {
1107				if (aio->clk_trace.cap_clk_en)
1108					clk_disable_unprepare(aio->cygaud->
1109						audio_clk[aio->pll_clk_num]);
1110				dev_err(aio->cygaud->dev, "%s clk_prepare_enable failed\n",
1111					__func__);
1112				return -EINVAL;
1113			}
1114		}
1115	}
1116
1117	return 0;
1118}
1119
1120static int cygnus_ssp_resume(struct snd_soc_component *component)
1121{
1122	struct snd_soc_dai *dai;
1123	int ret = 0;
1124
1125	for_each_component_dais(component, dai)
1126		ret |= __cygnus_ssp_resume(dai);
1127
1128	return ret;
1129}
1130
1131#else
1132#define cygnus_ssp_suspend NULL
1133#define cygnus_ssp_resume  NULL
1134#endif
1135
1136static const struct snd_soc_dai_ops cygnus_ssp_dai_ops = {
1137	.startup	= cygnus_ssp_startup,
1138	.shutdown	= cygnus_ssp_shutdown,
1139	.trigger	= cygnus_ssp_trigger,
1140	.hw_params	= cygnus_ssp_hw_params,
1141	.set_fmt	= cygnus_ssp_set_fmt,
1142	.set_sysclk	= cygnus_ssp_set_sysclk,
1143	.set_tdm_slot	= cygnus_set_dai_tdm_slot,
1144};
1145
1146static const struct snd_soc_dai_ops cygnus_spdif_dai_ops = {
1147	.startup	= cygnus_ssp_startup,
1148	.shutdown	= cygnus_ssp_shutdown,
1149	.trigger	= cygnus_ssp_trigger,
1150	.hw_params	= cygnus_ssp_hw_params,
1151	.set_sysclk	= cygnus_ssp_set_sysclk,
1152};
1153
1154#define INIT_CPU_DAI(num) { \
1155	.name = "cygnus-ssp" #num, \
1156	.playback = { \
1157		.channels_min = 2, \
1158		.channels_max = 16, \
1159		.rates = SNDRV_PCM_RATE_KNOT, \
1160		.formats = SNDRV_PCM_FMTBIT_S16_LE | \
1161				SNDRV_PCM_FMTBIT_S32_LE, \
1162	}, \
1163	.capture = { \
1164		.channels_min = 2, \
1165		.channels_max = 16, \
1166		.rates = SNDRV_PCM_RATE_KNOT, \
1167		.formats =  SNDRV_PCM_FMTBIT_S16_LE | \
1168				SNDRV_PCM_FMTBIT_S32_LE, \
1169	}, \
1170	.ops = &cygnus_ssp_dai_ops, \
 
 
1171}
1172
1173static const struct snd_soc_dai_driver cygnus_ssp_dai_info[] = {
1174	INIT_CPU_DAI(0),
1175	INIT_CPU_DAI(1),
1176	INIT_CPU_DAI(2),
1177};
1178
1179static const struct snd_soc_dai_driver cygnus_spdif_dai_info = {
1180	.name = "cygnus-spdif",
1181	.playback = {
1182		.channels_min = 2,
1183		.channels_max = 2,
1184		.rates = SNDRV_PCM_RATE_KNOT,
1185		.formats = SNDRV_PCM_FMTBIT_S16_LE |
1186			SNDRV_PCM_FMTBIT_S32_LE,
1187	},
1188	.ops = &cygnus_spdif_dai_ops,
 
 
1189};
1190
1191static struct snd_soc_dai_driver cygnus_ssp_dai[CYGNUS_MAX_PORTS];
1192
1193static const struct snd_soc_component_driver cygnus_ssp_component = {
1194	.name			= "cygnus-audio",
1195	.suspend		= cygnus_ssp_suspend,
1196	.resume			= cygnus_ssp_resume,
1197	.legacy_dai_naming	= 1,
1198};
1199
1200/*
1201 * Return < 0 if error
1202 * Return 0 if disabled
1203 * Return 1 if enabled and node is parsed successfully
1204 */
1205static int parse_ssp_child_node(struct platform_device *pdev,
1206				struct device_node *dn,
1207				struct cygnus_audio *cygaud,
1208				struct snd_soc_dai_driver *p_dai)
1209{
1210	struct cygnus_aio_port *aio;
1211	struct cygnus_ssp_regs ssp_regs[3];
1212	u32 rawval;
1213	int portnum = -1;
1214	enum cygnus_audio_port_type port_type;
1215
1216	if (of_property_read_u32(dn, "reg", &rawval)) {
1217		dev_err(&pdev->dev, "Missing reg property\n");
1218		return -EINVAL;
1219	}
1220
1221	portnum = rawval;
1222	switch (rawval) {
1223	case 0:
1224		ssp_regs[0] = INIT_SSP_REGS(0);
1225		port_type = PORT_TDM;
1226		break;
1227	case 1:
1228		ssp_regs[1] = INIT_SSP_REGS(1);
1229		port_type = PORT_TDM;
1230		break;
1231	case 2:
1232		ssp_regs[2] = INIT_SSP_REGS(2);
1233		port_type = PORT_TDM;
1234		break;
1235	case 3:
1236		port_type = PORT_SPDIF;
1237		break;
1238	default:
1239		dev_err(&pdev->dev, "Bad value for reg %u\n", rawval);
1240		return -EINVAL;
1241	}
1242
1243	aio = &cygaud->portinfo[portnum];
1244	aio->cygaud = cygaud;
1245	aio->portnum = portnum;
1246	aio->port_type = port_type;
1247	aio->fsync_width = -1;
1248
1249	switch (port_type) {
1250	case PORT_TDM:
1251		aio->regs = ssp_regs[portnum];
1252		*p_dai = cygnus_ssp_dai_info[portnum];
1253		aio->mode = CYGNUS_SSPMODE_UNKNOWN;
1254		break;
1255
1256	case PORT_SPDIF:
1257		aio->regs.bf_sourcech_cfg = BF_SRC_CFG3_OFFSET;
1258		aio->regs.bf_sourcech_ctrl = BF_SRC_CTRL3_OFFSET;
1259		aio->regs.i2s_mclk_cfg = SPDIF_MCLK_CFG_OFFSET;
1260		aio->regs.i2s_stream_cfg = SPDIF_STREAM_CFG_OFFSET;
1261		*p_dai = cygnus_spdif_dai_info;
1262
1263		/* For the purposes of this code SPDIF can be I2S mode */
1264		aio->mode = CYGNUS_SSPMODE_I2S;
1265		break;
1266	default:
1267		dev_err(&pdev->dev, "Bad value for port_type %d\n", port_type);
1268		return -EINVAL;
1269	}
1270
1271	dev_dbg(&pdev->dev, "%s portnum = %d\n", __func__, aio->portnum);
1272	aio->streams_on = 0;
1273	aio->cygaud->dev = &pdev->dev;
1274	aio->clk_trace.play_en = false;
1275	aio->clk_trace.cap_en = false;
1276
1277	audio_ssp_init_portregs(aio);
1278	return 0;
1279}
1280
1281static int audio_clk_init(struct platform_device *pdev,
1282						struct cygnus_audio *cygaud)
1283{
1284	int i;
1285	char clk_name[PROP_LEN_MAX];
1286
1287	for (i = 0; i < ARRAY_SIZE(cygaud->audio_clk); i++) {
1288		snprintf(clk_name, PROP_LEN_MAX, "ch%d_audio", i);
1289
1290		cygaud->audio_clk[i] = devm_clk_get(&pdev->dev, clk_name);
1291		if (IS_ERR(cygaud->audio_clk[i]))
1292			return PTR_ERR(cygaud->audio_clk[i]);
1293	}
1294
1295	return 0;
1296}
1297
1298static int cygnus_ssp_probe(struct platform_device *pdev)
1299{
1300	struct device *dev = &pdev->dev;
1301	struct device_node *child_node;
 
1302	struct cygnus_audio *cygaud;
1303	int err;
1304	int node_count;
1305	int active_port_count;
1306
1307	cygaud = devm_kzalloc(dev, sizeof(struct cygnus_audio), GFP_KERNEL);
1308	if (!cygaud)
1309		return -ENOMEM;
1310
1311	dev_set_drvdata(dev, cygaud);
1312
1313	cygaud->audio = devm_platform_ioremap_resource_byname(pdev, "aud");
 
1314	if (IS_ERR(cygaud->audio))
1315		return PTR_ERR(cygaud->audio);
1316
1317	cygaud->i2s_in = devm_platform_ioremap_resource_byname(pdev, "i2s_in");
 
1318	if (IS_ERR(cygaud->i2s_in))
1319		return PTR_ERR(cygaud->i2s_in);
1320
1321	/* Tri-state all controlable pins until we know that we need them */
1322	writel(CYGNUS_SSP_TRISTATE_MASK,
1323			cygaud->audio + AUD_MISC_SEROUT_OE_REG_BASE);
1324
1325	node_count = of_get_child_count(pdev->dev.of_node);
1326	if ((node_count < 1) || (node_count > CYGNUS_MAX_PORTS)) {
1327		dev_err(dev, "child nodes is %d.  Must be between 1 and %d\n",
1328			node_count, CYGNUS_MAX_PORTS);
1329		return -EINVAL;
1330	}
1331
1332	active_port_count = 0;
1333
1334	for_each_available_child_of_node(pdev->dev.of_node, child_node) {
1335		err = parse_ssp_child_node(pdev, child_node, cygaud,
1336					&cygnus_ssp_dai[active_port_count]);
1337
1338		/* negative is err, 0 is active and good, 1 is disabled */
1339		if (err < 0) {
1340			of_node_put(child_node);
1341			return err;
1342		}
1343		else if (!err) {
1344			dev_dbg(dev, "Activating DAI: %s\n",
1345				cygnus_ssp_dai[active_port_count].name);
1346			active_port_count++;
1347		}
1348	}
1349
1350	cygaud->dev = dev;
1351	cygaud->active_ports = 0;
1352
1353	dev_dbg(dev, "Registering %d DAIs\n", active_port_count);
1354	err = devm_snd_soc_register_component(dev, &cygnus_ssp_component,
1355				cygnus_ssp_dai, active_port_count);
1356	if (err) {
1357		dev_err(dev, "snd_soc_register_dai failed\n");
1358		return err;
1359	}
1360
1361	cygaud->irq_num = platform_get_irq(pdev, 0);
1362	if (cygaud->irq_num <= 0)
1363		return cygaud->irq_num;
 
 
 
1364
1365	err = audio_clk_init(pdev, cygaud);
1366	if (err) {
1367		dev_err(dev, "audio clock initialization failed\n");
1368		return err;
1369	}
1370
1371	err = cygnus_soc_platform_register(dev, cygaud);
1372	if (err) {
1373		dev_err(dev, "platform reg error %d\n", err);
1374		return err;
1375	}
1376
1377	return 0;
 
 
 
 
1378}
1379
1380static void cygnus_ssp_remove(struct platform_device *pdev)
1381{
1382	cygnus_soc_platform_unregister(&pdev->dev);
 
 
 
1383}
1384
1385static const struct of_device_id cygnus_ssp_of_match[] = {
1386	{ .compatible = "brcm,cygnus-audio" },
1387	{},
1388};
1389MODULE_DEVICE_TABLE(of, cygnus_ssp_of_match);
1390
1391static struct platform_driver cygnus_ssp_driver = {
1392	.probe		= cygnus_ssp_probe,
1393	.remove		= cygnus_ssp_remove,
1394	.driver		= {
1395		.name	= "cygnus-ssp",
1396		.of_match_table = cygnus_ssp_of_match,
1397	},
1398};
1399
1400module_platform_driver(cygnus_ssp_driver);
1401
1402MODULE_ALIAS("platform:cygnus-ssp");
1403MODULE_LICENSE("GPL v2");
1404MODULE_AUTHOR("Broadcom");
1405MODULE_DESCRIPTION("Cygnus ASoC SSP Interface");
v4.17
   1/*
   2 * Copyright (C) 2014-2015 Broadcom Corporation
   3 *
   4 * This program is free software; you can redistribute it and/or
   5 * modify it under the terms of the GNU General Public License as
   6 * published by the Free Software Foundation version 2.
   7 *
   8 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
   9 * kind, whether express or implied; without even the implied warranty
  10 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11 * GNU General Public License for more details.
  12 */
  13#include <linux/clk.h>
  14#include <linux/delay.h>
  15#include <linux/init.h>
  16#include <linux/io.h>
  17#include <linux/module.h>
  18#include <linux/of_device.h>
  19#include <linux/slab.h>
  20#include <sound/core.h>
  21#include <sound/pcm.h>
  22#include <sound/pcm_params.h>
  23#include <sound/soc.h>
  24#include <sound/soc-dai.h>
  25
  26#include "cygnus-ssp.h"
  27
  28#define DEFAULT_VCO    1354750204
  29
  30#define CAPTURE_FCI_ID_BASE 0x180
  31#define CYGNUS_SSP_TRISTATE_MASK 0x001fff
  32#define CYGNUS_PLLCLKSEL_MASK 0xf
  33
  34/* Used with stream_on field to indicate which streams are active */
  35#define  PLAYBACK_STREAM_MASK   BIT(0)
  36#define  CAPTURE_STREAM_MASK    BIT(1)
  37
  38#define I2S_STREAM_CFG_MASK      0xff003ff
  39#define I2S_CAP_STREAM_CFG_MASK  0xf0
  40#define SPDIF_STREAM_CFG_MASK    0x3ff
  41#define CH_GRP_STEREO            0x1
  42
  43/* Begin register offset defines */
  44#define AUD_MISC_SEROUT_OE_REG_BASE  0x01c
  45#define AUD_MISC_SEROUT_SPDIF_OE  12
  46#define AUD_MISC_SEROUT_MCLK_OE   3
  47#define AUD_MISC_SEROUT_LRCK_OE   2
  48#define AUD_MISC_SEROUT_SCLK_OE   1
  49#define AUD_MISC_SEROUT_SDAT_OE   0
  50
  51/* AUD_FMM_BF_CTRL_xxx regs */
  52#define BF_DST_CFG0_OFFSET  0x100
  53#define BF_DST_CFG1_OFFSET  0x104
  54#define BF_DST_CFG2_OFFSET  0x108
  55
  56#define BF_DST_CTRL0_OFFSET 0x130
  57#define BF_DST_CTRL1_OFFSET 0x134
  58#define BF_DST_CTRL2_OFFSET 0x138
  59
  60#define BF_SRC_CFG0_OFFSET  0x148
  61#define BF_SRC_CFG1_OFFSET  0x14c
  62#define BF_SRC_CFG2_OFFSET  0x150
  63#define BF_SRC_CFG3_OFFSET  0x154
  64
  65#define BF_SRC_CTRL0_OFFSET 0x1c0
  66#define BF_SRC_CTRL1_OFFSET 0x1c4
  67#define BF_SRC_CTRL2_OFFSET 0x1c8
  68#define BF_SRC_CTRL3_OFFSET 0x1cc
  69
  70#define BF_SRC_GRP0_OFFSET  0x1fc
  71#define BF_SRC_GRP1_OFFSET  0x200
  72#define BF_SRC_GRP2_OFFSET  0x204
  73#define BF_SRC_GRP3_OFFSET  0x208
  74
  75#define BF_SRC_GRP_EN_OFFSET        0x320
  76#define BF_SRC_GRP_FLOWON_OFFSET    0x324
  77#define BF_SRC_GRP_SYNC_DIS_OFFSET  0x328
  78
  79/* AUD_FMM_IOP_OUT_I2S_xxx regs */
  80#define OUT_I2S_0_STREAM_CFG_OFFSET 0xa00
  81#define OUT_I2S_0_CFG_OFFSET        0xa04
  82#define OUT_I2S_0_MCLK_CFG_OFFSET   0xa0c
  83
  84#define OUT_I2S_1_STREAM_CFG_OFFSET 0xa40
  85#define OUT_I2S_1_CFG_OFFSET        0xa44
  86#define OUT_I2S_1_MCLK_CFG_OFFSET   0xa4c
  87
  88#define OUT_I2S_2_STREAM_CFG_OFFSET 0xa80
  89#define OUT_I2S_2_CFG_OFFSET        0xa84
  90#define OUT_I2S_2_MCLK_CFG_OFFSET   0xa8c
  91
  92/* AUD_FMM_IOP_OUT_SPDIF_xxx regs */
  93#define SPDIF_STREAM_CFG_OFFSET  0xac0
  94#define SPDIF_CTRL_OFFSET        0xac4
  95#define SPDIF_FORMAT_CFG_OFFSET  0xad8
  96#define SPDIF_MCLK_CFG_OFFSET    0xadc
  97
  98/* AUD_FMM_IOP_PLL_0_xxx regs */
  99#define IOP_PLL_0_MACRO_OFFSET    0xb00
 100#define IOP_PLL_0_MDIV_Ch0_OFFSET 0xb14
 101#define IOP_PLL_0_MDIV_Ch1_OFFSET 0xb18
 102#define IOP_PLL_0_MDIV_Ch2_OFFSET 0xb1c
 103
 104#define IOP_PLL_0_ACTIVE_MDIV_Ch0_OFFSET 0xb30
 105#define IOP_PLL_0_ACTIVE_MDIV_Ch1_OFFSET 0xb34
 106#define IOP_PLL_0_ACTIVE_MDIV_Ch2_OFFSET 0xb38
 107
 108/* AUD_FMM_IOP_xxx regs */
 109#define IOP_PLL_0_CONTROL_OFFSET     0xb04
 110#define IOP_PLL_0_USER_NDIV_OFFSET   0xb08
 111#define IOP_PLL_0_ACTIVE_NDIV_OFFSET 0xb20
 112#define IOP_PLL_0_RESET_OFFSET       0xb5c
 113
 114/* AUD_FMM_IOP_IN_I2S_xxx regs */
 115#define IN_I2S_0_STREAM_CFG_OFFSET 0x00
 116#define IN_I2S_0_CFG_OFFSET        0x04
 117#define IN_I2S_1_STREAM_CFG_OFFSET 0x40
 118#define IN_I2S_1_CFG_OFFSET        0x44
 119#define IN_I2S_2_STREAM_CFG_OFFSET 0x80
 120#define IN_I2S_2_CFG_OFFSET        0x84
 121
 122/* AUD_FMM_IOP_MISC_xxx regs */
 123#define IOP_SW_INIT_LOGIC          0x1c0
 124
 125/* End register offset defines */
 126
 127
 128/* AUD_FMM_IOP_OUT_I2S_x_MCLK_CFG_0_REG */
 129#define I2S_OUT_MCLKRATE_SHIFT 16
 130
 131/* AUD_FMM_IOP_OUT_I2S_x_MCLK_CFG_REG */
 132#define I2S_OUT_PLLCLKSEL_SHIFT  0
 133
 134/* AUD_FMM_IOP_OUT_I2S_x_STREAM_CFG */
 135#define I2S_OUT_STREAM_ENA  31
 136#define I2S_OUT_STREAM_CFG_GROUP_ID  20
 137#define I2S_OUT_STREAM_CFG_CHANNEL_GROUPING  24
 138
 139/* AUD_FMM_IOP_IN_I2S_x_CAP */
 140#define I2S_IN_STREAM_CFG_CAP_ENA   31
 141#define I2S_IN_STREAM_CFG_0_GROUP_ID 4
 142
 143/* AUD_FMM_IOP_OUT_I2S_x_I2S_CFG_REG */
 144#define I2S_OUT_CFGX_CLK_ENA         0
 145#define I2S_OUT_CFGX_DATA_ENABLE     1
 146#define I2S_OUT_CFGX_DATA_ALIGNMENT  6
 147#define I2S_OUT_CFGX_BITS_PER_SLOT  13
 148#define I2S_OUT_CFGX_VALID_SLOT     14
 149#define I2S_OUT_CFGX_FSYNC_WIDTH    18
 150#define I2S_OUT_CFGX_SCLKS_PER_1FS_DIV32 26
 151#define I2S_OUT_CFGX_SLAVE_MODE     30
 152#define I2S_OUT_CFGX_TDM_MODE       31
 153
 154/* AUD_FMM_BF_CTRL_SOURCECH_CFGx_REG */
 155#define BF_SRC_CFGX_SFIFO_ENA              0
 156#define BF_SRC_CFGX_BUFFER_PAIR_ENABLE     1
 157#define BF_SRC_CFGX_SAMPLE_CH_MODE         2
 158#define BF_SRC_CFGX_SFIFO_SZ_DOUBLE        5
 159#define BF_SRC_CFGX_NOT_PAUSE_WHEN_EMPTY  10
 160#define BF_SRC_CFGX_BIT_RES               20
 161#define BF_SRC_CFGX_PROCESS_SEQ_ID_VALID  31
 162
 163/* AUD_FMM_BF_CTRL_DESTCH_CFGx_REG */
 164#define BF_DST_CFGX_CAP_ENA              0
 165#define BF_DST_CFGX_BUFFER_PAIR_ENABLE   1
 166#define BF_DST_CFGX_DFIFO_SZ_DOUBLE      2
 167#define BF_DST_CFGX_NOT_PAUSE_WHEN_FULL 11
 168#define BF_DST_CFGX_FCI_ID              12
 169#define BF_DST_CFGX_CAP_MODE            24
 170#define BF_DST_CFGX_PROC_SEQ_ID_VALID   31
 171
 172/* AUD_FMM_IOP_OUT_SPDIF_xxx */
 173#define SPDIF_0_OUT_DITHER_ENA     3
 174#define SPDIF_0_OUT_STREAM_ENA    31
 175
 176/* AUD_FMM_IOP_PLL_0_USER */
 177#define IOP_PLL_0_USER_NDIV_FRAC   10
 178
 179/* AUD_FMM_IOP_PLL_0_ACTIVE */
 180#define IOP_PLL_0_ACTIVE_NDIV_FRAC 10
 181
 182
 183#define INIT_SSP_REGS(num) (struct cygnus_ssp_regs){ \
 184		.i2s_stream_cfg = OUT_I2S_ ##num## _STREAM_CFG_OFFSET, \
 185		.i2s_cap_stream_cfg = IN_I2S_ ##num## _STREAM_CFG_OFFSET, \
 186		.i2s_cfg = OUT_I2S_ ##num## _CFG_OFFSET, \
 187		.i2s_cap_cfg = IN_I2S_ ##num## _CFG_OFFSET, \
 188		.i2s_mclk_cfg = OUT_I2S_ ##num## _MCLK_CFG_OFFSET, \
 189		.bf_destch_ctrl = BF_DST_CTRL ##num## _OFFSET, \
 190		.bf_destch_cfg = BF_DST_CFG ##num## _OFFSET, \
 191		.bf_sourcech_ctrl = BF_SRC_CTRL ##num## _OFFSET, \
 192		.bf_sourcech_cfg = BF_SRC_CFG ##num## _OFFSET, \
 193		.bf_sourcech_grp = BF_SRC_GRP ##num## _OFFSET \
 194}
 195
 196struct pll_macro_entry {
 197	u32 mclk;
 198	u32 pll_ch_num;
 199};
 200
 201/*
 202 * PLL has 3 output channels (1x, 2x, and 4x). Below are
 203 * the common MCLK frequencies used by audio driver
 204 */
 205static const struct pll_macro_entry pll_predef_mclk[] = {
 206	{ 4096000, 0},
 207	{ 8192000, 1},
 208	{16384000, 2},
 209
 210	{ 5644800, 0},
 211	{11289600, 1},
 212	{22579200, 2},
 213
 214	{ 6144000, 0},
 215	{12288000, 1},
 216	{24576000, 2},
 217
 218	{12288000, 0},
 219	{24576000, 1},
 220	{49152000, 2},
 221
 222	{22579200, 0},
 223	{45158400, 1},
 224	{90316800, 2},
 225
 226	{24576000, 0},
 227	{49152000, 1},
 228	{98304000, 2},
 229};
 230
 231#define CYGNUS_RATE_MIN     8000
 232#define CYGNUS_RATE_MAX   384000
 233
 234/* List of valid frame sizes for tdm mode */
 235static const int ssp_valid_tdm_framesize[] = {32, 64, 128, 256, 512};
 236
 237static const unsigned int cygnus_rates[] = {
 238	 8000, 11025,  16000,  22050,  32000,  44100, 48000,
 239	88200, 96000, 176400, 192000, 352800, 384000
 240};
 241
 242static const struct snd_pcm_hw_constraint_list cygnus_rate_constraint = {
 243	.count = ARRAY_SIZE(cygnus_rates),
 244	.list = cygnus_rates,
 245};
 246
 247static struct cygnus_aio_port *cygnus_dai_get_portinfo(struct snd_soc_dai *dai)
 248{
 249	struct cygnus_audio *cygaud = snd_soc_dai_get_drvdata(dai);
 250
 251	return &cygaud->portinfo[dai->id];
 252}
 253
 254static int audio_ssp_init_portregs(struct cygnus_aio_port *aio)
 255{
 256	u32 value, fci_id;
 257	int status = 0;
 258
 259	switch (aio->port_type) {
 260	case PORT_TDM:
 261		value = readl(aio->cygaud->audio + aio->regs.i2s_stream_cfg);
 262		value &= ~I2S_STREAM_CFG_MASK;
 263
 264		/* Set Group ID */
 265		writel(aio->portnum,
 266			aio->cygaud->audio + aio->regs.bf_sourcech_grp);
 267
 268		/* Configure the AUD_FMM_IOP_OUT_I2S_x_STREAM_CFG reg */
 269		value |= aio->portnum << I2S_OUT_STREAM_CFG_GROUP_ID;
 270		value |= aio->portnum; /* FCI ID is the port num */
 271		value |= CH_GRP_STEREO << I2S_OUT_STREAM_CFG_CHANNEL_GROUPING;
 272		writel(value, aio->cygaud->audio + aio->regs.i2s_stream_cfg);
 273
 274		/* Configure the AUD_FMM_BF_CTRL_SOURCECH_CFGX reg */
 275		value = readl(aio->cygaud->audio + aio->regs.bf_sourcech_cfg);
 276		value &= ~BIT(BF_SRC_CFGX_NOT_PAUSE_WHEN_EMPTY);
 277		value |= BIT(BF_SRC_CFGX_SFIFO_SZ_DOUBLE);
 278		value |= BIT(BF_SRC_CFGX_PROCESS_SEQ_ID_VALID);
 279		writel(value, aio->cygaud->audio + aio->regs.bf_sourcech_cfg);
 280
 281		/* Configure the AUD_FMM_IOP_IN_I2S_x_CAP_STREAM_CFG_0 reg */
 282		value = readl(aio->cygaud->i2s_in +
 283			aio->regs.i2s_cap_stream_cfg);
 284		value &= ~I2S_CAP_STREAM_CFG_MASK;
 285		value |= aio->portnum << I2S_IN_STREAM_CFG_0_GROUP_ID;
 286		writel(value, aio->cygaud->i2s_in +
 287			aio->regs.i2s_cap_stream_cfg);
 288
 289		/* Configure the AUD_FMM_BF_CTRL_DESTCH_CFGX_REG_BASE reg */
 290		fci_id = CAPTURE_FCI_ID_BASE + aio->portnum;
 291
 292		value = readl(aio->cygaud->audio + aio->regs.bf_destch_cfg);
 293		value |= BIT(BF_DST_CFGX_DFIFO_SZ_DOUBLE);
 294		value &= ~BIT(BF_DST_CFGX_NOT_PAUSE_WHEN_FULL);
 295		value |= (fci_id << BF_DST_CFGX_FCI_ID);
 296		value |= BIT(BF_DST_CFGX_PROC_SEQ_ID_VALID);
 297		writel(value, aio->cygaud->audio + aio->regs.bf_destch_cfg);
 298
 299		/* Enable the transmit pin for this port */
 300		value = readl(aio->cygaud->audio + AUD_MISC_SEROUT_OE_REG_BASE);
 301		value &= ~BIT((aio->portnum * 4) + AUD_MISC_SEROUT_SDAT_OE);
 302		writel(value, aio->cygaud->audio + AUD_MISC_SEROUT_OE_REG_BASE);
 303		break;
 304	case PORT_SPDIF:
 305		writel(aio->portnum, aio->cygaud->audio + BF_SRC_GRP3_OFFSET);
 306
 307		value = readl(aio->cygaud->audio + SPDIF_CTRL_OFFSET);
 308		value |= BIT(SPDIF_0_OUT_DITHER_ENA);
 309		writel(value, aio->cygaud->audio + SPDIF_CTRL_OFFSET);
 310
 311		/* Enable and set the FCI ID for the SPDIF channel */
 312		value = readl(aio->cygaud->audio + SPDIF_STREAM_CFG_OFFSET);
 313		value &= ~SPDIF_STREAM_CFG_MASK;
 314		value |= aio->portnum; /* FCI ID is the port num */
 315		value |= BIT(SPDIF_0_OUT_STREAM_ENA);
 316		writel(value, aio->cygaud->audio + SPDIF_STREAM_CFG_OFFSET);
 317
 318		value = readl(aio->cygaud->audio + aio->regs.bf_sourcech_cfg);
 319		value &= ~BIT(BF_SRC_CFGX_NOT_PAUSE_WHEN_EMPTY);
 320		value |= BIT(BF_SRC_CFGX_SFIFO_SZ_DOUBLE);
 321		value |= BIT(BF_SRC_CFGX_PROCESS_SEQ_ID_VALID);
 322		writel(value, aio->cygaud->audio + aio->regs.bf_sourcech_cfg);
 323
 324		/* Enable the spdif output pin */
 325		value = readl(aio->cygaud->audio + AUD_MISC_SEROUT_OE_REG_BASE);
 326		value &= ~BIT(AUD_MISC_SEROUT_SPDIF_OE);
 327		writel(value, aio->cygaud->audio + AUD_MISC_SEROUT_OE_REG_BASE);
 328		break;
 329	default:
 330		dev_err(aio->cygaud->dev, "Port not supported\n");
 331		status = -EINVAL;
 332	}
 333
 334	return status;
 335}
 336
 337static void audio_ssp_in_enable(struct cygnus_aio_port *aio)
 338{
 339	u32 value;
 340
 341	value = readl(aio->cygaud->audio + aio->regs.bf_destch_cfg);
 342	value |= BIT(BF_DST_CFGX_CAP_ENA);
 343	writel(value, aio->cygaud->audio + aio->regs.bf_destch_cfg);
 344
 345	writel(0x1, aio->cygaud->audio + aio->regs.bf_destch_ctrl);
 346
 347	value = readl(aio->cygaud->audio + aio->regs.i2s_cfg);
 348	value |= BIT(I2S_OUT_CFGX_CLK_ENA);
 349	value |= BIT(I2S_OUT_CFGX_DATA_ENABLE);
 350	writel(value, aio->cygaud->audio + aio->regs.i2s_cfg);
 351
 352	value = readl(aio->cygaud->i2s_in + aio->regs.i2s_cap_stream_cfg);
 353	value |= BIT(I2S_IN_STREAM_CFG_CAP_ENA);
 354	writel(value, aio->cygaud->i2s_in + aio->regs.i2s_cap_stream_cfg);
 355
 356	aio->streams_on |= CAPTURE_STREAM_MASK;
 357}
 358
 359static void audio_ssp_in_disable(struct cygnus_aio_port *aio)
 360{
 361	u32 value;
 362
 363	value = readl(aio->cygaud->i2s_in + aio->regs.i2s_cap_stream_cfg);
 364	value &= ~BIT(I2S_IN_STREAM_CFG_CAP_ENA);
 365	writel(value, aio->cygaud->i2s_in + aio->regs.i2s_cap_stream_cfg);
 366
 367	aio->streams_on &= ~CAPTURE_STREAM_MASK;
 368
 369	/* If both playback and capture are off */
 370	if (!aio->streams_on) {
 371		value = readl(aio->cygaud->audio + aio->regs.i2s_cfg);
 372		value &= ~BIT(I2S_OUT_CFGX_CLK_ENA);
 373		value &= ~BIT(I2S_OUT_CFGX_DATA_ENABLE);
 374		writel(value, aio->cygaud->audio + aio->regs.i2s_cfg);
 375	}
 376
 377	writel(0x0, aio->cygaud->audio + aio->regs.bf_destch_ctrl);
 378
 379	value = readl(aio->cygaud->audio + aio->regs.bf_destch_cfg);
 380	value &= ~BIT(BF_DST_CFGX_CAP_ENA);
 381	writel(value, aio->cygaud->audio + aio->regs.bf_destch_cfg);
 382}
 383
 384static int audio_ssp_out_enable(struct cygnus_aio_port *aio)
 385{
 386	u32 value;
 387	int status = 0;
 388
 389	switch (aio->port_type) {
 390	case PORT_TDM:
 391		value = readl(aio->cygaud->audio + aio->regs.i2s_stream_cfg);
 392		value |= BIT(I2S_OUT_STREAM_ENA);
 393		writel(value, aio->cygaud->audio + aio->regs.i2s_stream_cfg);
 394
 395		writel(1, aio->cygaud->audio + aio->regs.bf_sourcech_ctrl);
 396
 397		value = readl(aio->cygaud->audio + aio->regs.i2s_cfg);
 398		value |= BIT(I2S_OUT_CFGX_CLK_ENA);
 399		value |= BIT(I2S_OUT_CFGX_DATA_ENABLE);
 400		writel(value, aio->cygaud->audio + aio->regs.i2s_cfg);
 401
 402		value = readl(aio->cygaud->audio + aio->regs.bf_sourcech_cfg);
 403		value |= BIT(BF_SRC_CFGX_SFIFO_ENA);
 404		writel(value, aio->cygaud->audio + aio->regs.bf_sourcech_cfg);
 405
 406		aio->streams_on |= PLAYBACK_STREAM_MASK;
 407		break;
 408	case PORT_SPDIF:
 409		value = readl(aio->cygaud->audio + SPDIF_FORMAT_CFG_OFFSET);
 410		value |= 0x3;
 411		writel(value, aio->cygaud->audio + SPDIF_FORMAT_CFG_OFFSET);
 412
 413		writel(1, aio->cygaud->audio + aio->regs.bf_sourcech_ctrl);
 414
 415		value = readl(aio->cygaud->audio + aio->regs.bf_sourcech_cfg);
 416		value |= BIT(BF_SRC_CFGX_SFIFO_ENA);
 417		writel(value, aio->cygaud->audio + aio->regs.bf_sourcech_cfg);
 418		break;
 419	default:
 420		dev_err(aio->cygaud->dev,
 421			"Port not supported %d\n", aio->portnum);
 422		status = -EINVAL;
 423	}
 424
 425	return status;
 426}
 427
 428static int audio_ssp_out_disable(struct cygnus_aio_port *aio)
 429{
 430	u32 value;
 431	int status = 0;
 432
 433	switch (aio->port_type) {
 434	case PORT_TDM:
 435		aio->streams_on &= ~PLAYBACK_STREAM_MASK;
 436
 437		/* If both playback and capture are off */
 438		if (!aio->streams_on) {
 439			value = readl(aio->cygaud->audio + aio->regs.i2s_cfg);
 440			value &= ~BIT(I2S_OUT_CFGX_CLK_ENA);
 441			value &= ~BIT(I2S_OUT_CFGX_DATA_ENABLE);
 442			writel(value, aio->cygaud->audio + aio->regs.i2s_cfg);
 443		}
 444
 445		/* set group_sync_dis = 1 */
 446		value = readl(aio->cygaud->audio + BF_SRC_GRP_SYNC_DIS_OFFSET);
 447		value |= BIT(aio->portnum);
 448		writel(value, aio->cygaud->audio + BF_SRC_GRP_SYNC_DIS_OFFSET);
 449
 450		writel(0, aio->cygaud->audio + aio->regs.bf_sourcech_ctrl);
 451
 452		value = readl(aio->cygaud->audio + aio->regs.bf_sourcech_cfg);
 453		value &= ~BIT(BF_SRC_CFGX_SFIFO_ENA);
 454		writel(value, aio->cygaud->audio + aio->regs.bf_sourcech_cfg);
 455
 456		/* set group_sync_dis = 0 */
 457		value = readl(aio->cygaud->audio + BF_SRC_GRP_SYNC_DIS_OFFSET);
 458		value &= ~BIT(aio->portnum);
 459		writel(value, aio->cygaud->audio + BF_SRC_GRP_SYNC_DIS_OFFSET);
 460
 461		value = readl(aio->cygaud->audio + aio->regs.i2s_stream_cfg);
 462		value &= ~BIT(I2S_OUT_STREAM_ENA);
 463		writel(value, aio->cygaud->audio + aio->regs.i2s_stream_cfg);
 464
 465		/* IOP SW INIT on OUT_I2S_x */
 466		value = readl(aio->cygaud->i2s_in + IOP_SW_INIT_LOGIC);
 467		value |= BIT(aio->portnum);
 468		writel(value, aio->cygaud->i2s_in + IOP_SW_INIT_LOGIC);
 469		value &= ~BIT(aio->portnum);
 470		writel(value, aio->cygaud->i2s_in + IOP_SW_INIT_LOGIC);
 471		break;
 472	case PORT_SPDIF:
 473		value = readl(aio->cygaud->audio + SPDIF_FORMAT_CFG_OFFSET);
 474		value &= ~0x3;
 475		writel(value, aio->cygaud->audio + SPDIF_FORMAT_CFG_OFFSET);
 476		writel(0, aio->cygaud->audio + aio->regs.bf_sourcech_ctrl);
 477
 478		value = readl(aio->cygaud->audio + aio->regs.bf_sourcech_cfg);
 479		value &= ~BIT(BF_SRC_CFGX_SFIFO_ENA);
 480		writel(value, aio->cygaud->audio + aio->regs.bf_sourcech_cfg);
 481		break;
 482	default:
 483		dev_err(aio->cygaud->dev,
 484			"Port not supported %d\n", aio->portnum);
 485		status = -EINVAL;
 486	}
 487
 488	return status;
 489}
 490
 491static int pll_configure_mclk(struct cygnus_audio *cygaud, u32 mclk,
 492	struct cygnus_aio_port *aio)
 493{
 494	int i = 0, error;
 495	bool found = false;
 496	const struct pll_macro_entry *p_entry;
 497	struct clk *ch_clk;
 498
 499	for (i = 0; i < ARRAY_SIZE(pll_predef_mclk); i++) {
 500		p_entry = &pll_predef_mclk[i];
 501		if (p_entry->mclk == mclk) {
 502			found = true;
 503			break;
 504		}
 505	}
 506	if (!found) {
 507		dev_err(cygaud->dev,
 508			"%s No valid mclk freq (%u) found!\n", __func__, mclk);
 509		return -EINVAL;
 510	}
 511
 512	ch_clk = cygaud->audio_clk[p_entry->pll_ch_num];
 513
 514	if ((aio->clk_trace.cap_en) && (!aio->clk_trace.cap_clk_en)) {
 515		error = clk_prepare_enable(ch_clk);
 516		if (error) {
 517			dev_err(cygaud->dev, "%s clk_prepare_enable failed %d\n",
 518				__func__, error);
 519			return error;
 520		}
 521		aio->clk_trace.cap_clk_en = true;
 522	}
 523
 524	if ((aio->clk_trace.play_en) && (!aio->clk_trace.play_clk_en)) {
 525		error = clk_prepare_enable(ch_clk);
 526		if (error) {
 527			dev_err(cygaud->dev, "%s clk_prepare_enable failed %d\n",
 528				__func__, error);
 529			return error;
 530		}
 531		aio->clk_trace.play_clk_en = true;
 532	}
 533
 534	error = clk_set_rate(ch_clk, mclk);
 535	if (error) {
 536		dev_err(cygaud->dev, "%s Set MCLK rate failed: %d\n",
 537			__func__, error);
 538		return error;
 539	}
 540
 541	return p_entry->pll_ch_num;
 542}
 543
 544static int cygnus_ssp_set_clocks(struct cygnus_aio_port *aio)
 545{
 546	u32 value;
 547	u32 mask = 0xf;
 548	u32 sclk;
 549	u32 mclk_rate;
 550	unsigned int bit_rate;
 551	unsigned int ratio;
 552
 553	bit_rate = aio->bit_per_frame * aio->lrclk;
 554
 555	/*
 556	 * Check if the bit clock can be generated from the given MCLK.
 557	 * MCLK must be a perfect multiple of bit clock and must be one of the
 558	 * following values... (2,4,6,8,10,12,14)
 559	 */
 560	if ((aio->mclk % bit_rate) != 0)
 561		return -EINVAL;
 562
 563	ratio = aio->mclk / bit_rate;
 564	switch (ratio) {
 565	case 2:
 566	case 4:
 567	case 6:
 568	case 8:
 569	case 10:
 570	case 12:
 571	case 14:
 572		mclk_rate = ratio / 2;
 573		break;
 574
 575	default:
 576		dev_err(aio->cygaud->dev,
 577			"Invalid combination of MCLK and BCLK\n");
 578		dev_err(aio->cygaud->dev, "lrclk = %u, bits/frame = %u, mclk = %u\n",
 579			aio->lrclk, aio->bit_per_frame, aio->mclk);
 580		return -EINVAL;
 581	}
 582
 583	/* Set sclk rate */
 584	switch (aio->port_type) {
 585	case PORT_TDM:
 586		sclk = aio->bit_per_frame;
 587		if (sclk == 512)
 588			sclk = 0;
 589
 590		/* sclks_per_1fs_div = sclk cycles/32 */
 591		sclk /= 32;
 592
 593		/* Set number of bitclks per frame */
 594		value = readl(aio->cygaud->audio + aio->regs.i2s_cfg);
 595		value &= ~(mask << I2S_OUT_CFGX_SCLKS_PER_1FS_DIV32);
 596		value |= sclk << I2S_OUT_CFGX_SCLKS_PER_1FS_DIV32;
 597		writel(value, aio->cygaud->audio + aio->regs.i2s_cfg);
 598		dev_dbg(aio->cygaud->dev,
 599			"SCLKS_PER_1FS_DIV32 = 0x%x\n", value);
 600		break;
 601	case PORT_SPDIF:
 602		break;
 603	default:
 604		dev_err(aio->cygaud->dev, "Unknown port type\n");
 605		return -EINVAL;
 606	}
 607
 608	/* Set MCLK_RATE ssp port (spdif and ssp are the same) */
 609	value = readl(aio->cygaud->audio + aio->regs.i2s_mclk_cfg);
 610	value &= ~(0xf << I2S_OUT_MCLKRATE_SHIFT);
 611	value |= (mclk_rate << I2S_OUT_MCLKRATE_SHIFT);
 612	writel(value, aio->cygaud->audio + aio->regs.i2s_mclk_cfg);
 613
 614	dev_dbg(aio->cygaud->dev, "mclk cfg reg = 0x%x\n", value);
 615	dev_dbg(aio->cygaud->dev, "bits per frame = %u, mclk = %u Hz, lrclk = %u Hz\n",
 616			aio->bit_per_frame, aio->mclk, aio->lrclk);
 617	return 0;
 618}
 619
 620static int cygnus_ssp_hw_params(struct snd_pcm_substream *substream,
 621				 struct snd_pcm_hw_params *params,
 622				 struct snd_soc_dai *dai)
 623{
 624	struct cygnus_aio_port *aio = cygnus_dai_get_portinfo(dai);
 625	int rate, bitres;
 626	u32 value;
 627	u32 mask = 0x1f;
 628	int ret = 0;
 629
 630	dev_dbg(aio->cygaud->dev, "%s port = %d\n", __func__, aio->portnum);
 631	dev_dbg(aio->cygaud->dev, "params_channels %d\n",
 632			params_channels(params));
 633	dev_dbg(aio->cygaud->dev, "rate %d\n", params_rate(params));
 634	dev_dbg(aio->cygaud->dev, "format %d\n", params_format(params));
 635
 636	rate = params_rate(params);
 637
 638	switch (aio->mode) {
 639	case CYGNUS_SSPMODE_TDM:
 640		if ((rate == 192000) && (params_channels(params) > 4)) {
 641			dev_err(aio->cygaud->dev, "Cannot run %d channels at %dHz\n",
 642				params_channels(params), rate);
 643			return -EINVAL;
 644		}
 645		break;
 646	case CYGNUS_SSPMODE_I2S:
 647		aio->bit_per_frame = 64; /* I2S must be 64 bit per frame */
 648		break;
 649	default:
 650		dev_err(aio->cygaud->dev,
 651			"%s port running in unknown mode\n", __func__);
 652		return -EINVAL;
 653	}
 654
 655	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
 656		value = readl(aio->cygaud->audio + aio->regs.bf_sourcech_cfg);
 657		value &= ~BIT(BF_SRC_CFGX_BUFFER_PAIR_ENABLE);
 658		value &= ~BIT(BF_SRC_CFGX_SAMPLE_CH_MODE);
 659		writel(value, aio->cygaud->audio + aio->regs.bf_sourcech_cfg);
 660
 661		switch (params_format(params)) {
 662		case SNDRV_PCM_FORMAT_S16_LE:
 663			bitres = 16;
 664			break;
 665
 666		case SNDRV_PCM_FORMAT_S32_LE:
 667			/* 32 bit mode is coded as 0 */
 668			bitres = 0;
 669			break;
 670
 671		default:
 672			return -EINVAL;
 673		}
 674
 675		value = readl(aio->cygaud->audio + aio->regs.bf_sourcech_cfg);
 676		value &= ~(mask << BF_SRC_CFGX_BIT_RES);
 677		value |= (bitres << BF_SRC_CFGX_BIT_RES);
 678		writel(value, aio->cygaud->audio + aio->regs.bf_sourcech_cfg);
 679
 680	} else {
 681
 682		switch (params_format(params)) {
 683		case SNDRV_PCM_FORMAT_S16_LE:
 684			value = readl(aio->cygaud->audio +
 685					aio->regs.bf_destch_cfg);
 686			value |= BIT(BF_DST_CFGX_CAP_MODE);
 687			writel(value, aio->cygaud->audio +
 688					aio->regs.bf_destch_cfg);
 689			break;
 690
 691		case SNDRV_PCM_FORMAT_S32_LE:
 692			value = readl(aio->cygaud->audio +
 693					aio->regs.bf_destch_cfg);
 694			value &= ~BIT(BF_DST_CFGX_CAP_MODE);
 695			writel(value, aio->cygaud->audio +
 696					aio->regs.bf_destch_cfg);
 697			break;
 698
 699		default:
 700			return -EINVAL;
 701		}
 702	}
 703
 704	aio->lrclk = rate;
 705
 706	if (!aio->is_slave)
 707		ret = cygnus_ssp_set_clocks(aio);
 708
 709	return ret;
 710}
 711
 712/*
 713 * This function sets the mclk frequency for pll clock
 714 */
 715static int cygnus_ssp_set_sysclk(struct snd_soc_dai *dai,
 716			int clk_id, unsigned int freq, int dir)
 717{
 718	int sel;
 719	u32 value;
 720	struct cygnus_aio_port *aio = cygnus_dai_get_portinfo(dai);
 721	struct cygnus_audio *cygaud = snd_soc_dai_get_drvdata(dai);
 722
 723	dev_dbg(aio->cygaud->dev,
 724		"%s Enter port = %d\n", __func__, aio->portnum);
 725	sel = pll_configure_mclk(cygaud, freq, aio);
 726	if (sel < 0) {
 727		dev_err(aio->cygaud->dev,
 728			"%s Setting mclk failed.\n", __func__);
 729		return -EINVAL;
 730	}
 731
 732	aio->mclk = freq;
 733
 734	dev_dbg(aio->cygaud->dev, "%s Setting MCLKSEL to %d\n", __func__, sel);
 735	value = readl(aio->cygaud->audio + aio->regs.i2s_mclk_cfg);
 736	value &= ~(0xf << I2S_OUT_PLLCLKSEL_SHIFT);
 737	value |= (sel << I2S_OUT_PLLCLKSEL_SHIFT);
 738	writel(value, aio->cygaud->audio + aio->regs.i2s_mclk_cfg);
 739
 740	return 0;
 741}
 742
 743static int cygnus_ssp_startup(struct snd_pcm_substream *substream,
 744			       struct snd_soc_dai *dai)
 745{
 746	struct cygnus_aio_port *aio = cygnus_dai_get_portinfo(dai);
 747
 748	snd_soc_dai_set_dma_data(dai, substream, aio);
 749	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
 750		aio->clk_trace.play_en = true;
 751	else
 752		aio->clk_trace.cap_en = true;
 753
 754	substream->runtime->hw.rate_min = CYGNUS_RATE_MIN;
 755	substream->runtime->hw.rate_max = CYGNUS_RATE_MAX;
 756
 757	snd_pcm_hw_constraint_list(substream->runtime, 0,
 758			SNDRV_PCM_HW_PARAM_RATE, &cygnus_rate_constraint);
 759	return 0;
 760}
 761
 762static void cygnus_ssp_shutdown(struct snd_pcm_substream *substream,
 763			       struct snd_soc_dai *dai)
 764{
 765	struct cygnus_aio_port *aio = cygnus_dai_get_portinfo(dai);
 766
 767	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
 768		aio->clk_trace.play_en = false;
 769	else
 770		aio->clk_trace.cap_en = false;
 771
 772	if (!aio->is_slave) {
 773		u32 val;
 774
 775		val = readl(aio->cygaud->audio + aio->regs.i2s_mclk_cfg);
 776		val &= CYGNUS_PLLCLKSEL_MASK;
 777		if (val >= ARRAY_SIZE(aio->cygaud->audio_clk)) {
 778			dev_err(aio->cygaud->dev, "Clk index %u is out of bounds\n",
 779				val);
 780			return;
 781		}
 782
 783		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
 784			if (aio->clk_trace.play_clk_en) {
 785				clk_disable_unprepare(aio->cygaud->
 786						audio_clk[val]);
 787				aio->clk_trace.play_clk_en = false;
 788			}
 789		} else {
 790			if (aio->clk_trace.cap_clk_en) {
 791				clk_disable_unprepare(aio->cygaud->
 792						audio_clk[val]);
 793				aio->clk_trace.cap_clk_en = false;
 794			}
 795		}
 796	}
 797}
 798
 799/*
 800 * Bit    Update  Notes
 801 * 31     Yes     TDM Mode        (1 = TDM, 0 = i2s)
 802 * 30     Yes     Slave Mode	  (1 = Slave, 0 = Master)
 803 * 29:26  No      Sclks per frame
 804 * 25:18  Yes     FS Width
 805 * 17:14  No      Valid Slots
 806 * 13     No      Bits		  (1 = 16 bits, 0 = 32 bits)
 807 * 12:08  No     Bits per samp
 808 * 07     Yes     Justifcation    (1 = LSB, 0 = MSB)
 809 * 06     Yes     Alignment       (1 = Delay 1 clk, 0 = no delay
 810 * 05     Yes     SCLK polarity   (1 = Rising, 0 = Falling)
 811 * 04     Yes     LRCLK Polarity  (1 = High for left, 0 = Low for left)
 812 * 03:02  Yes     Reserved - write as zero
 813 * 01     No      Data Enable
 814 * 00     No      CLK Enable
 815 */
 816#define I2S_OUT_CFG_REG_UPDATE_MASK   0x3C03FF03
 817
 818/* Input cfg is same as output, but the FS width is not a valid field */
 819#define I2S_IN_CFG_REG_UPDATE_MASK  (I2S_OUT_CFG_REG_UPDATE_MASK | 0x03FC0000)
 820
 821int cygnus_ssp_set_custom_fsync_width(struct snd_soc_dai *cpu_dai, int len)
 822{
 823	struct cygnus_aio_port *aio = cygnus_dai_get_portinfo(cpu_dai);
 824
 825	if ((len > 0) && (len < 256)) {
 826		aio->fsync_width = len;
 827		return 0;
 828	} else {
 829		return -EINVAL;
 830	}
 831}
 832EXPORT_SYMBOL_GPL(cygnus_ssp_set_custom_fsync_width);
 833
 834static int cygnus_ssp_set_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
 835{
 836	struct cygnus_aio_port *aio = cygnus_dai_get_portinfo(cpu_dai);
 837	u32 ssp_curcfg;
 838	u32 ssp_newcfg;
 839	u32 ssp_outcfg;
 840	u32 ssp_incfg;
 841	u32 val;
 842	u32 mask;
 843
 844	dev_dbg(aio->cygaud->dev, "%s Enter  fmt: %x\n", __func__, fmt);
 845
 846	if (aio->port_type == PORT_SPDIF)
 847		return -EINVAL;
 848
 849	ssp_newcfg = 0;
 850
 851	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
 852	case SND_SOC_DAIFMT_CBM_CFM:
 853		ssp_newcfg |= BIT(I2S_OUT_CFGX_SLAVE_MODE);
 854		aio->is_slave = 1;
 855		break;
 856	case SND_SOC_DAIFMT_CBS_CFS:
 857		ssp_newcfg &= ~BIT(I2S_OUT_CFGX_SLAVE_MODE);
 858		aio->is_slave = 0;
 859		break;
 860	default:
 861		return -EINVAL;
 862	}
 863
 864	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
 865	case SND_SOC_DAIFMT_I2S:
 866		ssp_newcfg |= BIT(I2S_OUT_CFGX_DATA_ALIGNMENT);
 867		ssp_newcfg |= BIT(I2S_OUT_CFGX_FSYNC_WIDTH);
 868		aio->mode = CYGNUS_SSPMODE_I2S;
 869		break;
 870
 871	case SND_SOC_DAIFMT_DSP_A:
 872	case SND_SOC_DAIFMT_DSP_B:
 873		ssp_newcfg |= BIT(I2S_OUT_CFGX_TDM_MODE);
 874
 875		/* DSP_A = data after FS, DSP_B = data during FS */
 876		if ((fmt & SND_SOC_DAIFMT_FORMAT_MASK) == SND_SOC_DAIFMT_DSP_A)
 877			ssp_newcfg |= BIT(I2S_OUT_CFGX_DATA_ALIGNMENT);
 878
 879		if ((aio->fsync_width > 0) && (aio->fsync_width < 256))
 880			ssp_newcfg |=
 881				(aio->fsync_width << I2S_OUT_CFGX_FSYNC_WIDTH);
 882		else
 883			ssp_newcfg |= BIT(I2S_OUT_CFGX_FSYNC_WIDTH);
 884
 885		aio->mode = CYGNUS_SSPMODE_TDM;
 886		break;
 887
 888	default:
 889		return -EINVAL;
 890	}
 891
 892	/*
 893	 * SSP out cfg.
 894	 * Retain bits we do not want to update, then OR in new bits
 895	 */
 896	ssp_curcfg = readl(aio->cygaud->audio + aio->regs.i2s_cfg);
 897	ssp_outcfg = (ssp_curcfg & I2S_OUT_CFG_REG_UPDATE_MASK) | ssp_newcfg;
 898	writel(ssp_outcfg, aio->cygaud->audio + aio->regs.i2s_cfg);
 899
 900	/*
 901	 * SSP in cfg.
 902	 * Retain bits we do not want to update, then OR in new bits
 903	 */
 904	ssp_curcfg = readl(aio->cygaud->i2s_in + aio->regs.i2s_cap_cfg);
 905	ssp_incfg = (ssp_curcfg & I2S_IN_CFG_REG_UPDATE_MASK) | ssp_newcfg;
 906	writel(ssp_incfg, aio->cygaud->i2s_in + aio->regs.i2s_cap_cfg);
 907
 908	val = readl(aio->cygaud->audio + AUD_MISC_SEROUT_OE_REG_BASE);
 909
 910	/*
 911	 * Configure the word clk and bit clk as output or tristate
 912	 * Each port has 4 bits for controlling its pins.
 913	 * Shift the mask based upon port number.
 914	 */
 915	mask = BIT(AUD_MISC_SEROUT_LRCK_OE)
 916			| BIT(AUD_MISC_SEROUT_SCLK_OE)
 917			| BIT(AUD_MISC_SEROUT_MCLK_OE);
 918	mask = mask << (aio->portnum * 4);
 919	if (aio->is_slave)
 920		/* Set bit for tri-state */
 921		val |= mask;
 922	else
 923		/* Clear bit for drive */
 924		val &= ~mask;
 925
 926	dev_dbg(aio->cygaud->dev, "%s  Set OE bits 0x%x\n", __func__, val);
 927	writel(val, aio->cygaud->audio + AUD_MISC_SEROUT_OE_REG_BASE);
 928
 929	return 0;
 930}
 931
 932static int cygnus_ssp_trigger(struct snd_pcm_substream *substream, int cmd,
 933			       struct snd_soc_dai *dai)
 934{
 935	struct cygnus_aio_port *aio = cygnus_dai_get_portinfo(dai);
 936	struct cygnus_audio *cygaud = snd_soc_dai_get_drvdata(dai);
 937
 938	dev_dbg(aio->cygaud->dev,
 939		"%s cmd %d at port = %d\n", __func__, cmd, aio->portnum);
 940
 941	switch (cmd) {
 942	case SNDRV_PCM_TRIGGER_START:
 943	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
 944	case SNDRV_PCM_TRIGGER_RESUME:
 945		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
 946			audio_ssp_out_enable(aio);
 947		else
 948			audio_ssp_in_enable(aio);
 949		cygaud->active_ports++;
 950
 951		break;
 952
 953	case SNDRV_PCM_TRIGGER_STOP:
 954	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
 955	case SNDRV_PCM_TRIGGER_SUSPEND:
 956		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
 957			audio_ssp_out_disable(aio);
 958		else
 959			audio_ssp_in_disable(aio);
 960		cygaud->active_ports--;
 961		break;
 962
 963	default:
 964		return -EINVAL;
 965	}
 966
 967	return 0;
 968}
 969
 970static int cygnus_set_dai_tdm_slot(struct snd_soc_dai *cpu_dai,
 971	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
 972{
 973	struct cygnus_aio_port *aio = cygnus_dai_get_portinfo(cpu_dai);
 974	u32 value;
 975	int bits_per_slot = 0;     /* default to 32-bits per slot */
 976	int frame_bits;
 977	unsigned int active_slots;
 978	bool found = false;
 979	int i;
 980
 981	if (tx_mask != rx_mask) {
 982		dev_err(aio->cygaud->dev,
 983			"%s tx_mask must equal rx_mask\n", __func__);
 984		return -EINVAL;
 985	}
 986
 987	active_slots = hweight32(tx_mask);
 988
 989	if (active_slots > 16)
 990		return -EINVAL;
 991
 992	/* Slot value must be even */
 993	if (active_slots % 2)
 994		return -EINVAL;
 995
 996	/* We encode 16 slots as 0 in the reg */
 997	if (active_slots == 16)
 998		active_slots = 0;
 999
1000	/* Slot Width is either 16 or 32 */
1001	switch (slot_width) {
1002	case 16:
1003		bits_per_slot = 1;
1004		break;
1005	case 32:
1006		bits_per_slot = 0;
1007		break;
1008	default:
1009		bits_per_slot = 0;
1010		dev_warn(aio->cygaud->dev,
1011			"%s Defaulting Slot Width to 32\n", __func__);
1012	}
1013
1014	frame_bits = slots * slot_width;
1015
1016	for (i = 0; i < ARRAY_SIZE(ssp_valid_tdm_framesize); i++) {
1017		if (ssp_valid_tdm_framesize[i] == frame_bits) {
1018			found = true;
1019			break;
1020		}
1021	}
1022
1023	if (!found) {
1024		dev_err(aio->cygaud->dev,
1025			"%s In TDM mode, frame bits INVALID (%d)\n",
1026			__func__, frame_bits);
1027		return -EINVAL;
1028	}
1029
1030	aio->bit_per_frame = frame_bits;
1031
1032	dev_dbg(aio->cygaud->dev, "%s active_slots %u, bits per frame %d\n",
1033			__func__, active_slots, frame_bits);
1034
1035	/* Set capture side of ssp port */
1036	value = readl(aio->cygaud->i2s_in + aio->regs.i2s_cap_cfg);
1037	value &= ~(0xf << I2S_OUT_CFGX_VALID_SLOT);
1038	value |= (active_slots << I2S_OUT_CFGX_VALID_SLOT);
1039	value &= ~BIT(I2S_OUT_CFGX_BITS_PER_SLOT);
1040	value |= (bits_per_slot << I2S_OUT_CFGX_BITS_PER_SLOT);
1041	writel(value, aio->cygaud->i2s_in + aio->regs.i2s_cap_cfg);
1042
1043	/* Set playback side of ssp port */
1044	value = readl(aio->cygaud->audio + aio->regs.i2s_cfg);
1045	value &= ~(0xf << I2S_OUT_CFGX_VALID_SLOT);
1046	value |= (active_slots << I2S_OUT_CFGX_VALID_SLOT);
1047	value &= ~BIT(I2S_OUT_CFGX_BITS_PER_SLOT);
1048	value |= (bits_per_slot << I2S_OUT_CFGX_BITS_PER_SLOT);
1049	writel(value, aio->cygaud->audio + aio->regs.i2s_cfg);
1050
1051	return 0;
1052}
1053
1054#ifdef CONFIG_PM_SLEEP
1055static int cygnus_ssp_suspend(struct snd_soc_dai *cpu_dai)
1056{
1057	struct cygnus_aio_port *aio = cygnus_dai_get_portinfo(cpu_dai);
1058
 
 
 
1059	if (!aio->is_slave) {
1060		u32 val;
1061
1062		val = readl(aio->cygaud->audio + aio->regs.i2s_mclk_cfg);
1063		val &= CYGNUS_PLLCLKSEL_MASK;
1064		if (val >= ARRAY_SIZE(aio->cygaud->audio_clk)) {
1065			dev_err(aio->cygaud->dev, "Clk index %u is out of bounds\n",
1066				val);
1067			return -EINVAL;
1068		}
1069
1070		if (aio->clk_trace.cap_clk_en)
1071			clk_disable_unprepare(aio->cygaud->audio_clk[val]);
1072		if (aio->clk_trace.play_clk_en)
1073			clk_disable_unprepare(aio->cygaud->audio_clk[val]);
1074
1075		aio->pll_clk_num = val;
1076	}
1077
1078	return 0;
1079}
1080
1081static int cygnus_ssp_resume(struct snd_soc_dai *cpu_dai)
 
 
 
 
 
 
 
 
 
 
 
1082{
1083	struct cygnus_aio_port *aio = cygnus_dai_get_portinfo(cpu_dai);
1084	int error;
1085
 
 
 
1086	if (!aio->is_slave) {
1087		if (aio->clk_trace.cap_clk_en) {
1088			error = clk_prepare_enable(aio->cygaud->
1089					audio_clk[aio->pll_clk_num]);
1090			if (error) {
1091				dev_err(aio->cygaud->dev, "%s clk_prepare_enable failed\n",
1092					__func__);
1093				return -EINVAL;
1094			}
1095		}
1096		if (aio->clk_trace.play_clk_en) {
1097			error = clk_prepare_enable(aio->cygaud->
1098					audio_clk[aio->pll_clk_num]);
1099			if (error) {
1100				if (aio->clk_trace.cap_clk_en)
1101					clk_disable_unprepare(aio->cygaud->
1102						audio_clk[aio->pll_clk_num]);
1103				dev_err(aio->cygaud->dev, "%s clk_prepare_enable failed\n",
1104					__func__);
1105				return -EINVAL;
1106			}
1107		}
1108	}
1109
1110	return 0;
1111}
 
 
 
 
 
 
 
 
 
 
 
 
1112#else
1113#define cygnus_ssp_suspend NULL
1114#define cygnus_ssp_resume  NULL
1115#endif
1116
1117static const struct snd_soc_dai_ops cygnus_ssp_dai_ops = {
1118	.startup	= cygnus_ssp_startup,
1119	.shutdown	= cygnus_ssp_shutdown,
1120	.trigger	= cygnus_ssp_trigger,
1121	.hw_params	= cygnus_ssp_hw_params,
1122	.set_fmt	= cygnus_ssp_set_fmt,
1123	.set_sysclk	= cygnus_ssp_set_sysclk,
1124	.set_tdm_slot	= cygnus_set_dai_tdm_slot,
1125};
1126
1127static const struct snd_soc_dai_ops cygnus_spdif_dai_ops = {
1128	.startup	= cygnus_ssp_startup,
1129	.shutdown	= cygnus_ssp_shutdown,
1130	.trigger	= cygnus_ssp_trigger,
1131	.hw_params	= cygnus_ssp_hw_params,
1132	.set_sysclk	= cygnus_ssp_set_sysclk,
1133};
1134
1135#define INIT_CPU_DAI(num) { \
1136	.name = "cygnus-ssp" #num, \
1137	.playback = { \
1138		.channels_min = 2, \
1139		.channels_max = 16, \
1140		.rates = SNDRV_PCM_RATE_KNOT, \
1141		.formats = SNDRV_PCM_FMTBIT_S16_LE | \
1142				SNDRV_PCM_FMTBIT_S32_LE, \
1143	}, \
1144	.capture = { \
1145		.channels_min = 2, \
1146		.channels_max = 16, \
1147		.rates = SNDRV_PCM_RATE_KNOT, \
1148		.formats =  SNDRV_PCM_FMTBIT_S16_LE | \
1149				SNDRV_PCM_FMTBIT_S32_LE, \
1150	}, \
1151	.ops = &cygnus_ssp_dai_ops, \
1152	.suspend = cygnus_ssp_suspend, \
1153	.resume = cygnus_ssp_resume, \
1154}
1155
1156static const struct snd_soc_dai_driver cygnus_ssp_dai_info[] = {
1157	INIT_CPU_DAI(0),
1158	INIT_CPU_DAI(1),
1159	INIT_CPU_DAI(2),
1160};
1161
1162static const struct snd_soc_dai_driver cygnus_spdif_dai_info = {
1163	.name = "cygnus-spdif",
1164	.playback = {
1165		.channels_min = 2,
1166		.channels_max = 2,
1167		.rates = SNDRV_PCM_RATE_KNOT,
1168		.formats = SNDRV_PCM_FMTBIT_S16_LE |
1169			SNDRV_PCM_FMTBIT_S32_LE,
1170	},
1171	.ops = &cygnus_spdif_dai_ops,
1172	.suspend = cygnus_ssp_suspend,
1173	.resume = cygnus_ssp_resume,
1174};
1175
1176static struct snd_soc_dai_driver cygnus_ssp_dai[CYGNUS_MAX_PORTS];
1177
1178static const struct snd_soc_component_driver cygnus_ssp_component = {
1179	.name		= "cygnus-audio",
 
 
 
1180};
1181
1182/*
1183 * Return < 0 if error
1184 * Return 0 if disabled
1185 * Return 1 if enabled and node is parsed successfully
1186 */
1187static int parse_ssp_child_node(struct platform_device *pdev,
1188				struct device_node *dn,
1189				struct cygnus_audio *cygaud,
1190				struct snd_soc_dai_driver *p_dai)
1191{
1192	struct cygnus_aio_port *aio;
1193	struct cygnus_ssp_regs ssp_regs[3];
1194	u32 rawval;
1195	int portnum = -1;
1196	enum cygnus_audio_port_type port_type;
1197
1198	if (of_property_read_u32(dn, "reg", &rawval)) {
1199		dev_err(&pdev->dev, "Missing reg property\n");
1200		return -EINVAL;
1201	}
1202
1203	portnum = rawval;
1204	switch (rawval) {
1205	case 0:
1206		ssp_regs[0] = INIT_SSP_REGS(0);
1207		port_type = PORT_TDM;
1208		break;
1209	case 1:
1210		ssp_regs[1] = INIT_SSP_REGS(1);
1211		port_type = PORT_TDM;
1212		break;
1213	case 2:
1214		ssp_regs[2] = INIT_SSP_REGS(2);
1215		port_type = PORT_TDM;
1216		break;
1217	case 3:
1218		port_type = PORT_SPDIF;
1219		break;
1220	default:
1221		dev_err(&pdev->dev, "Bad value for reg %u\n", rawval);
1222		return -EINVAL;
1223	}
1224
1225	aio = &cygaud->portinfo[portnum];
1226	aio->cygaud = cygaud;
1227	aio->portnum = portnum;
1228	aio->port_type = port_type;
1229	aio->fsync_width = -1;
1230
1231	switch (port_type) {
1232	case PORT_TDM:
1233		aio->regs = ssp_regs[portnum];
1234		*p_dai = cygnus_ssp_dai_info[portnum];
1235		aio->mode = CYGNUS_SSPMODE_UNKNOWN;
1236		break;
1237
1238	case PORT_SPDIF:
1239		aio->regs.bf_sourcech_cfg = BF_SRC_CFG3_OFFSET;
1240		aio->regs.bf_sourcech_ctrl = BF_SRC_CTRL3_OFFSET;
1241		aio->regs.i2s_mclk_cfg = SPDIF_MCLK_CFG_OFFSET;
1242		aio->regs.i2s_stream_cfg = SPDIF_STREAM_CFG_OFFSET;
1243		*p_dai = cygnus_spdif_dai_info;
1244
1245		/* For the purposes of this code SPDIF can be I2S mode */
1246		aio->mode = CYGNUS_SSPMODE_I2S;
1247		break;
1248	default:
1249		dev_err(&pdev->dev, "Bad value for port_type %d\n", port_type);
1250		return -EINVAL;
1251	}
1252
1253	dev_dbg(&pdev->dev, "%s portnum = %d\n", __func__, aio->portnum);
1254	aio->streams_on = 0;
1255	aio->cygaud->dev = &pdev->dev;
1256	aio->clk_trace.play_en = false;
1257	aio->clk_trace.cap_en = false;
1258
1259	audio_ssp_init_portregs(aio);
1260	return 0;
1261}
1262
1263static int audio_clk_init(struct platform_device *pdev,
1264						struct cygnus_audio *cygaud)
1265{
1266	int i;
1267	char clk_name[PROP_LEN_MAX];
1268
1269	for (i = 0; i < ARRAY_SIZE(cygaud->audio_clk); i++) {
1270		snprintf(clk_name, PROP_LEN_MAX, "ch%d_audio", i);
1271
1272		cygaud->audio_clk[i] = devm_clk_get(&pdev->dev, clk_name);
1273		if (IS_ERR(cygaud->audio_clk[i]))
1274			return PTR_ERR(cygaud->audio_clk[i]);
1275	}
1276
1277	return 0;
1278}
1279
1280static int cygnus_ssp_probe(struct platform_device *pdev)
1281{
1282	struct device *dev = &pdev->dev;
1283	struct device_node *child_node;
1284	struct resource *res;
1285	struct cygnus_audio *cygaud;
1286	int err = -EINVAL;
1287	int node_count;
1288	int active_port_count;
1289
1290	cygaud = devm_kzalloc(dev, sizeof(struct cygnus_audio), GFP_KERNEL);
1291	if (!cygaud)
1292		return -ENOMEM;
1293
1294	dev_set_drvdata(dev, cygaud);
1295
1296	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "aud");
1297	cygaud->audio = devm_ioremap_resource(dev, res);
1298	if (IS_ERR(cygaud->audio))
1299		return PTR_ERR(cygaud->audio);
1300
1301	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "i2s_in");
1302	cygaud->i2s_in = devm_ioremap_resource(dev, res);
1303	if (IS_ERR(cygaud->i2s_in))
1304		return PTR_ERR(cygaud->i2s_in);
1305
1306	/* Tri-state all controlable pins until we know that we need them */
1307	writel(CYGNUS_SSP_TRISTATE_MASK,
1308			cygaud->audio + AUD_MISC_SEROUT_OE_REG_BASE);
1309
1310	node_count = of_get_child_count(pdev->dev.of_node);
1311	if ((node_count < 1) || (node_count > CYGNUS_MAX_PORTS)) {
1312		dev_err(dev, "child nodes is %d.  Must be between 1 and %d\n",
1313			node_count, CYGNUS_MAX_PORTS);
1314		return -EINVAL;
1315	}
1316
1317	active_port_count = 0;
1318
1319	for_each_available_child_of_node(pdev->dev.of_node, child_node) {
1320		err = parse_ssp_child_node(pdev, child_node, cygaud,
1321					&cygnus_ssp_dai[active_port_count]);
1322
1323		/* negative is err, 0 is active and good, 1 is disabled */
1324		if (err < 0)
 
1325			return err;
 
1326		else if (!err) {
1327			dev_dbg(dev, "Activating DAI: %s\n",
1328				cygnus_ssp_dai[active_port_count].name);
1329			active_port_count++;
1330		}
1331	}
1332
1333	cygaud->dev = dev;
1334	cygaud->active_ports = 0;
1335
1336	dev_dbg(dev, "Registering %d DAIs\n", active_port_count);
1337	err = snd_soc_register_component(dev, &cygnus_ssp_component,
1338				cygnus_ssp_dai, active_port_count);
1339	if (err) {
1340		dev_err(dev, "snd_soc_register_dai failed\n");
1341		return err;
1342	}
1343
1344	cygaud->irq_num = platform_get_irq(pdev, 0);
1345	if (cygaud->irq_num <= 0) {
1346		dev_err(dev, "platform_get_irq failed\n");
1347		err = cygaud->irq_num;
1348		goto err_irq;
1349	}
1350
1351	err = audio_clk_init(pdev, cygaud);
1352	if (err) {
1353		dev_err(dev, "audio clock initialization failed\n");
1354		goto err_irq;
1355	}
1356
1357	err = cygnus_soc_platform_register(dev, cygaud);
1358	if (err) {
1359		dev_err(dev, "platform reg error %d\n", err);
1360		goto err_irq;
1361	}
1362
1363	return 0;
1364
1365err_irq:
1366	snd_soc_unregister_component(dev);
1367	return err;
1368}
1369
1370static int cygnus_ssp_remove(struct platform_device *pdev)
1371{
1372	cygnus_soc_platform_unregister(&pdev->dev);
1373	snd_soc_unregister_component(&pdev->dev);
1374
1375	return 0;
1376}
1377
1378static const struct of_device_id cygnus_ssp_of_match[] = {
1379	{ .compatible = "brcm,cygnus-audio" },
1380	{},
1381};
1382MODULE_DEVICE_TABLE(of, cygnus_ssp_of_match);
1383
1384static struct platform_driver cygnus_ssp_driver = {
1385	.probe		= cygnus_ssp_probe,
1386	.remove		= cygnus_ssp_remove,
1387	.driver		= {
1388		.name	= "cygnus-ssp",
1389		.of_match_table = cygnus_ssp_of_match,
1390	},
1391};
1392
1393module_platform_driver(cygnus_ssp_driver);
1394
1395MODULE_ALIAS("platform:cygnus-ssp");
1396MODULE_LICENSE("GPL v2");
1397MODULE_AUTHOR("Broadcom");
1398MODULE_DESCRIPTION("Cygnus ASoC SSP Interface");