1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Driver for Atmel I2S controller 4 * 5 * Copyright (C) 2015 Atmel Corporation 6 * 7 * Author: Cyrille Pitchen <cyrille.pitchen@atmel.com> 8 */ 9 10 #include <linux/init.h> 11 #include <linux/module.h> 12 #include <linux/device.h> 13 #include <linux/slab.h> 14 #include <linux/delay.h> 15 #include <linux/io.h> 16 #include <linux/clk.h> 17 #include <linux/mfd/syscon.h> 18 19 #include <sound/core.h> 20 #include <sound/pcm.h> 21 #include <sound/pcm_params.h> 22 #include <sound/initval.h> 23 #include <sound/soc.h> 24 #include <sound/dmaengine_pcm.h> 25 26 #define ATMEL_I2SC_MAX_TDM_CHANNELS 8 27 28 /* 29 * ---- I2S Controller Register map ---- 30 */ 31 #define ATMEL_I2SC_CR 0x0000 /* Control Register */ 32 #define ATMEL_I2SC_MR 0x0004 /* Mode Register */ 33 #define ATMEL_I2SC_SR 0x0008 /* Status Register */ 34 #define ATMEL_I2SC_SCR 0x000c /* Status Clear Register */ 35 #define ATMEL_I2SC_SSR 0x0010 /* Status Set Register */ 36 #define ATMEL_I2SC_IER 0x0014 /* Interrupt Enable Register */ 37 #define ATMEL_I2SC_IDR 0x0018 /* Interrupt Disable Register */ 38 #define ATMEL_I2SC_IMR 0x001c /* Interrupt Mask Register */ 39 #define ATMEL_I2SC_RHR 0x0020 /* Receiver Holding Register */ 40 #define ATMEL_I2SC_THR 0x0024 /* Transmitter Holding Register */ 41 #define ATMEL_I2SC_VERSION 0x0028 /* Version Register */ 42 43 /* 44 * ---- Control Register (Write-only) ---- 45 */ 46 #define ATMEL_I2SC_CR_RXEN BIT(0) /* Receiver Enable */ 47 #define ATMEL_I2SC_CR_RXDIS BIT(1) /* Receiver Disable */ 48 #define ATMEL_I2SC_CR_CKEN BIT(2) /* Clock Enable */ 49 #define ATMEL_I2SC_CR_CKDIS BIT(3) /* Clock Disable */ 50 #define ATMEL_I2SC_CR_TXEN BIT(4) /* Transmitter Enable */ 51 #define ATMEL_I2SC_CR_TXDIS BIT(5) /* Transmitter Disable */ 52 #define ATMEL_I2SC_CR_SWRST BIT(7) /* Software Reset */ 53 54 /* 55 * ---- Mode Register (Read/Write) ---- 56 */ 57 #define ATMEL_I2SC_MR_MODE_MASK GENMASK(0, 0) 58 #define ATMEL_I2SC_MR_MODE_SLAVE (0 << 0) 59 #define ATMEL_I2SC_MR_MODE_MASTER (1 << 0) 60 61 #define ATMEL_I2SC_MR_DATALENGTH_MASK GENMASK(4, 2) 62 #define ATMEL_I2SC_MR_DATALENGTH_32_BITS (0 << 2) 63 #define ATMEL_I2SC_MR_DATALENGTH_24_BITS (1 << 2) 64 #define ATMEL_I2SC_MR_DATALENGTH_20_BITS (2 << 2) 65 #define ATMEL_I2SC_MR_DATALENGTH_18_BITS (3 << 2) 66 #define ATMEL_I2SC_MR_DATALENGTH_16_BITS (4 << 2) 67 #define ATMEL_I2SC_MR_DATALENGTH_16_BITS_COMPACT (5 << 2) 68 #define ATMEL_I2SC_MR_DATALENGTH_8_BITS (6 << 2) 69 #define ATMEL_I2SC_MR_DATALENGTH_8_BITS_COMPACT (7 << 2) 70 71 #define ATMEL_I2SC_MR_FORMAT_MASK GENMASK(7, 6) 72 #define ATMEL_I2SC_MR_FORMAT_I2S (0 << 6) 73 #define ATMEL_I2SC_MR_FORMAT_LJ (1 << 6) /* Left Justified */ 74 #define ATMEL_I2SC_MR_FORMAT_TDM (2 << 6) 75 #define ATMEL_I2SC_MR_FORMAT_TDMLJ (3 << 6) 76 77 /* Left audio samples duplicated to right audio channel */ 78 #define ATMEL_I2SC_MR_RXMONO BIT(8) 79 80 /* Receiver uses one DMA channel ... */ 81 #define ATMEL_I2SC_MR_RXDMA_MASK GENMASK(9, 9) 82 #define ATMEL_I2SC_MR_RXDMA_SINGLE (0 << 9) /* for all audio channels */ 83 #define ATMEL_I2SC_MR_RXDMA_MULTIPLE (1 << 9) /* per audio channel */ 84 85 /* I2SDO output of I2SC is internally connected to I2SDI input */ 86 #define ATMEL_I2SC_MR_RXLOOP BIT(10) 87 88 /* Left audio samples duplicated to right audio channel */ 89 #define ATMEL_I2SC_MR_TXMONO BIT(12) 90 91 /* Transmitter uses one DMA channel ... */ 92 #define ATMEL_I2SC_MR_TXDMA_MASK GENMASK(13, 13) 93 #define ATMEL_I2SC_MR_TXDMA_SINGLE (0 << 13) /* for all audio channels */ 94 #define ATMEL_I2SC_MR_TXDME_MULTIPLE (1 << 13) /* per audio channel */ 95 96 /* x sample transmitted when underrun */ 97 #define ATMEL_I2SC_MR_TXSAME_MASK GENMASK(14, 14) 98 #define ATMEL_I2SC_MR_TXSAME_ZERO (0 << 14) /* Zero sample */ 99 #define ATMEL_I2SC_MR_TXSAME_PREVIOUS (1 << 14) /* Previous sample */ 100 101 /* Audio Clock to I2SC Master Clock ratio */ 102 #define ATMEL_I2SC_MR_IMCKDIV_MASK GENMASK(21, 16) 103 #define ATMEL_I2SC_MR_IMCKDIV(div) \ 104 (((div) << 16) & ATMEL_I2SC_MR_IMCKDIV_MASK) 105 106 /* Master Clock to fs ratio */ 107 #define ATMEL_I2SC_MR_IMCKFS_MASK GENMASK(29, 24) 108 #define ATMEL_I2SC_MR_IMCKFS(fs) \ 109 (((fs) << 24) & ATMEL_I2SC_MR_IMCKFS_MASK) 110 111 /* Master Clock mode */ 112 #define ATMEL_I2SC_MR_IMCKMODE_MASK GENMASK(30, 30) 113 /* 0: No master clock generated (selected clock drives I2SCK pin) */ 114 #define ATMEL_I2SC_MR_IMCKMODE_I2SCK (0 << 30) 115 /* 1: master clock generated (internally generated clock drives I2SMCK pin) */ 116 #define ATMEL_I2SC_MR_IMCKMODE_I2SMCK (1 << 30) 117 118 /* Slot Width */ 119 /* 0: slot is 32 bits wide for DATALENGTH = 18/20/24 bits. */ 120 /* 1: slot is 24 bits wide for DATALENGTH = 18/20/24 bits. */ 121 #define ATMEL_I2SC_MR_IWS BIT(31) 122 123 /* 124 * ---- Status Registers ---- 125 */ 126 #define ATMEL_I2SC_SR_RXEN BIT(0) /* Receiver Enabled */ 127 #define ATMEL_I2SC_SR_RXRDY BIT(1) /* Receive Ready */ 128 #define ATMEL_I2SC_SR_RXOR BIT(2) /* Receive Overrun */ 129 130 #define ATMEL_I2SC_SR_TXEN BIT(4) /* Transmitter Enabled */ 131 #define ATMEL_I2SC_SR_TXRDY BIT(5) /* Transmit Ready */ 132 #define ATMEL_I2SC_SR_TXUR BIT(6) /* Transmit Underrun */ 133 134 /* Receive Overrun Channel */ 135 #define ATMEL_I2SC_SR_RXORCH_MASK GENMASK(15, 8) 136 #define ATMEL_I2SC_SR_RXORCH(ch) (1 << (((ch) & 0x7) + 8)) 137 138 /* Transmit Underrun Channel */ 139 #define ATMEL_I2SC_SR_TXURCH_MASK GENMASK(27, 20) 140 #define ATMEL_I2SC_SR_TXURCH(ch) (1 << (((ch) & 0x7) + 20)) 141 142 /* 143 * ---- Interrupt Enable/Disable/Mask Registers ---- 144 */ 145 #define ATMEL_I2SC_INT_RXRDY ATMEL_I2SC_SR_RXRDY 146 #define ATMEL_I2SC_INT_RXOR ATMEL_I2SC_SR_RXOR 147 #define ATMEL_I2SC_INT_TXRDY ATMEL_I2SC_SR_TXRDY 148 #define ATMEL_I2SC_INT_TXUR ATMEL_I2SC_SR_TXUR 149 150 static const struct regmap_config atmel_i2s_regmap_config = { 151 .reg_bits = 32, 152 .reg_stride = 4, 153 .val_bits = 32, 154 .max_register = ATMEL_I2SC_VERSION, 155 }; 156 157 struct atmel_i2s_gck_param { 158 int fs; 159 unsigned long mck; 160 int imckdiv; 161 int imckfs; 162 }; 163 164 #define I2S_MCK_12M288 12288000UL 165 #define I2S_MCK_11M2896 11289600UL 166 167 /* mck = (32 * (imckfs+1) / (imckdiv+1)) * fs */ 168 static const struct atmel_i2s_gck_param gck_params[] = { 169 /* mck = 12.288MHz */ 170 { 8000, I2S_MCK_12M288, 0, 47}, /* mck = 1536 fs */ 171 { 16000, I2S_MCK_12M288, 1, 47}, /* mck = 768 fs */ 172 { 24000, I2S_MCK_12M288, 3, 63}, /* mck = 512 fs */ 173 { 32000, I2S_MCK_12M288, 3, 47}, /* mck = 384 fs */ 174 { 48000, I2S_MCK_12M288, 7, 63}, /* mck = 256 fs */ 175 { 64000, I2S_MCK_12M288, 7, 47}, /* mck = 192 fs */ 176 { 96000, I2S_MCK_12M288, 7, 31}, /* mck = 128 fs */ 177 {192000, I2S_MCK_12M288, 7, 15}, /* mck = 64 fs */ 178 179 /* mck = 11.2896MHz */ 180 { 11025, I2S_MCK_11M2896, 1, 63}, /* mck = 1024 fs */ 181 { 22050, I2S_MCK_11M2896, 3, 63}, /* mck = 512 fs */ 182 { 44100, I2S_MCK_11M2896, 7, 63}, /* mck = 256 fs */ 183 { 88200, I2S_MCK_11M2896, 7, 31}, /* mck = 128 fs */ 184 {176400, I2S_MCK_11M2896, 7, 15}, /* mck = 64 fs */ 185 }; 186 187 struct atmel_i2s_dev; 188 189 struct atmel_i2s_caps { 190 int (*mck_init)(struct atmel_i2s_dev *, struct device_node *np); 191 }; 192 193 struct atmel_i2s_dev { 194 struct device *dev; 195 struct regmap *regmap; 196 struct clk *pclk; 197 struct clk *gclk; 198 struct snd_dmaengine_dai_dma_data playback; 199 struct snd_dmaengine_dai_dma_data capture; 200 unsigned int fmt; 201 const struct atmel_i2s_gck_param *gck_param; 202 const struct atmel_i2s_caps *caps; 203 int clk_use_no; 204 }; 205 206 static irqreturn_t atmel_i2s_interrupt(int irq, void *dev_id) 207 { 208 struct atmel_i2s_dev *dev = dev_id; 209 unsigned int sr, imr, pending, ch, mask; 210 irqreturn_t ret = IRQ_NONE; 211 212 regmap_read(dev->regmap, ATMEL_I2SC_SR, &sr); 213 regmap_read(dev->regmap, ATMEL_I2SC_IMR, &imr); 214 pending = sr & imr; 215 216 if (!pending) 217 return IRQ_NONE; 218 219 if (pending & ATMEL_I2SC_INT_RXOR) { 220 mask = ATMEL_I2SC_SR_RXOR; 221 222 for (ch = 0; ch < ATMEL_I2SC_MAX_TDM_CHANNELS; ++ch) { 223 if (sr & ATMEL_I2SC_SR_RXORCH(ch)) { 224 mask |= ATMEL_I2SC_SR_RXORCH(ch); 225 dev_err(dev->dev, 226 "RX overrun on channel %d\n", ch); 227 } 228 } 229 regmap_write(dev->regmap, ATMEL_I2SC_SCR, mask); 230 ret = IRQ_HANDLED; 231 } 232 233 if (pending & ATMEL_I2SC_INT_TXUR) { 234 mask = ATMEL_I2SC_SR_TXUR; 235 236 for (ch = 0; ch < ATMEL_I2SC_MAX_TDM_CHANNELS; ++ch) { 237 if (sr & ATMEL_I2SC_SR_TXURCH(ch)) { 238 mask |= ATMEL_I2SC_SR_TXURCH(ch); 239 dev_err(dev->dev, 240 "TX underrun on channel %d\n", ch); 241 } 242 } 243 regmap_write(dev->regmap, ATMEL_I2SC_SCR, mask); 244 ret = IRQ_HANDLED; 245 } 246 247 return ret; 248 } 249 250 #define ATMEL_I2S_RATES SNDRV_PCM_RATE_8000_192000 251 252 #define ATMEL_I2S_FORMATS (SNDRV_PCM_FMTBIT_S8 | \ 253 SNDRV_PCM_FMTBIT_S16_LE | \ 254 SNDRV_PCM_FMTBIT_S18_3LE | \ 255 SNDRV_PCM_FMTBIT_S20_3LE | \ 256 SNDRV_PCM_FMTBIT_S24_3LE | \ 257 SNDRV_PCM_FMTBIT_S24_LE | \ 258 SNDRV_PCM_FMTBIT_S32_LE) 259 260 static int atmel_i2s_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt) 261 { 262 struct atmel_i2s_dev *dev = snd_soc_dai_get_drvdata(dai); 263 264 dev->fmt = fmt; 265 return 0; 266 } 267 268 static int atmel_i2s_prepare(struct snd_pcm_substream *substream, 269 struct snd_soc_dai *dai) 270 { 271 struct atmel_i2s_dev *dev = snd_soc_dai_get_drvdata(dai); 272 bool is_playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK); 273 unsigned int rhr, sr = 0; 274 275 if (is_playback) { 276 regmap_read(dev->regmap, ATMEL_I2SC_SR, &sr); 277 if (sr & ATMEL_I2SC_SR_RXRDY) { 278 /* 279 * The RX Ready flag should not be set. However if here, 280 * we flush (read) the Receive Holding Register to start 281 * from a clean state. 282 */ 283 dev_dbg(dev->dev, "RXRDY is set\n"); 284 regmap_read(dev->regmap, ATMEL_I2SC_RHR, &rhr); 285 } 286 } 287 288 return 0; 289 } 290 291 static int atmel_i2s_get_gck_param(struct atmel_i2s_dev *dev, int fs) 292 { 293 int i, best; 294 295 if (!dev->gclk) { 296 dev_err(dev->dev, "cannot generate the I2S Master Clock\n"); 297 return -EINVAL; 298 } 299 300 /* 301 * Find the best possible settings to generate the I2S Master Clock 302 * from the PLL Audio. 303 */ 304 dev->gck_param = NULL; 305 best = INT_MAX; 306 for (i = 0; i < ARRAY_SIZE(gck_params); ++i) { 307 const struct atmel_i2s_gck_param *gck_param = &gck_params[i]; 308 int val = abs(fs - gck_param->fs); 309 310 if (val < best) { 311 best = val; 312 dev->gck_param = gck_param; 313 } 314 } 315 316 return 0; 317 } 318 319 static int atmel_i2s_hw_params(struct snd_pcm_substream *substream, 320 struct snd_pcm_hw_params *params, 321 struct snd_soc_dai *dai) 322 { 323 struct atmel_i2s_dev *dev = snd_soc_dai_get_drvdata(dai); 324 bool is_playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK); 325 unsigned int mr = 0, mr_mask; 326 int ret; 327 328 mr_mask = ATMEL_I2SC_MR_FORMAT_MASK | ATMEL_I2SC_MR_MODE_MASK | 329 ATMEL_I2SC_MR_DATALENGTH_MASK; 330 if (is_playback) 331 mr_mask |= ATMEL_I2SC_MR_TXMONO; 332 else 333 mr_mask |= ATMEL_I2SC_MR_RXMONO; 334 335 switch (dev->fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 336 case SND_SOC_DAIFMT_I2S: 337 mr |= ATMEL_I2SC_MR_FORMAT_I2S; 338 break; 339 340 default: 341 dev_err(dev->dev, "unsupported bus format\n"); 342 return -EINVAL; 343 } 344 345 switch (dev->fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) { 346 case SND_SOC_DAIFMT_BP_FP: 347 /* codec is slave, so cpu is master */ 348 mr |= ATMEL_I2SC_MR_MODE_MASTER; 349 ret = atmel_i2s_get_gck_param(dev, params_rate(params)); 350 if (ret) 351 return ret; 352 break; 353 354 case SND_SOC_DAIFMT_BC_FC: 355 /* codec is master, so cpu is slave */ 356 mr |= ATMEL_I2SC_MR_MODE_SLAVE; 357 dev->gck_param = NULL; 358 break; 359 360 default: 361 dev_err(dev->dev, "unsupported master/slave mode\n"); 362 return -EINVAL; 363 } 364 365 switch (params_channels(params)) { 366 case 1: 367 if (is_playback) 368 mr |= ATMEL_I2SC_MR_TXMONO; 369 else 370 mr |= ATMEL_I2SC_MR_RXMONO; 371 break; 372 case 2: 373 break; 374 default: 375 dev_err(dev->dev, "unsupported number of audio channels\n"); 376 return -EINVAL; 377 } 378 379 switch (params_format(params)) { 380 case SNDRV_PCM_FORMAT_S8: 381 mr |= ATMEL_I2SC_MR_DATALENGTH_8_BITS; 382 break; 383 384 case SNDRV_PCM_FORMAT_S16_LE: 385 mr |= ATMEL_I2SC_MR_DATALENGTH_16_BITS; 386 break; 387 388 case SNDRV_PCM_FORMAT_S18_3LE: 389 mr |= ATMEL_I2SC_MR_DATALENGTH_18_BITS | ATMEL_I2SC_MR_IWS; 390 break; 391 392 case SNDRV_PCM_FORMAT_S20_3LE: 393 mr |= ATMEL_I2SC_MR_DATALENGTH_20_BITS | ATMEL_I2SC_MR_IWS; 394 break; 395 396 case SNDRV_PCM_FORMAT_S24_3LE: 397 mr |= ATMEL_I2SC_MR_DATALENGTH_24_BITS | ATMEL_I2SC_MR_IWS; 398 break; 399 400 case SNDRV_PCM_FORMAT_S24_LE: 401 mr |= ATMEL_I2SC_MR_DATALENGTH_24_BITS; 402 break; 403 404 case SNDRV_PCM_FORMAT_S32_LE: 405 mr |= ATMEL_I2SC_MR_DATALENGTH_32_BITS; 406 break; 407 408 default: 409 dev_err(dev->dev, "unsupported size/endianness for audio samples\n"); 410 return -EINVAL; 411 } 412 413 return regmap_update_bits(dev->regmap, ATMEL_I2SC_MR, mr_mask, mr); 414 } 415 416 static int atmel_i2s_switch_mck_generator(struct atmel_i2s_dev *dev, 417 bool enabled) 418 { 419 unsigned int mr, mr_mask; 420 unsigned long gclk_rate; 421 int ret; 422 423 mr = 0; 424 mr_mask = (ATMEL_I2SC_MR_IMCKDIV_MASK | 425 ATMEL_I2SC_MR_IMCKFS_MASK | 426 ATMEL_I2SC_MR_IMCKMODE_MASK); 427 428 if (!enabled) { 429 /* Disable the I2S Master Clock generator. */ 430 ret = regmap_write(dev->regmap, ATMEL_I2SC_CR, 431 ATMEL_I2SC_CR_CKDIS); 432 if (ret) 433 return ret; 434 435 /* Reset the I2S Master Clock generator settings. */ 436 ret = regmap_update_bits(dev->regmap, ATMEL_I2SC_MR, 437 mr_mask, mr); 438 if (ret) 439 return ret; 440 441 /* Disable/unprepare the PMC generated clock. */ 442 clk_disable_unprepare(dev->gclk); 443 444 return 0; 445 } 446 447 if (!dev->gck_param) 448 return -EINVAL; 449 450 gclk_rate = dev->gck_param->mck * (dev->gck_param->imckdiv + 1); 451 452 ret = clk_set_rate(dev->gclk, gclk_rate); 453 if (ret) 454 return ret; 455 456 ret = clk_prepare_enable(dev->gclk); 457 if (ret) 458 return ret; 459 460 /* Update the Mode Register to generate the I2S Master Clock. */ 461 mr |= ATMEL_I2SC_MR_IMCKDIV(dev->gck_param->imckdiv); 462 mr |= ATMEL_I2SC_MR_IMCKFS(dev->gck_param->imckfs); 463 mr |= ATMEL_I2SC_MR_IMCKMODE_I2SMCK; 464 ret = regmap_update_bits(dev->regmap, ATMEL_I2SC_MR, mr_mask, mr); 465 if (ret) 466 return ret; 467 468 /* Finally enable the I2S Master Clock generator. */ 469 return regmap_write(dev->regmap, ATMEL_I2SC_CR, 470 ATMEL_I2SC_CR_CKEN); 471 } 472 473 static int atmel_i2s_trigger(struct snd_pcm_substream *substream, int cmd, 474 struct snd_soc_dai *dai) 475 { 476 struct atmel_i2s_dev *dev = snd_soc_dai_get_drvdata(dai); 477 bool is_playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK); 478 bool is_master, mck_enabled; 479 unsigned int cr, mr; 480 int err; 481 482 switch (cmd) { 483 case SNDRV_PCM_TRIGGER_START: 484 case SNDRV_PCM_TRIGGER_RESUME: 485 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 486 cr = is_playback ? ATMEL_I2SC_CR_TXEN : ATMEL_I2SC_CR_RXEN; 487 mck_enabled = true; 488 break; 489 case SNDRV_PCM_TRIGGER_STOP: 490 case SNDRV_PCM_TRIGGER_SUSPEND: 491 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 492 cr = is_playback ? ATMEL_I2SC_CR_TXDIS : ATMEL_I2SC_CR_RXDIS; 493 mck_enabled = false; 494 break; 495 default: 496 return -EINVAL; 497 } 498 499 /* Read the Mode Register to retrieve the master/slave state. */ 500 err = regmap_read(dev->regmap, ATMEL_I2SC_MR, &mr); 501 if (err) 502 return err; 503 is_master = (mr & ATMEL_I2SC_MR_MODE_MASK) == ATMEL_I2SC_MR_MODE_MASTER; 504 505 /* If master starts, enable the audio clock. */ 506 if (is_master && mck_enabled) { 507 if (!dev->clk_use_no) { 508 err = atmel_i2s_switch_mck_generator(dev, true); 509 if (err) 510 return err; 511 } 512 dev->clk_use_no++; 513 } 514 515 err = regmap_write(dev->regmap, ATMEL_I2SC_CR, cr); 516 if (err) 517 return err; 518 519 /* If master stops, disable the audio clock. */ 520 if (is_master && !mck_enabled) { 521 if (dev->clk_use_no == 1) { 522 err = atmel_i2s_switch_mck_generator(dev, false); 523 if (err) 524 return err; 525 } 526 dev->clk_use_no--; 527 } 528 529 return err; 530 } 531 532 static const struct snd_soc_dai_ops atmel_i2s_dai_ops = { 533 .prepare = atmel_i2s_prepare, 534 .trigger = atmel_i2s_trigger, 535 .hw_params = atmel_i2s_hw_params, 536 .set_fmt = atmel_i2s_set_dai_fmt, 537 }; 538 539 static int atmel_i2s_dai_probe(struct snd_soc_dai *dai) 540 { 541 struct atmel_i2s_dev *dev = snd_soc_dai_get_drvdata(dai); 542 543 snd_soc_dai_init_dma_data(dai, &dev->playback, &dev->capture); 544 return 0; 545 } 546 547 static struct snd_soc_dai_driver atmel_i2s_dai = { 548 .probe = atmel_i2s_dai_probe, 549 .playback = { 550 .channels_min = 1, 551 .channels_max = 2, 552 .rates = ATMEL_I2S_RATES, 553 .formats = ATMEL_I2S_FORMATS, 554 }, 555 .capture = { 556 .channels_min = 1, 557 .channels_max = 2, 558 .rates = ATMEL_I2S_RATES, 559 .formats = ATMEL_I2S_FORMATS, 560 }, 561 .ops = &atmel_i2s_dai_ops, 562 .symmetric_rate = 1, 563 .symmetric_sample_bits = 1, 564 }; 565 566 static const struct snd_soc_component_driver atmel_i2s_component = { 567 .name = "atmel-i2s", 568 .legacy_dai_naming = 1, 569 }; 570 571 static int atmel_i2s_sama5d2_mck_init(struct atmel_i2s_dev *dev, 572 struct device_node *np) 573 { 574 struct clk *muxclk; 575 int err; 576 577 if (!dev->gclk) 578 return 0; 579 580 /* muxclk is optional, so we return error for probe defer only */ 581 muxclk = devm_clk_get(dev->dev, "muxclk"); 582 if (IS_ERR(muxclk)) { 583 err = PTR_ERR(muxclk); 584 if (err == -EPROBE_DEFER) 585 return -EPROBE_DEFER; 586 dev_dbg(dev->dev, 587 "failed to get the I2S clock control: %d\n", err); 588 return 0; 589 } 590 591 return clk_set_parent(muxclk, dev->gclk); 592 } 593 594 static const struct atmel_i2s_caps atmel_i2s_sama5d2_caps = { 595 .mck_init = atmel_i2s_sama5d2_mck_init, 596 }; 597 598 static const struct of_device_id atmel_i2s_dt_ids[] = { 599 { 600 .compatible = "atmel,sama5d2-i2s", 601 .data = (void *)&atmel_i2s_sama5d2_caps, 602 }, 603 604 { /* sentinel */ } 605 }; 606 607 MODULE_DEVICE_TABLE(of, atmel_i2s_dt_ids); 608 609 static int atmel_i2s_probe(struct platform_device *pdev) 610 { 611 struct device_node *np = pdev->dev.of_node; 612 const struct of_device_id *match; 613 struct atmel_i2s_dev *dev; 614 struct resource *mem; 615 struct regmap *regmap; 616 void __iomem *base; 617 int irq; 618 int err; 619 unsigned int pcm_flags = 0; 620 unsigned int version; 621 622 /* Get memory for driver data. */ 623 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL); 624 if (!dev) 625 return -ENOMEM; 626 627 /* Get hardware capabilities. */ 628 match = of_match_node(atmel_i2s_dt_ids, np); 629 if (match) 630 dev->caps = match->data; 631 632 /* Map I/O registers. */ 633 base = devm_platform_get_and_ioremap_resource(pdev, 0, &mem); 634 if (IS_ERR(base)) 635 return PTR_ERR(base); 636 637 regmap = devm_regmap_init_mmio(&pdev->dev, base, 638 &atmel_i2s_regmap_config); 639 if (IS_ERR(regmap)) 640 return PTR_ERR(regmap); 641 642 /* Request IRQ. */ 643 irq = platform_get_irq(pdev, 0); 644 if (irq < 0) 645 return irq; 646 647 err = devm_request_irq(&pdev->dev, irq, atmel_i2s_interrupt, 0, 648 dev_name(&pdev->dev), dev); 649 if (err) 650 return err; 651 652 /* Get the peripheral clock. */ 653 dev->pclk = devm_clk_get(&pdev->dev, "pclk"); 654 if (IS_ERR(dev->pclk)) { 655 err = PTR_ERR(dev->pclk); 656 dev_err(&pdev->dev, 657 "failed to get the peripheral clock: %d\n", err); 658 return err; 659 } 660 661 /* Get audio clock to generate the I2S Master Clock (I2S_MCK) */ 662 dev->gclk = devm_clk_get(&pdev->dev, "gclk"); 663 if (IS_ERR(dev->gclk)) { 664 if (PTR_ERR(dev->gclk) == -EPROBE_DEFER) 665 return -EPROBE_DEFER; 666 /* Master Mode not supported */ 667 dev->gclk = NULL; 668 } 669 dev->dev = &pdev->dev; 670 dev->regmap = regmap; 671 platform_set_drvdata(pdev, dev); 672 673 /* Do hardware specific settings to initialize I2S_MCK generator */ 674 if (dev->caps && dev->caps->mck_init) { 675 err = dev->caps->mck_init(dev, np); 676 if (err) 677 return err; 678 } 679 680 /* Enable the peripheral clock. */ 681 err = clk_prepare_enable(dev->pclk); 682 if (err) 683 return err; 684 685 /* Get IP version. */ 686 regmap_read(dev->regmap, ATMEL_I2SC_VERSION, &version); 687 dev_info(&pdev->dev, "hw version: %#x\n", version); 688 689 /* Enable error interrupts. */ 690 regmap_write(dev->regmap, ATMEL_I2SC_IER, 691 ATMEL_I2SC_INT_RXOR | ATMEL_I2SC_INT_TXUR); 692 693 err = devm_snd_soc_register_component(&pdev->dev, 694 &atmel_i2s_component, 695 &atmel_i2s_dai, 1); 696 if (err) { 697 dev_err(&pdev->dev, "failed to register DAI: %d\n", err); 698 clk_disable_unprepare(dev->pclk); 699 return err; 700 } 701 702 /* Prepare DMA config. */ 703 dev->playback.addr = (dma_addr_t)mem->start + ATMEL_I2SC_THR; 704 dev->playback.maxburst = 1; 705 dev->capture.addr = (dma_addr_t)mem->start + ATMEL_I2SC_RHR; 706 dev->capture.maxburst = 1; 707 708 if (of_property_match_string(np, "dma-names", "rx-tx") == 0) 709 pcm_flags |= SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX; 710 err = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, pcm_flags); 711 if (err) { 712 dev_err(&pdev->dev, "failed to register PCM: %d\n", err); 713 clk_disable_unprepare(dev->pclk); 714 return err; 715 } 716 717 return 0; 718 } 719 720 static void atmel_i2s_remove(struct platform_device *pdev) 721 { 722 struct atmel_i2s_dev *dev = platform_get_drvdata(pdev); 723 724 clk_disable_unprepare(dev->pclk); 725 } 726 727 static struct platform_driver atmel_i2s_driver = { 728 .driver = { 729 .name = "atmel_i2s", 730 .of_match_table = of_match_ptr(atmel_i2s_dt_ids), 731 }, 732 .probe = atmel_i2s_probe, 733 .remove_new = atmel_i2s_remove, 734 }; 735 module_platform_driver(atmel_i2s_driver); 736 737 MODULE_DESCRIPTION("Atmel I2S Controller driver"); 738 MODULE_AUTHOR("Cyrille Pitchen <cyrille.pitchen@atmel.com>"); 739 MODULE_LICENSE("GPL v2"); 740