.. | .. |
---|
56 | 56 | /* Validity Bit Mode */ |
---|
57 | 57 | #define SPDIFRX_MR_VBMODE_MASK GENAMSK(1, 1) |
---|
58 | 58 | #define SPDIFRX_MR_VBMODE_ALWAYS_LOAD \ |
---|
59 | | - (0 << 1) /* Load sample regardles of validity bit value */ |
---|
| 59 | + (0 << 1) /* Load sample regardless of validity bit value */ |
---|
60 | 60 | #define SPDIFRX_MR_VBMODE_DISCARD_IF_VB1 \ |
---|
61 | 61 | (1 << 1) /* Load sample only if validity bit is 0 */ |
---|
62 | 62 | |
---|
.. | .. |
---|
217 | 217 | struct mchp_spdifrx_user_data { |
---|
218 | 218 | unsigned char data[SPDIFRX_UD_BITS / 8]; |
---|
219 | 219 | struct completion done; |
---|
220 | | - spinlock_t lock; /* protect access to user data */ |
---|
221 | 220 | }; |
---|
222 | 221 | |
---|
223 | 222 | struct mchp_spdifrx_mixer_control { |
---|
.. | .. |
---|
231 | 230 | struct mchp_spdifrx_dev { |
---|
232 | 231 | struct snd_dmaengine_dai_dma_data capture; |
---|
233 | 232 | struct mchp_spdifrx_mixer_control control; |
---|
234 | | - spinlock_t blockend_lock; /* protect access to blockend_refcount */ |
---|
235 | | - int blockend_refcount; |
---|
| 233 | + struct mutex mlock; |
---|
236 | 234 | struct device *dev; |
---|
237 | 235 | struct regmap *regmap; |
---|
238 | 236 | struct clk *pclk; |
---|
239 | 237 | struct clk *gclk; |
---|
240 | 238 | unsigned int fmt; |
---|
| 239 | + unsigned int trigger_enabled; |
---|
241 | 240 | unsigned int gclk_enabled:1; |
---|
242 | 241 | }; |
---|
243 | 242 | |
---|
.. | .. |
---|
275 | 274 | } |
---|
276 | 275 | } |
---|
277 | 276 | |
---|
278 | | -/* called from non-atomic context only */ |
---|
279 | | -static void mchp_spdifrx_isr_blockend_en(struct mchp_spdifrx_dev *dev) |
---|
280 | | -{ |
---|
281 | | - unsigned long flags; |
---|
282 | | - |
---|
283 | | - spin_lock_irqsave(&dev->blockend_lock, flags); |
---|
284 | | - dev->blockend_refcount++; |
---|
285 | | - /* don't enable BLOCKEND interrupt if it's already enabled */ |
---|
286 | | - if (dev->blockend_refcount == 1) |
---|
287 | | - regmap_write(dev->regmap, SPDIFRX_IER, SPDIFRX_IR_BLOCKEND); |
---|
288 | | - spin_unlock_irqrestore(&dev->blockend_lock, flags); |
---|
289 | | -} |
---|
290 | | - |
---|
291 | | -/* called from atomic/non-atomic context */ |
---|
292 | | -static void mchp_spdifrx_isr_blockend_dis(struct mchp_spdifrx_dev *dev) |
---|
293 | | -{ |
---|
294 | | - unsigned long flags; |
---|
295 | | - |
---|
296 | | - spin_lock_irqsave(&dev->blockend_lock, flags); |
---|
297 | | - dev->blockend_refcount--; |
---|
298 | | - /* don't enable BLOCKEND interrupt if it's already enabled */ |
---|
299 | | - if (dev->blockend_refcount == 0) |
---|
300 | | - regmap_write(dev->regmap, SPDIFRX_IDR, SPDIFRX_IR_BLOCKEND); |
---|
301 | | - spin_unlock_irqrestore(&dev->blockend_lock, flags); |
---|
302 | | -} |
---|
303 | | - |
---|
304 | 277 | static irqreturn_t mchp_spdif_interrupt(int irq, void *dev_id) |
---|
305 | 278 | { |
---|
306 | 279 | struct mchp_spdifrx_dev *dev = dev_id; |
---|
307 | 280 | struct mchp_spdifrx_mixer_control *ctrl = &dev->control; |
---|
308 | | - u32 sr, imr, pending, idr = 0; |
---|
| 281 | + u32 sr, imr, pending; |
---|
309 | 282 | irqreturn_t ret = IRQ_NONE; |
---|
310 | 283 | int ch; |
---|
311 | 284 | |
---|
.. | .. |
---|
320 | 293 | |
---|
321 | 294 | if (pending & SPDIFRX_IR_BLOCKEND) { |
---|
322 | 295 | for (ch = 0; ch < SPDIFRX_CHANNELS; ch++) { |
---|
323 | | - spin_lock(&ctrl->user_data[ch].lock); |
---|
324 | 296 | mchp_spdifrx_channel_user_data_read(dev, ch); |
---|
325 | | - spin_unlock(&ctrl->user_data[ch].lock); |
---|
326 | | - |
---|
327 | 297 | complete(&ctrl->user_data[ch].done); |
---|
328 | 298 | } |
---|
329 | | - mchp_spdifrx_isr_blockend_dis(dev); |
---|
| 299 | + regmap_write(dev->regmap, SPDIFRX_IDR, SPDIFRX_IR_BLOCKEND); |
---|
330 | 300 | ret = IRQ_HANDLED; |
---|
331 | 301 | } |
---|
332 | 302 | |
---|
.. | .. |
---|
334 | 304 | if (pending & SPDIFRX_IR_CSC(ch)) { |
---|
335 | 305 | mchp_spdifrx_channel_status_read(dev, ch); |
---|
336 | 306 | complete(&ctrl->ch_stat[ch].done); |
---|
337 | | - idr |= SPDIFRX_IR_CSC(ch); |
---|
| 307 | + regmap_write(dev->regmap, SPDIFRX_IDR, SPDIFRX_IR_CSC(ch)); |
---|
338 | 308 | ret = IRQ_HANDLED; |
---|
339 | 309 | } |
---|
340 | 310 | } |
---|
.. | .. |
---|
344 | 314 | ret = IRQ_HANDLED; |
---|
345 | 315 | } |
---|
346 | 316 | |
---|
347 | | - regmap_write(dev->regmap, SPDIFRX_IDR, idr); |
---|
348 | | - |
---|
349 | 317 | return ret; |
---|
350 | 318 | } |
---|
351 | 319 | |
---|
.. | .. |
---|
353 | 321 | struct snd_soc_dai *dai) |
---|
354 | 322 | { |
---|
355 | 323 | struct mchp_spdifrx_dev *dev = snd_soc_dai_get_drvdata(dai); |
---|
356 | | - u32 mr; |
---|
357 | | - int running; |
---|
358 | | - int ret; |
---|
359 | | - |
---|
360 | | - regmap_read(dev->regmap, SPDIFRX_MR, &mr); |
---|
361 | | - running = !!(mr & SPDIFRX_MR_RXEN_ENABLE); |
---|
| 324 | + int ret = 0; |
---|
362 | 325 | |
---|
363 | 326 | switch (cmd) { |
---|
364 | 327 | case SNDRV_PCM_TRIGGER_START: |
---|
365 | 328 | case SNDRV_PCM_TRIGGER_RESUME: |
---|
366 | 329 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
---|
367 | | - if (!running) { |
---|
368 | | - mr &= ~SPDIFRX_MR_RXEN_MASK; |
---|
369 | | - mr |= SPDIFRX_MR_RXEN_ENABLE; |
---|
370 | | - /* enable overrun interrupts */ |
---|
371 | | - regmap_write(dev->regmap, SPDIFRX_IER, |
---|
372 | | - SPDIFRX_IR_OVERRUN); |
---|
373 | | - } |
---|
| 330 | + mutex_lock(&dev->mlock); |
---|
| 331 | + /* Enable overrun interrupts */ |
---|
| 332 | + regmap_write(dev->regmap, SPDIFRX_IER, SPDIFRX_IR_OVERRUN); |
---|
| 333 | + |
---|
| 334 | + /* Enable receiver. */ |
---|
| 335 | + regmap_update_bits(dev->regmap, SPDIFRX_MR, SPDIFRX_MR_RXEN_MASK, |
---|
| 336 | + SPDIFRX_MR_RXEN_ENABLE); |
---|
| 337 | + dev->trigger_enabled = true; |
---|
| 338 | + mutex_unlock(&dev->mlock); |
---|
374 | 339 | break; |
---|
375 | 340 | case SNDRV_PCM_TRIGGER_STOP: |
---|
376 | 341 | case SNDRV_PCM_TRIGGER_SUSPEND: |
---|
377 | 342 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
---|
378 | | - if (running) { |
---|
379 | | - mr &= ~SPDIFRX_MR_RXEN_MASK; |
---|
380 | | - mr |= SPDIFRX_MR_RXEN_DISABLE; |
---|
381 | | - /* disable overrun interrupts */ |
---|
382 | | - regmap_write(dev->regmap, SPDIFRX_IDR, |
---|
383 | | - SPDIFRX_IR_OVERRUN); |
---|
384 | | - } |
---|
| 343 | + mutex_lock(&dev->mlock); |
---|
| 344 | + /* Disable overrun interrupts */ |
---|
| 345 | + regmap_write(dev->regmap, SPDIFRX_IDR, SPDIFRX_IR_OVERRUN); |
---|
| 346 | + |
---|
| 347 | + /* Disable receiver. */ |
---|
| 348 | + regmap_update_bits(dev->regmap, SPDIFRX_MR, SPDIFRX_MR_RXEN_MASK, |
---|
| 349 | + SPDIFRX_MR_RXEN_DISABLE); |
---|
| 350 | + dev->trigger_enabled = false; |
---|
| 351 | + mutex_unlock(&dev->mlock); |
---|
385 | 352 | break; |
---|
386 | 353 | default: |
---|
387 | | - return -EINVAL; |
---|
| 354 | + ret = -EINVAL; |
---|
388 | 355 | } |
---|
389 | 356 | |
---|
390 | | - ret = regmap_write(dev->regmap, SPDIFRX_MR, mr); |
---|
391 | | - if (ret) { |
---|
392 | | - dev_err(dev->dev, "unable to enable/disable RX: %d\n", ret); |
---|
393 | | - return ret; |
---|
394 | | - } |
---|
395 | | - |
---|
396 | | - return 0; |
---|
| 357 | + return ret; |
---|
397 | 358 | } |
---|
398 | 359 | |
---|
399 | 360 | static int mchp_spdifrx_hw_params(struct snd_pcm_substream *substream, |
---|
.. | .. |
---|
401 | 362 | struct snd_soc_dai *dai) |
---|
402 | 363 | { |
---|
403 | 364 | struct mchp_spdifrx_dev *dev = snd_soc_dai_get_drvdata(dai); |
---|
404 | | - u32 mr; |
---|
| 365 | + u32 mr = 0; |
---|
405 | 366 | int ret; |
---|
406 | 367 | |
---|
407 | 368 | dev_dbg(dev->dev, "%s() rate=%u format=%#x width=%u channels=%u\n", |
---|
.. | .. |
---|
411 | 372 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
---|
412 | 373 | dev_err(dev->dev, "Playback is not supported\n"); |
---|
413 | 374 | return -EINVAL; |
---|
414 | | - } |
---|
415 | | - |
---|
416 | | - regmap_read(dev->regmap, SPDIFRX_MR, &mr); |
---|
417 | | - |
---|
418 | | - if (mr & SPDIFRX_MR_RXEN_ENABLE) { |
---|
419 | | - dev_err(dev->dev, "PCM already running\n"); |
---|
420 | | - return -EBUSY; |
---|
421 | 375 | } |
---|
422 | 376 | |
---|
423 | 377 | if (params_channels(params) != SPDIFRX_CHANNELS) { |
---|
.. | .. |
---|
445 | 399 | return -EINVAL; |
---|
446 | 400 | } |
---|
447 | 401 | |
---|
| 402 | + mutex_lock(&dev->mlock); |
---|
| 403 | + if (dev->trigger_enabled) { |
---|
| 404 | + dev_err(dev->dev, "PCM already running\n"); |
---|
| 405 | + ret = -EBUSY; |
---|
| 406 | + goto unlock; |
---|
| 407 | + } |
---|
| 408 | + |
---|
448 | 409 | if (dev->gclk_enabled) { |
---|
449 | 410 | clk_disable_unprepare(dev->gclk); |
---|
450 | 411 | dev->gclk_enabled = 0; |
---|
.. | .. |
---|
455 | 416 | dev_err(dev->dev, |
---|
456 | 417 | "unable to set gclk min rate: rate %u * ratio %u + 1\n", |
---|
457 | 418 | params_rate(params), SPDIFRX_GCLK_RATIO_MIN); |
---|
458 | | - return ret; |
---|
| 419 | + goto unlock; |
---|
459 | 420 | } |
---|
460 | 421 | ret = clk_prepare_enable(dev->gclk); |
---|
461 | 422 | if (ret) { |
---|
462 | 423 | dev_err(dev->dev, "unable to enable gclk: %d\n", ret); |
---|
463 | | - return ret; |
---|
| 424 | + goto unlock; |
---|
464 | 425 | } |
---|
465 | 426 | dev->gclk_enabled = 1; |
---|
466 | 427 | |
---|
467 | 428 | dev_dbg(dev->dev, "GCLK range min set to %d\n", |
---|
468 | 429 | params_rate(params) * SPDIFRX_GCLK_RATIO_MIN + 1); |
---|
469 | 430 | |
---|
470 | | - return regmap_write(dev->regmap, SPDIFRX_MR, mr); |
---|
| 431 | + ret = regmap_write(dev->regmap, SPDIFRX_MR, mr); |
---|
| 432 | + |
---|
| 433 | +unlock: |
---|
| 434 | + mutex_unlock(&dev->mlock); |
---|
| 435 | + |
---|
| 436 | + return ret; |
---|
471 | 437 | } |
---|
472 | 438 | |
---|
473 | 439 | static int mchp_spdifrx_hw_free(struct snd_pcm_substream *substream, |
---|
.. | .. |
---|
475 | 441 | { |
---|
476 | 442 | struct mchp_spdifrx_dev *dev = snd_soc_dai_get_drvdata(dai); |
---|
477 | 443 | |
---|
| 444 | + mutex_lock(&dev->mlock); |
---|
478 | 445 | if (dev->gclk_enabled) { |
---|
479 | 446 | clk_disable_unprepare(dev->gclk); |
---|
480 | 447 | dev->gclk_enabled = 0; |
---|
481 | 448 | } |
---|
| 449 | + mutex_unlock(&dev->mlock); |
---|
482 | 450 | return 0; |
---|
483 | 451 | } |
---|
484 | 452 | |
---|
.. | .. |
---|
515 | 483 | { |
---|
516 | 484 | struct mchp_spdifrx_mixer_control *ctrl = &dev->control; |
---|
517 | 485 | struct mchp_spdifrx_ch_stat *ch_stat = &ctrl->ch_stat[channel]; |
---|
518 | | - int ret; |
---|
| 486 | + int ret = 0; |
---|
519 | 487 | |
---|
520 | | - regmap_write(dev->regmap, SPDIFRX_IER, SPDIFRX_IR_CSC(channel)); |
---|
521 | | - /* check for new data available */ |
---|
522 | | - ret = wait_for_completion_interruptible_timeout(&ch_stat->done, |
---|
523 | | - msecs_to_jiffies(100)); |
---|
524 | | - /* IP might not be started or valid stream might not be prezent */ |
---|
525 | | - if (ret < 0) { |
---|
526 | | - dev_dbg(dev->dev, "channel status for channel %d timeout\n", |
---|
527 | | - channel); |
---|
| 488 | + mutex_lock(&dev->mlock); |
---|
| 489 | + |
---|
| 490 | + /* |
---|
| 491 | + * We may reach this point with both clocks enabled but the receiver |
---|
| 492 | + * still disabled. To void waiting for completion and return with |
---|
| 493 | + * timeout check the dev->trigger_enabled. |
---|
| 494 | + * |
---|
| 495 | + * To retrieve data: |
---|
| 496 | + * - if the receiver is enabled CSC IRQ will update the data in software |
---|
| 497 | + * caches (ch_stat->data) |
---|
| 498 | + * - otherwise we just update it here the software caches with latest |
---|
| 499 | + * available information and return it; in this case we don't need |
---|
| 500 | + * spin locking as the IRQ is disabled and will not be raised from |
---|
| 501 | + * anywhere else. |
---|
| 502 | + */ |
---|
| 503 | + |
---|
| 504 | + if (dev->trigger_enabled) { |
---|
| 505 | + reinit_completion(&ch_stat->done); |
---|
| 506 | + regmap_write(dev->regmap, SPDIFRX_IER, SPDIFRX_IR_CSC(channel)); |
---|
| 507 | + /* Check for new data available */ |
---|
| 508 | + ret = wait_for_completion_interruptible_timeout(&ch_stat->done, |
---|
| 509 | + msecs_to_jiffies(100)); |
---|
| 510 | + /* Valid stream might not be present */ |
---|
| 511 | + if (ret <= 0) { |
---|
| 512 | + dev_dbg(dev->dev, "channel status for channel %d timeout\n", |
---|
| 513 | + channel); |
---|
| 514 | + regmap_write(dev->regmap, SPDIFRX_IDR, SPDIFRX_IR_CSC(channel)); |
---|
| 515 | + ret = ret ? : -ETIMEDOUT; |
---|
| 516 | + goto unlock; |
---|
| 517 | + } else { |
---|
| 518 | + ret = 0; |
---|
| 519 | + } |
---|
| 520 | + } else { |
---|
| 521 | + /* Update software cache with latest channel status. */ |
---|
| 522 | + mchp_spdifrx_channel_status_read(dev, channel); |
---|
528 | 523 | } |
---|
529 | 524 | |
---|
530 | 525 | memcpy(uvalue->value.iec958.status, ch_stat->data, |
---|
531 | 526 | sizeof(ch_stat->data)); |
---|
532 | 527 | |
---|
533 | | - return 0; |
---|
| 528 | +unlock: |
---|
| 529 | + mutex_unlock(&dev->mlock); |
---|
| 530 | + return ret; |
---|
534 | 531 | } |
---|
535 | 532 | |
---|
536 | 533 | static int mchp_spdifrx_cs1_get(struct snd_kcontrol *kcontrol, |
---|
.. | .. |
---|
564 | 561 | int channel, |
---|
565 | 562 | struct snd_ctl_elem_value *uvalue) |
---|
566 | 563 | { |
---|
567 | | - unsigned long flags; |
---|
568 | 564 | struct mchp_spdifrx_mixer_control *ctrl = &dev->control; |
---|
569 | 565 | struct mchp_spdifrx_user_data *user_data = &ctrl->user_data[channel]; |
---|
570 | | - int ret; |
---|
| 566 | + int ret = 0; |
---|
571 | 567 | |
---|
572 | | - reinit_completion(&user_data->done); |
---|
573 | | - mchp_spdifrx_isr_blockend_en(dev); |
---|
574 | | - ret = wait_for_completion_interruptible_timeout(&user_data->done, |
---|
575 | | - msecs_to_jiffies(100)); |
---|
576 | | - /* IP might not be started or valid stream might not be prezent */ |
---|
577 | | - if (ret <= 0) { |
---|
578 | | - dev_dbg(dev->dev, "user data for channel %d timeout\n", |
---|
579 | | - channel); |
---|
580 | | - mchp_spdifrx_isr_blockend_dis(dev); |
---|
581 | | - return ret; |
---|
| 568 | + mutex_lock(&dev->mlock); |
---|
| 569 | + |
---|
| 570 | + /* |
---|
| 571 | + * We may reach this point with both clocks enabled but the receiver |
---|
| 572 | + * still disabled. To void waiting for completion to just timeout we |
---|
| 573 | + * check here the dev->trigger_enabled flag. |
---|
| 574 | + * |
---|
| 575 | + * To retrieve data: |
---|
| 576 | + * - if the receiver is enabled we need to wait for blockend IRQ to read |
---|
| 577 | + * data to and update it for us in software caches |
---|
| 578 | + * - otherwise reading the SPDIFRX_CHUD() registers is enough. |
---|
| 579 | + */ |
---|
| 580 | + |
---|
| 581 | + if (dev->trigger_enabled) { |
---|
| 582 | + reinit_completion(&user_data->done); |
---|
| 583 | + regmap_write(dev->regmap, SPDIFRX_IER, SPDIFRX_IR_BLOCKEND); |
---|
| 584 | + ret = wait_for_completion_interruptible_timeout(&user_data->done, |
---|
| 585 | + msecs_to_jiffies(100)); |
---|
| 586 | + /* Valid stream might not be present. */ |
---|
| 587 | + if (ret <= 0) { |
---|
| 588 | + dev_dbg(dev->dev, "user data for channel %d timeout\n", |
---|
| 589 | + channel); |
---|
| 590 | + regmap_write(dev->regmap, SPDIFRX_IDR, SPDIFRX_IR_BLOCKEND); |
---|
| 591 | + ret = ret ? : -ETIMEDOUT; |
---|
| 592 | + goto unlock; |
---|
| 593 | + } else { |
---|
| 594 | + ret = 0; |
---|
| 595 | + } |
---|
| 596 | + } else { |
---|
| 597 | + /* Update software cache with last available data. */ |
---|
| 598 | + mchp_spdifrx_channel_user_data_read(dev, channel); |
---|
582 | 599 | } |
---|
583 | 600 | |
---|
584 | | - spin_lock_irqsave(&user_data->lock, flags); |
---|
585 | 601 | memcpy(uvalue->value.iec958.subcode, user_data->data, |
---|
586 | 602 | sizeof(user_data->data)); |
---|
587 | | - spin_unlock_irqrestore(&user_data->lock, flags); |
---|
588 | 603 | |
---|
589 | | - return 0; |
---|
| 604 | +unlock: |
---|
| 605 | + mutex_unlock(&dev->mlock); |
---|
| 606 | + return ret; |
---|
590 | 607 | } |
---|
591 | 608 | |
---|
592 | 609 | static int mchp_spdifrx_subcode_ch1_get(struct snd_kcontrol *kcontrol, |
---|
.. | .. |
---|
627 | 644 | u32 val; |
---|
628 | 645 | bool ulock_old = ctrl->ulock; |
---|
629 | 646 | |
---|
630 | | - regmap_read(dev->regmap, SPDIFRX_RSR, &val); |
---|
631 | | - ctrl->ulock = !(val & SPDIFRX_RSR_ULOCK); |
---|
| 647 | + mutex_lock(&dev->mlock); |
---|
| 648 | + |
---|
| 649 | + /* |
---|
| 650 | + * The RSR.ULOCK has wrong value if both pclk and gclk are enabled |
---|
| 651 | + * and the receiver is disabled. Thus we take into account the |
---|
| 652 | + * dev->trigger_enabled here to return a real status. |
---|
| 653 | + */ |
---|
| 654 | + if (dev->trigger_enabled) { |
---|
| 655 | + regmap_read(dev->regmap, SPDIFRX_RSR, &val); |
---|
| 656 | + ctrl->ulock = !(val & SPDIFRX_RSR_ULOCK); |
---|
| 657 | + } else { |
---|
| 658 | + ctrl->ulock = 0; |
---|
| 659 | + } |
---|
| 660 | + |
---|
632 | 661 | uvalue->value.integer.value[0] = ctrl->ulock; |
---|
| 662 | + |
---|
| 663 | + mutex_unlock(&dev->mlock); |
---|
633 | 664 | |
---|
634 | 665 | return ulock_old != ctrl->ulock; |
---|
635 | 666 | } |
---|
.. | .. |
---|
643 | 674 | u32 val; |
---|
644 | 675 | bool badf_old = ctrl->badf; |
---|
645 | 676 | |
---|
646 | | - regmap_read(dev->regmap, SPDIFRX_RSR, &val); |
---|
647 | | - ctrl->badf = !!(val & SPDIFRX_RSR_BADF); |
---|
| 677 | + mutex_lock(&dev->mlock); |
---|
| 678 | + |
---|
| 679 | + /* |
---|
| 680 | + * The RSR.ULOCK has wrong value if both pclk and gclk are enabled |
---|
| 681 | + * and the receiver is disabled. Thus we take into account the |
---|
| 682 | + * dev->trigger_enabled here to return a real status. |
---|
| 683 | + */ |
---|
| 684 | + if (dev->trigger_enabled) { |
---|
| 685 | + regmap_read(dev->regmap, SPDIFRX_RSR, &val); |
---|
| 686 | + ctrl->badf = !!(val & SPDIFRX_RSR_BADF); |
---|
| 687 | + } else { |
---|
| 688 | + ctrl->badf = 0; |
---|
| 689 | + } |
---|
| 690 | + |
---|
| 691 | + mutex_unlock(&dev->mlock); |
---|
| 692 | + |
---|
648 | 693 | uvalue->value.integer.value[0] = ctrl->badf; |
---|
649 | 694 | |
---|
650 | 695 | return badf_old != ctrl->badf; |
---|
.. | .. |
---|
656 | 701 | struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol); |
---|
657 | 702 | struct mchp_spdifrx_dev *dev = snd_soc_dai_get_drvdata(dai); |
---|
658 | 703 | struct mchp_spdifrx_mixer_control *ctrl = &dev->control; |
---|
659 | | - u32 val; |
---|
| 704 | + u32 val = ~0U, loops = 10; |
---|
| 705 | + int ret; |
---|
660 | 706 | bool signal_old = ctrl->signal; |
---|
661 | 707 | |
---|
662 | | - regmap_read(dev->regmap, SPDIFRX_RSR, &val); |
---|
663 | | - ctrl->signal = !(val & SPDIFRX_RSR_NOSIGNAL); |
---|
| 708 | + mutex_lock(&dev->mlock); |
---|
| 709 | + |
---|
| 710 | + /* |
---|
| 711 | + * To get the signal we need to have receiver enabled. This |
---|
| 712 | + * could be enabled also from trigger() function thus we need to |
---|
| 713 | + * take care of not disabling the receiver when it runs. |
---|
| 714 | + */ |
---|
| 715 | + if (!dev->trigger_enabled) { |
---|
| 716 | + ret = clk_prepare_enable(dev->gclk); |
---|
| 717 | + if (ret) |
---|
| 718 | + goto unlock; |
---|
| 719 | + |
---|
| 720 | + regmap_update_bits(dev->regmap, SPDIFRX_MR, SPDIFRX_MR_RXEN_MASK, |
---|
| 721 | + SPDIFRX_MR_RXEN_ENABLE); |
---|
| 722 | + |
---|
| 723 | + /* Wait for RSR.ULOCK bit. */ |
---|
| 724 | + while (--loops) { |
---|
| 725 | + regmap_read(dev->regmap, SPDIFRX_RSR, &val); |
---|
| 726 | + if (!(val & SPDIFRX_RSR_ULOCK)) |
---|
| 727 | + break; |
---|
| 728 | + usleep_range(100, 150); |
---|
| 729 | + } |
---|
| 730 | + |
---|
| 731 | + regmap_update_bits(dev->regmap, SPDIFRX_MR, SPDIFRX_MR_RXEN_MASK, |
---|
| 732 | + SPDIFRX_MR_RXEN_DISABLE); |
---|
| 733 | + |
---|
| 734 | + clk_disable_unprepare(dev->gclk); |
---|
| 735 | + } else { |
---|
| 736 | + regmap_read(dev->regmap, SPDIFRX_RSR, &val); |
---|
| 737 | + } |
---|
| 738 | + |
---|
| 739 | +unlock: |
---|
| 740 | + mutex_unlock(&dev->mlock); |
---|
| 741 | + |
---|
| 742 | + if (!(val & SPDIFRX_RSR_ULOCK)) |
---|
| 743 | + ctrl->signal = !(val & SPDIFRX_RSR_NOSIGNAL); |
---|
| 744 | + else |
---|
| 745 | + ctrl->signal = 0; |
---|
664 | 746 | uvalue->value.integer.value[0] = ctrl->signal; |
---|
665 | 747 | |
---|
666 | 748 | return signal_old != ctrl->signal; |
---|
.. | .. |
---|
685 | 767 | u32 val; |
---|
686 | 768 | int rate; |
---|
687 | 769 | |
---|
688 | | - regmap_read(dev->regmap, SPDIFRX_RSR, &val); |
---|
| 770 | + mutex_lock(&dev->mlock); |
---|
689 | 771 | |
---|
690 | | - /* if the receiver is not locked, ISF data is invalid */ |
---|
691 | | - if (val & SPDIFRX_RSR_ULOCK || !(val & SPDIFRX_RSR_IFS_MASK)) { |
---|
| 772 | + /* |
---|
| 773 | + * The RSR.ULOCK has wrong value if both pclk and gclk are enabled |
---|
| 774 | + * and the receiver is disabled. Thus we take into account the |
---|
| 775 | + * dev->trigger_enabled here to return a real status. |
---|
| 776 | + */ |
---|
| 777 | + if (dev->trigger_enabled) { |
---|
| 778 | + regmap_read(dev->regmap, SPDIFRX_RSR, &val); |
---|
| 779 | + /* If the receiver is not locked, ISF data is invalid. */ |
---|
| 780 | + if (val & SPDIFRX_RSR_ULOCK || !(val & SPDIFRX_RSR_IFS_MASK)) { |
---|
| 781 | + ucontrol->value.integer.value[0] = 0; |
---|
| 782 | + goto unlock; |
---|
| 783 | + } |
---|
| 784 | + } else { |
---|
| 785 | + /* Reveicer is not locked, IFS data is invalid. */ |
---|
692 | 786 | ucontrol->value.integer.value[0] = 0; |
---|
693 | | - return 0; |
---|
| 787 | + goto unlock; |
---|
694 | 788 | } |
---|
695 | 789 | |
---|
696 | 790 | rate = clk_get_rate(dev->gclk); |
---|
697 | 791 | |
---|
698 | 792 | ucontrol->value.integer.value[0] = rate / (32 * SPDIFRX_RSR_IFS(val)); |
---|
699 | 793 | |
---|
| 794 | +unlock: |
---|
| 795 | + mutex_unlock(&dev->mlock); |
---|
700 | 796 | return 0; |
---|
701 | 797 | } |
---|
702 | 798 | |
---|
.. | .. |
---|
808 | 904 | SPDIFRX_MR_AUTORST_NOACTION | |
---|
809 | 905 | SPDIFRX_MR_PACK_DISABLED); |
---|
810 | 906 | |
---|
811 | | - dev->blockend_refcount = 0; |
---|
812 | 907 | for (ch = 0; ch < SPDIFRX_CHANNELS; ch++) { |
---|
813 | 908 | init_completion(&ctrl->ch_stat[ch].done); |
---|
814 | 909 | init_completion(&ctrl->user_data[ch].done); |
---|
815 | | - spin_lock_init(&ctrl->user_data[ch].lock); |
---|
816 | 910 | } |
---|
817 | 911 | |
---|
818 | 912 | /* Add controls */ |
---|
.. | .. |
---|
827 | 921 | struct mchp_spdifrx_dev *dev = snd_soc_dai_get_drvdata(dai); |
---|
828 | 922 | |
---|
829 | 923 | /* Disable interrupts */ |
---|
830 | | - regmap_write(dev->regmap, SPDIFRX_IDR, 0xFF); |
---|
| 924 | + regmap_write(dev->regmap, SPDIFRX_IDR, GENMASK(14, 0)); |
---|
831 | 925 | |
---|
832 | 926 | clk_disable_unprepare(dev->pclk); |
---|
833 | 927 | |
---|
.. | .. |
---|
912 | 1006 | "failed to get the PMC generated clock: %d\n", err); |
---|
913 | 1007 | return err; |
---|
914 | 1008 | } |
---|
915 | | - spin_lock_init(&dev->blockend_lock); |
---|
| 1009 | + |
---|
| 1010 | + /* |
---|
| 1011 | + * Signal control need a valid rate on gclk. hw_params() configures |
---|
| 1012 | + * it propertly but requesting signal before any hw_params() has been |
---|
| 1013 | + * called lead to invalid value returned for signal. Thus, configure |
---|
| 1014 | + * gclk at a valid rate, here, in initialization, to simplify the |
---|
| 1015 | + * control path. |
---|
| 1016 | + */ |
---|
| 1017 | + clk_set_min_rate(dev->gclk, 48000 * SPDIFRX_GCLK_RATIO_MIN + 1); |
---|
| 1018 | + |
---|
| 1019 | + mutex_init(&dev->mlock); |
---|
916 | 1020 | |
---|
917 | 1021 | dev->dev = &pdev->dev; |
---|
918 | 1022 | dev->regmap = regmap; |
---|