| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * es8316.c -- es8316 ALSA SoC audio driver |
|---|
| 3 | 4 | * Copyright Everest Semiconductor Co.,Ltd |
|---|
| 4 | 5 | * |
|---|
| 5 | 6 | * Authors: David Yang <yangxiaohua@everest-semi.com>, |
|---|
| 6 | 7 | * Daniel Drake <drake@endlessm.com> |
|---|
| 7 | | - * |
|---|
| 8 | | - * This program is free software; you can redistribute it and/or modify |
|---|
| 9 | | - * it under the terms of the GNU General Public License version 2 as |
|---|
| 10 | | - * published by the Free Software Foundation. |
|---|
| 11 | 8 | */ |
|---|
| 12 | 9 | |
|---|
| 13 | 10 | #include <linux/module.h> |
|---|
| 14 | 11 | #include <linux/acpi.h> |
|---|
| 15 | 12 | #include <linux/clk.h> |
|---|
| 16 | 13 | #include <linux/delay.h> |
|---|
| 17 | | -#include <linux/extcon.h> |
|---|
| 18 | | -#include <linux/gpio.h> |
|---|
| 19 | 14 | #include <linux/i2c.h> |
|---|
| 20 | 15 | #include <linux/mod_devicetable.h> |
|---|
| 21 | | -#include <linux/of_gpio.h> |
|---|
| 16 | +#include <linux/mutex.h> |
|---|
| 22 | 17 | #include <linux/regmap.h> |
|---|
| 23 | 18 | #include <sound/pcm.h> |
|---|
| 24 | 19 | #include <sound/pcm_params.h> |
|---|
| 25 | 20 | #include <sound/soc.h> |
|---|
| 26 | 21 | #include <sound/soc-dapm.h> |
|---|
| 27 | 22 | #include <sound/tlv.h> |
|---|
| 23 | +#include <sound/jack.h> |
|---|
| 28 | 24 | #include "es8316.h" |
|---|
| 29 | 25 | |
|---|
| 30 | | -#define ES8316_EXTCON_ID EXTCON_JACK_HEADPHONE |
|---|
| 31 | 26 | /* In slave mode at single speed, the codec is documented as accepting 5 |
|---|
| 32 | 27 | * MCLK/LRCK ratios, but we also add ratio 400, which is commonly used on |
|---|
| 33 | 28 | * Intel Cherry Trail platforms (19.2MHz MCLK, 48kHz LRCK). |
|---|
| .. | .. |
|---|
| 38 | 33 | }; |
|---|
| 39 | 34 | |
|---|
| 40 | 35 | struct es8316_priv { |
|---|
| 36 | + struct mutex lock; |
|---|
| 37 | + struct clk *mclk; |
|---|
| 38 | + struct regmap *regmap; |
|---|
| 39 | + struct snd_soc_component *component; |
|---|
| 40 | + struct snd_soc_jack *jack; |
|---|
| 41 | + int irq; |
|---|
| 41 | 42 | unsigned int sysclk; |
|---|
| 42 | 43 | unsigned int allowed_rates[NR_SUPPORTED_MCLK_LRCK_RATIOS]; |
|---|
| 43 | 44 | struct snd_pcm_hw_constraint_list sysclk_constraints; |
|---|
| 44 | | - struct clk *mclk; |
|---|
| 45 | | - |
|---|
| 46 | | - struct gpio_desc *gpiod_spk_ctl; |
|---|
| 47 | | - bool muted; |
|---|
| 48 | | - bool hp_inserted; |
|---|
| 49 | | - struct notifier_block extcon_nb; |
|---|
| 50 | | - int pwr_count; |
|---|
| 45 | + bool jd_inverted; |
|---|
| 51 | 46 | }; |
|---|
| 52 | 47 | |
|---|
| 53 | 48 | /* |
|---|
| .. | .. |
|---|
| 57 | 52 | static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(adc_vol_tlv, -9600, 50, 1); |
|---|
| 58 | 53 | static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(alc_max_gain_tlv, -650, 150, 0); |
|---|
| 59 | 54 | static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(alc_min_gain_tlv, -1200, 150, 0); |
|---|
| 60 | | -static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(alc_target_tlv, -1650, 150, 0); |
|---|
| 55 | + |
|---|
| 56 | +static const SNDRV_CTL_TLVD_DECLARE_DB_RANGE(alc_target_tlv, |
|---|
| 57 | + 0, 10, TLV_DB_SCALE_ITEM(-1650, 150, 0), |
|---|
| 58 | + 11, 11, TLV_DB_SCALE_ITEM(-150, 0, 0), |
|---|
| 59 | +); |
|---|
| 60 | + |
|---|
| 61 | 61 | static const SNDRV_CTL_TLVD_DECLARE_DB_RANGE(hpmixer_gain_tlv, |
|---|
| 62 | 62 | 0, 4, TLV_DB_SCALE_ITEM(-1200, 150, 0), |
|---|
| 63 | 63 | 8, 11, TLV_DB_SCALE_ITEM(-450, 150, 0), |
|---|
| .. | .. |
|---|
| 94 | 94 | SOC_DOUBLE_TLV("Headphone Playback Volume", ES8316_CPHP_ICAL_VOL, |
|---|
| 95 | 95 | 4, 0, 3, 1, hpout_vol_tlv), |
|---|
| 96 | 96 | SOC_DOUBLE_TLV("Headphone Mixer Volume", ES8316_HPMIX_VOL, |
|---|
| 97 | | - 0, 4, 11, 0, hpmixer_gain_tlv), |
|---|
| 97 | + 4, 0, 11, 0, hpmixer_gain_tlv), |
|---|
| 98 | 98 | |
|---|
| 99 | 99 | SOC_ENUM("Playback Polarity", dacpol), |
|---|
| 100 | 100 | SOC_DOUBLE_R_TLV("DAC Playback Volume", ES8316_DAC_VOLL, |
|---|
| .. | .. |
|---|
| 104 | 104 | SOC_SINGLE("DAC Notch Filter Switch", ES8316_DAC_SET2, 6, 1, 0), |
|---|
| 105 | 105 | SOC_SINGLE("DAC Double Fs Switch", ES8316_DAC_SET2, 7, 1, 0), |
|---|
| 106 | 106 | SOC_SINGLE("DAC Stereo Enhancement", ES8316_DAC_SET3, 0, 7, 0), |
|---|
| 107 | + SOC_SINGLE("DAC Mono Mix Switch", ES8316_DAC_SET3, 3, 1, 0), |
|---|
| 107 | 108 | |
|---|
| 108 | 109 | SOC_ENUM("Capture Polarity", adcpol), |
|---|
| 109 | 110 | SOC_SINGLE("Mic Boost Switch", ES8316_ADC_D2SEPGA, 0, 1, 0), |
|---|
| .. | .. |
|---|
| 119 | 120 | alc_max_gain_tlv), |
|---|
| 120 | 121 | SOC_SINGLE_TLV("ALC Capture Min Volume", ES8316_ADC_ALC2, 0, 28, 0, |
|---|
| 121 | 122 | alc_min_gain_tlv), |
|---|
| 122 | | - SOC_SINGLE_TLV("ALC Capture Target Volume", ES8316_ADC_ALC3, 4, 10, 0, |
|---|
| 123 | + SOC_SINGLE_TLV("ALC Capture Target Volume", ES8316_ADC_ALC3, 4, 11, 0, |
|---|
| 123 | 124 | alc_target_tlv), |
|---|
| 124 | 125 | SOC_SINGLE("ALC Capture Hold Time", ES8316_ADC_ALC3, 0, 10, 0), |
|---|
| 125 | 126 | SOC_SINGLE("ALC Capture Decay Time", ES8316_ADC_ALC4, 4, 10, 0), |
|---|
| .. | .. |
|---|
| 152 | 153 | "dmic data at high level", |
|---|
| 153 | 154 | "dmic data at low level", |
|---|
| 154 | 155 | }; |
|---|
| 155 | | -static const unsigned int es8316_dmic_values[] = { 0, 1, 2 }; |
|---|
| 156 | +static const unsigned int es8316_dmic_values[] = { 0, 2, 3 }; |
|---|
| 156 | 157 | static const struct soc_enum es8316_dmic_src_enum = |
|---|
| 157 | 158 | SOC_VALUE_ENUM_SINGLE(ES8316_ADC_DMIC, 0, 3, |
|---|
| 158 | 159 | ARRAY_SIZE(es8316_dmic_txt), |
|---|
| .. | .. |
|---|
| 168 | 169 | "lin-rin with Boost", |
|---|
| 169 | 170 | "lin-rin with Boost and PGA" |
|---|
| 170 | 171 | }; |
|---|
| 171 | | - |
|---|
| 172 | | -static const unsigned int es8316_hpmux_values[] = { 0, 1, 2, 3 }; |
|---|
| 173 | 172 | |
|---|
| 174 | 173 | static SOC_ENUM_SINGLE_DECL(es8316_left_hpmux_enum, ES8316_HPMIX_SEL, |
|---|
| 175 | 174 | 4, es8316_hpmux_texts); |
|---|
| .. | .. |
|---|
| 200 | 199 | "RDATA TO LDAC, RDATA TO RDAC", |
|---|
| 201 | 200 | "RDATA TO LDAC, LDATA TO RDAC", |
|---|
| 202 | 201 | }; |
|---|
| 203 | | - |
|---|
| 204 | | -static const unsigned int es8316_dacsrc_values[] = { 0, 1, 2, 3 }; |
|---|
| 205 | 202 | |
|---|
| 206 | 203 | static SOC_ENUM_SINGLE_DECL(es8316_dacsrc_mux_enum, ES8316_DAC_SET1, |
|---|
| 207 | 204 | 6, es8316_dacsrc_texts); |
|---|
| .. | .. |
|---|
| 368 | 365 | { |
|---|
| 369 | 366 | struct snd_soc_component *component = codec_dai->component; |
|---|
| 370 | 367 | struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component); |
|---|
| 371 | | - int i; |
|---|
| 368 | + int i, ret; |
|---|
| 372 | 369 | int count = 0; |
|---|
| 373 | 370 | |
|---|
| 374 | 371 | es8316->sysclk = freq; |
|---|
| 372 | + es8316->sysclk_constraints.list = NULL; |
|---|
| 373 | + es8316->sysclk_constraints.count = 0; |
|---|
| 375 | 374 | |
|---|
| 376 | 375 | if (freq == 0) |
|---|
| 377 | 376 | return 0; |
|---|
| 377 | + |
|---|
| 378 | + ret = clk_set_rate(es8316->mclk, freq); |
|---|
| 379 | + if (ret) |
|---|
| 380 | + return ret; |
|---|
| 378 | 381 | |
|---|
| 379 | 382 | /* Limit supported sample rates to ones that can be autodetected |
|---|
| 380 | 383 | * by the codec running in slave mode. |
|---|
| .. | .. |
|---|
| 386 | 389 | es8316->allowed_rates[count++] = freq / ratio; |
|---|
| 387 | 390 | } |
|---|
| 388 | 391 | |
|---|
| 389 | | - es8316->sysclk_constraints.list = es8316->allowed_rates; |
|---|
| 390 | | - es8316->sysclk_constraints.count = count; |
|---|
| 392 | + if (count) { |
|---|
| 393 | + es8316->sysclk_constraints.list = es8316->allowed_rates; |
|---|
| 394 | + es8316->sysclk_constraints.count = count; |
|---|
| 395 | + } |
|---|
| 391 | 396 | |
|---|
| 392 | 397 | return 0; |
|---|
| 393 | 398 | } |
|---|
| .. | .. |
|---|
| 449 | 454 | struct snd_soc_component *component = dai->component; |
|---|
| 450 | 455 | struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component); |
|---|
| 451 | 456 | |
|---|
| 452 | | - if (es8316->sysclk == 0) { |
|---|
| 453 | | - dev_err(component->dev, "No sysclk provided\n"); |
|---|
| 454 | | - return -EINVAL; |
|---|
| 455 | | - } |
|---|
| 456 | | - |
|---|
| 457 | | - /* The set of sample rates that can be supported depends on the |
|---|
| 458 | | - * MCLK supplied to the CODEC. |
|---|
| 459 | | - */ |
|---|
| 460 | | - snd_pcm_hw_constraint_list(substream->runtime, 0, |
|---|
| 461 | | - SNDRV_PCM_HW_PARAM_RATE, |
|---|
| 462 | | - &es8316->sysclk_constraints); |
|---|
| 457 | + if (es8316->sysclk_constraints.list) |
|---|
| 458 | + snd_pcm_hw_constraint_list(substream->runtime, 0, |
|---|
| 459 | + SNDRV_PCM_HW_PARAM_RATE, |
|---|
| 460 | + &es8316->sysclk_constraints); |
|---|
| 463 | 461 | |
|---|
| 464 | 462 | return 0; |
|---|
| 465 | 463 | } |
|---|
| .. | .. |
|---|
| 471 | 469 | struct snd_soc_component *component = dai->component; |
|---|
| 472 | 470 | struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component); |
|---|
| 473 | 471 | u8 wordlen = 0; |
|---|
| 472 | + int i; |
|---|
| 474 | 473 | |
|---|
| 475 | | - if (!es8316->sysclk) { |
|---|
| 476 | | - dev_err(component->dev, "No MCLK configured\n"); |
|---|
| 477 | | - return -EINVAL; |
|---|
| 474 | + /* Validate supported sample rates that are autodetected from MCLK */ |
|---|
| 475 | + for (i = 0; i < NR_SUPPORTED_MCLK_LRCK_RATIOS; i++) { |
|---|
| 476 | + const unsigned int ratio = supported_mclk_lrck_ratios[i]; |
|---|
| 477 | + |
|---|
| 478 | + if (es8316->sysclk % ratio != 0) |
|---|
| 479 | + continue; |
|---|
| 480 | + if (es8316->sysclk / ratio == params_rate(params)) |
|---|
| 481 | + break; |
|---|
| 478 | 482 | } |
|---|
| 483 | + if (i == NR_SUPPORTED_MCLK_LRCK_RATIOS) |
|---|
| 484 | + return -EINVAL; |
|---|
| 479 | 485 | |
|---|
| 480 | 486 | switch (params_format(params)) { |
|---|
| 481 | 487 | case SNDRV_PCM_FORMAT_S16_LE: |
|---|
| .. | .. |
|---|
| 501 | 507 | return 0; |
|---|
| 502 | 508 | } |
|---|
| 503 | 509 | |
|---|
| 504 | | -static void es8316_enable_spk(struct es8316_priv *es8316, bool enable) |
|---|
| 510 | +static int es8316_mute(struct snd_soc_dai *dai, int mute, int direction) |
|---|
| 505 | 511 | { |
|---|
| 506 | | - if (!es8316 || !es8316->gpiod_spk_ctl) |
|---|
| 507 | | - return; |
|---|
| 508 | | - gpiod_set_value(es8316->gpiod_spk_ctl, enable); |
|---|
| 509 | | -} |
|---|
| 510 | | - |
|---|
| 511 | | -static int es8316_extcon_notifier(struct notifier_block *self, unsigned long event, void *ptr) |
|---|
| 512 | | -{ |
|---|
| 513 | | - struct es8316_priv *es8316 = container_of(self, struct es8316_priv, |
|---|
| 514 | | - extcon_nb); |
|---|
| 515 | | - |
|---|
| 516 | | - if (event) { |
|---|
| 517 | | - es8316->hp_inserted = true; |
|---|
| 518 | | - es8316_enable_spk(es8316, false); |
|---|
| 519 | | - } else { |
|---|
| 520 | | - es8316->hp_inserted = false; |
|---|
| 521 | | - } |
|---|
| 522 | | - return NOTIFY_DONE; |
|---|
| 523 | | -} |
|---|
| 524 | | - |
|---|
| 525 | | -static int es8316_mute(struct snd_soc_dai *dai, int mute) |
|---|
| 526 | | -{ |
|---|
| 527 | | - struct snd_soc_component *component = dai->component; |
|---|
| 528 | | - struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component); |
|---|
| 529 | | - unsigned int val = mute ? 0x20 : 0; |
|---|
| 530 | | - |
|---|
| 531 | | - es8316->muted = mute; |
|---|
| 532 | | - if (mute) { |
|---|
| 533 | | - es8316_enable_spk(es8316, false); |
|---|
| 534 | | - msleep(100); |
|---|
| 535 | | - snd_soc_component_update_bits(dai->component, ES8316_DAC_SET1, 0x20, val); |
|---|
| 536 | | - } else { |
|---|
| 537 | | - snd_soc_component_update_bits(dai->component, ES8316_DAC_SET1, 0x20, val); |
|---|
| 538 | | - msleep(130); |
|---|
| 539 | | - if (!es8316->hp_inserted) |
|---|
| 540 | | - es8316_enable_spk(es8316, true); |
|---|
| 541 | | - } |
|---|
| 542 | | - return 0; |
|---|
| 543 | | -} |
|---|
| 544 | | - |
|---|
| 545 | | -static int es8316_set_bias_level(struct snd_soc_component *component, |
|---|
| 546 | | - enum snd_soc_bias_level level) |
|---|
| 547 | | -{ |
|---|
| 548 | | - struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component); |
|---|
| 549 | | - int ret; |
|---|
| 550 | | - |
|---|
| 551 | | - switch (level) { |
|---|
| 552 | | - case SND_SOC_BIAS_ON: |
|---|
| 553 | | - break; |
|---|
| 554 | | - case SND_SOC_BIAS_PREPARE: |
|---|
| 555 | | - if (IS_ERR(es8316->mclk)) |
|---|
| 556 | | - break; |
|---|
| 557 | | - |
|---|
| 558 | | - if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_ON) { |
|---|
| 559 | | - clk_disable_unprepare(es8316->mclk); |
|---|
| 560 | | - } else { |
|---|
| 561 | | - ret = clk_prepare_enable(es8316->mclk); |
|---|
| 562 | | - if (ret) |
|---|
| 563 | | - return ret; |
|---|
| 564 | | - } |
|---|
| 565 | | - break; |
|---|
| 566 | | - |
|---|
| 567 | | - case SND_SOC_BIAS_STANDBY: |
|---|
| 568 | | - break; |
|---|
| 569 | | - |
|---|
| 570 | | - case SND_SOC_BIAS_OFF: |
|---|
| 571 | | - break; |
|---|
| 572 | | - } |
|---|
| 512 | + snd_soc_component_update_bits(dai->component, ES8316_DAC_SET1, 0x20, |
|---|
| 513 | + mute ? 0x20 : 0); |
|---|
| 573 | 514 | return 0; |
|---|
| 574 | 515 | } |
|---|
| 575 | 516 | |
|---|
| .. | .. |
|---|
| 581 | 522 | .hw_params = es8316_pcm_hw_params, |
|---|
| 582 | 523 | .set_fmt = es8316_set_dai_fmt, |
|---|
| 583 | 524 | .set_sysclk = es8316_set_dai_sysclk, |
|---|
| 584 | | - .digital_mute = es8316_mute, |
|---|
| 525 | + .mute_stream = es8316_mute, |
|---|
| 526 | + .no_capture_mute = 1, |
|---|
| 585 | 527 | }; |
|---|
| 586 | 528 | |
|---|
| 587 | 529 | static struct snd_soc_dai_driver es8316_dai = { |
|---|
| .. | .. |
|---|
| 604 | 546 | .symmetric_rates = 1, |
|---|
| 605 | 547 | }; |
|---|
| 606 | 548 | |
|---|
| 549 | +static void es8316_enable_micbias_for_mic_gnd_short_detect( |
|---|
| 550 | + struct snd_soc_component *component) |
|---|
| 551 | +{ |
|---|
| 552 | + struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component); |
|---|
| 553 | + |
|---|
| 554 | + snd_soc_dapm_mutex_lock(dapm); |
|---|
| 555 | + snd_soc_dapm_force_enable_pin_unlocked(dapm, "Bias"); |
|---|
| 556 | + snd_soc_dapm_force_enable_pin_unlocked(dapm, "Analog power"); |
|---|
| 557 | + snd_soc_dapm_force_enable_pin_unlocked(dapm, "Mic Bias"); |
|---|
| 558 | + snd_soc_dapm_sync_unlocked(dapm); |
|---|
| 559 | + snd_soc_dapm_mutex_unlock(dapm); |
|---|
| 560 | + |
|---|
| 561 | + msleep(20); |
|---|
| 562 | +} |
|---|
| 563 | + |
|---|
| 564 | +static void es8316_disable_micbias_for_mic_gnd_short_detect( |
|---|
| 565 | + struct snd_soc_component *component) |
|---|
| 566 | +{ |
|---|
| 567 | + struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component); |
|---|
| 568 | + |
|---|
| 569 | + snd_soc_dapm_mutex_lock(dapm); |
|---|
| 570 | + snd_soc_dapm_disable_pin_unlocked(dapm, "Mic Bias"); |
|---|
| 571 | + snd_soc_dapm_disable_pin_unlocked(dapm, "Analog power"); |
|---|
| 572 | + snd_soc_dapm_disable_pin_unlocked(dapm, "Bias"); |
|---|
| 573 | + snd_soc_dapm_sync_unlocked(dapm); |
|---|
| 574 | + snd_soc_dapm_mutex_unlock(dapm); |
|---|
| 575 | +} |
|---|
| 576 | + |
|---|
| 577 | +static irqreturn_t es8316_irq(int irq, void *data) |
|---|
| 578 | +{ |
|---|
| 579 | + struct es8316_priv *es8316 = data; |
|---|
| 580 | + struct snd_soc_component *comp = es8316->component; |
|---|
| 581 | + unsigned int flags; |
|---|
| 582 | + |
|---|
| 583 | + mutex_lock(&es8316->lock); |
|---|
| 584 | + |
|---|
| 585 | + regmap_read(es8316->regmap, ES8316_GPIO_FLAG, &flags); |
|---|
| 586 | + if (flags == 0x00) |
|---|
| 587 | + goto out; /* Powered-down / reset */ |
|---|
| 588 | + |
|---|
| 589 | + /* Catch spurious IRQ before set_jack is called */ |
|---|
| 590 | + if (!es8316->jack) |
|---|
| 591 | + goto out; |
|---|
| 592 | + |
|---|
| 593 | + if (es8316->jd_inverted) |
|---|
| 594 | + flags ^= ES8316_GPIO_FLAG_HP_NOT_INSERTED; |
|---|
| 595 | + |
|---|
| 596 | + dev_dbg(comp->dev, "gpio flags %#04x\n", flags); |
|---|
| 597 | + if (flags & ES8316_GPIO_FLAG_HP_NOT_INSERTED) { |
|---|
| 598 | + /* Jack removed, or spurious IRQ? */ |
|---|
| 599 | + if (es8316->jack->status & SND_JACK_MICROPHONE) |
|---|
| 600 | + es8316_disable_micbias_for_mic_gnd_short_detect(comp); |
|---|
| 601 | + |
|---|
| 602 | + if (es8316->jack->status & SND_JACK_HEADPHONE) { |
|---|
| 603 | + snd_soc_jack_report(es8316->jack, 0, |
|---|
| 604 | + SND_JACK_HEADSET | SND_JACK_BTN_0); |
|---|
| 605 | + dev_dbg(comp->dev, "jack unplugged\n"); |
|---|
| 606 | + } |
|---|
| 607 | + } else if (!(es8316->jack->status & SND_JACK_HEADPHONE)) { |
|---|
| 608 | + /* Jack inserted, determine type */ |
|---|
| 609 | + es8316_enable_micbias_for_mic_gnd_short_detect(comp); |
|---|
| 610 | + regmap_read(es8316->regmap, ES8316_GPIO_FLAG, &flags); |
|---|
| 611 | + if (es8316->jd_inverted) |
|---|
| 612 | + flags ^= ES8316_GPIO_FLAG_HP_NOT_INSERTED; |
|---|
| 613 | + dev_dbg(comp->dev, "gpio flags %#04x\n", flags); |
|---|
| 614 | + if (flags & ES8316_GPIO_FLAG_HP_NOT_INSERTED) { |
|---|
| 615 | + /* Jack unplugged underneath us */ |
|---|
| 616 | + es8316_disable_micbias_for_mic_gnd_short_detect(comp); |
|---|
| 617 | + } else if (flags & ES8316_GPIO_FLAG_GM_NOT_SHORTED) { |
|---|
| 618 | + /* Open, headset */ |
|---|
| 619 | + snd_soc_jack_report(es8316->jack, |
|---|
| 620 | + SND_JACK_HEADSET, |
|---|
| 621 | + SND_JACK_HEADSET); |
|---|
| 622 | + /* Keep mic-gnd-short detection on for button press */ |
|---|
| 623 | + } else { |
|---|
| 624 | + /* Shorted, headphones */ |
|---|
| 625 | + snd_soc_jack_report(es8316->jack, |
|---|
| 626 | + SND_JACK_HEADPHONE, |
|---|
| 627 | + SND_JACK_HEADSET); |
|---|
| 628 | + /* No longer need mic-gnd-short detection */ |
|---|
| 629 | + es8316_disable_micbias_for_mic_gnd_short_detect(comp); |
|---|
| 630 | + } |
|---|
| 631 | + } else if (es8316->jack->status & SND_JACK_MICROPHONE) { |
|---|
| 632 | + /* Interrupt while jack inserted, report button state */ |
|---|
| 633 | + if (flags & ES8316_GPIO_FLAG_GM_NOT_SHORTED) { |
|---|
| 634 | + /* Open, button release */ |
|---|
| 635 | + snd_soc_jack_report(es8316->jack, 0, SND_JACK_BTN_0); |
|---|
| 636 | + } else { |
|---|
| 637 | + /* Short, button press */ |
|---|
| 638 | + snd_soc_jack_report(es8316->jack, |
|---|
| 639 | + SND_JACK_BTN_0, |
|---|
| 640 | + SND_JACK_BTN_0); |
|---|
| 641 | + } |
|---|
| 642 | + } |
|---|
| 643 | + |
|---|
| 644 | +out: |
|---|
| 645 | + mutex_unlock(&es8316->lock); |
|---|
| 646 | + return IRQ_HANDLED; |
|---|
| 647 | +} |
|---|
| 648 | + |
|---|
| 649 | +static void es8316_enable_jack_detect(struct snd_soc_component *component, |
|---|
| 650 | + struct snd_soc_jack *jack) |
|---|
| 651 | +{ |
|---|
| 652 | + struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component); |
|---|
| 653 | + |
|---|
| 654 | + /* |
|---|
| 655 | + * Init es8316->jd_inverted here and not in the probe, as we cannot |
|---|
| 656 | + * guarantee that the bytchr-es8316 driver, which might set this |
|---|
| 657 | + * property, will probe before us. |
|---|
| 658 | + */ |
|---|
| 659 | + es8316->jd_inverted = device_property_read_bool(component->dev, |
|---|
| 660 | + "everest,jack-detect-inverted"); |
|---|
| 661 | + |
|---|
| 662 | + mutex_lock(&es8316->lock); |
|---|
| 663 | + |
|---|
| 664 | + es8316->jack = jack; |
|---|
| 665 | + |
|---|
| 666 | + if (es8316->jack->status & SND_JACK_MICROPHONE) |
|---|
| 667 | + es8316_enable_micbias_for_mic_gnd_short_detect(component); |
|---|
| 668 | + |
|---|
| 669 | + snd_soc_component_update_bits(component, ES8316_GPIO_DEBOUNCE, |
|---|
| 670 | + ES8316_GPIO_ENABLE_INTERRUPT, |
|---|
| 671 | + ES8316_GPIO_ENABLE_INTERRUPT); |
|---|
| 672 | + |
|---|
| 673 | + mutex_unlock(&es8316->lock); |
|---|
| 674 | + |
|---|
| 675 | + /* Enable irq and sync initial jack state */ |
|---|
| 676 | + enable_irq(es8316->irq); |
|---|
| 677 | + es8316_irq(es8316->irq, es8316); |
|---|
| 678 | +} |
|---|
| 679 | + |
|---|
| 680 | +static void es8316_disable_jack_detect(struct snd_soc_component *component) |
|---|
| 681 | +{ |
|---|
| 682 | + struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component); |
|---|
| 683 | + |
|---|
| 684 | + disable_irq(es8316->irq); |
|---|
| 685 | + |
|---|
| 686 | + mutex_lock(&es8316->lock); |
|---|
| 687 | + |
|---|
| 688 | + snd_soc_component_update_bits(component, ES8316_GPIO_DEBOUNCE, |
|---|
| 689 | + ES8316_GPIO_ENABLE_INTERRUPT, 0); |
|---|
| 690 | + |
|---|
| 691 | + if (es8316->jack->status & SND_JACK_MICROPHONE) { |
|---|
| 692 | + es8316_disable_micbias_for_mic_gnd_short_detect(component); |
|---|
| 693 | + snd_soc_jack_report(es8316->jack, 0, SND_JACK_BTN_0); |
|---|
| 694 | + } |
|---|
| 695 | + |
|---|
| 696 | + es8316->jack = NULL; |
|---|
| 697 | + |
|---|
| 698 | + mutex_unlock(&es8316->lock); |
|---|
| 699 | +} |
|---|
| 700 | + |
|---|
| 701 | +static int es8316_set_jack(struct snd_soc_component *component, |
|---|
| 702 | + struct snd_soc_jack *jack, void *data) |
|---|
| 703 | +{ |
|---|
| 704 | + if (jack) |
|---|
| 705 | + es8316_enable_jack_detect(component, jack); |
|---|
| 706 | + else |
|---|
| 707 | + es8316_disable_jack_detect(component); |
|---|
| 708 | + |
|---|
| 709 | + return 0; |
|---|
| 710 | +} |
|---|
| 711 | + |
|---|
| 607 | 712 | static int es8316_probe(struct snd_soc_component *component) |
|---|
| 608 | 713 | { |
|---|
| 609 | 714 | struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component); |
|---|
| 610 | | - int ret = 0; |
|---|
| 715 | + int ret; |
|---|
| 611 | 716 | |
|---|
| 612 | | - es8316->mclk = devm_clk_get(component->dev, "mclk"); |
|---|
| 613 | | - if (PTR_ERR(es8316->mclk) == -EPROBE_DEFER) |
|---|
| 614 | | - return -EPROBE_DEFER; |
|---|
| 717 | + es8316->component = component; |
|---|
| 718 | + |
|---|
| 719 | + es8316->mclk = devm_clk_get_optional(component->dev, "mclk"); |
|---|
| 720 | + if (IS_ERR(es8316->mclk)) { |
|---|
| 721 | + dev_err(component->dev, "unable to get mclk\n"); |
|---|
| 722 | + return PTR_ERR(es8316->mclk); |
|---|
| 723 | + } |
|---|
| 724 | + if (!es8316->mclk) |
|---|
| 725 | + dev_warn(component->dev, "assuming static mclk\n"); |
|---|
| 615 | 726 | |
|---|
| 616 | 727 | ret = clk_prepare_enable(es8316->mclk); |
|---|
| 617 | | - if (ret) |
|---|
| 728 | + if (ret) { |
|---|
| 729 | + dev_err(component->dev, "unable to enable mclk\n"); |
|---|
| 618 | 730 | return ret; |
|---|
| 731 | + } |
|---|
| 619 | 732 | |
|---|
| 620 | 733 | /* Reset codec and enable current state machine */ |
|---|
| 621 | 734 | snd_soc_component_write(component, ES8316_RESET, 0x3f); |
|---|
| .. | .. |
|---|
| 649 | 762 | static const struct snd_soc_component_driver soc_component_dev_es8316 = { |
|---|
| 650 | 763 | .probe = es8316_probe, |
|---|
| 651 | 764 | .remove = es8316_remove, |
|---|
| 652 | | - .set_bias_level = es8316_set_bias_level, |
|---|
| 765 | + .set_jack = es8316_set_jack, |
|---|
| 653 | 766 | .controls = es8316_snd_controls, |
|---|
| 654 | 767 | .num_controls = ARRAY_SIZE(es8316_snd_controls), |
|---|
| 655 | 768 | .dapm_widgets = es8316_dapm_widgets, |
|---|
| .. | .. |
|---|
| 661 | 774 | .non_legacy_dai_naming = 1, |
|---|
| 662 | 775 | }; |
|---|
| 663 | 776 | |
|---|
| 777 | +static const struct regmap_range es8316_volatile_ranges[] = { |
|---|
| 778 | + regmap_reg_range(ES8316_GPIO_FLAG, ES8316_GPIO_FLAG), |
|---|
| 779 | +}; |
|---|
| 780 | + |
|---|
| 781 | +static const struct regmap_access_table es8316_volatile_table = { |
|---|
| 782 | + .yes_ranges = es8316_volatile_ranges, |
|---|
| 783 | + .n_yes_ranges = ARRAY_SIZE(es8316_volatile_ranges), |
|---|
| 784 | +}; |
|---|
| 785 | + |
|---|
| 664 | 786 | static const struct regmap_config es8316_regmap = { |
|---|
| 665 | 787 | .reg_bits = 8, |
|---|
| 666 | 788 | .val_bits = 8, |
|---|
| 667 | 789 | .max_register = 0x53, |
|---|
| 790 | + .volatile_table = &es8316_volatile_table, |
|---|
| 668 | 791 | .cache_type = REGCACHE_RBTREE, |
|---|
| 669 | 792 | }; |
|---|
| 670 | 793 | |
|---|
| 671 | 794 | static int es8316_i2c_probe(struct i2c_client *i2c_client, |
|---|
| 672 | 795 | const struct i2c_device_id *id) |
|---|
| 673 | 796 | { |
|---|
| 797 | + struct device *dev = &i2c_client->dev; |
|---|
| 674 | 798 | struct es8316_priv *es8316; |
|---|
| 675 | | - struct extcon_dev *edev; |
|---|
| 676 | | - struct regmap *regmap; |
|---|
| 677 | | - int ret = -1; |
|---|
| 799 | + int ret; |
|---|
| 678 | 800 | |
|---|
| 679 | 801 | es8316 = devm_kzalloc(&i2c_client->dev, sizeof(struct es8316_priv), |
|---|
| 680 | 802 | GFP_KERNEL); |
|---|
| 681 | 803 | if (es8316 == NULL) |
|---|
| 682 | 804 | return -ENOMEM; |
|---|
| 683 | | - es8316->hp_inserted = false; |
|---|
| 684 | | - es8316->muted = true; |
|---|
| 805 | + |
|---|
| 685 | 806 | i2c_set_clientdata(i2c_client, es8316); |
|---|
| 686 | 807 | |
|---|
| 687 | | - regmap = devm_regmap_init_i2c(i2c_client, &es8316_regmap); |
|---|
| 688 | | - if (IS_ERR(regmap)) |
|---|
| 689 | | - return PTR_ERR(regmap); |
|---|
| 690 | | - es8316->gpiod_spk_ctl = devm_gpiod_get_optional(&i2c_client->dev, |
|---|
| 691 | | - "spk-con", |
|---|
| 692 | | - GPIOD_OUT_LOW); |
|---|
| 693 | | - if (IS_ERR(es8316->gpiod_spk_ctl)) { |
|---|
| 694 | | - ret = IS_ERR(es8316->gpiod_spk_ctl); |
|---|
| 695 | | - es8316->gpiod_spk_ctl = NULL; |
|---|
| 696 | | - dev_warn(&i2c_client->dev, "cannot get spk-con-gpio %d\n", ret); |
|---|
| 697 | | - } |
|---|
| 698 | | - if (device_property_read_bool(&i2c_client->dev, "extcon")) { |
|---|
| 699 | | - edev = extcon_get_edev_by_phandle(&i2c_client->dev, 0); |
|---|
| 700 | | - if (IS_ERR(edev)) { |
|---|
| 701 | | - if (PTR_ERR(edev) == -EPROBE_DEFER) |
|---|
| 702 | | - return -EPROBE_DEFER; |
|---|
| 703 | | - dev_err(&i2c_client->dev, "Invalid or missing extcon\n"); |
|---|
| 704 | | - return PTR_ERR(edev); |
|---|
| 705 | | - } |
|---|
| 706 | | - es8316->extcon_nb.notifier_call = es8316_extcon_notifier; |
|---|
| 707 | | - ret = devm_extcon_register_notifier(&i2c_client->dev, edev, |
|---|
| 708 | | - ES8316_EXTCON_ID, |
|---|
| 709 | | - &es8316->extcon_nb); |
|---|
| 710 | | - if (ret < 0) { |
|---|
| 711 | | - dev_err(&i2c_client->dev, "register notifier fail\n"); |
|---|
| 712 | | - return ret; |
|---|
| 808 | + es8316->regmap = devm_regmap_init_i2c(i2c_client, &es8316_regmap); |
|---|
| 809 | + if (IS_ERR(es8316->regmap)) |
|---|
| 810 | + return PTR_ERR(es8316->regmap); |
|---|
| 811 | + |
|---|
| 812 | + es8316->irq = i2c_client->irq; |
|---|
| 813 | + mutex_init(&es8316->lock); |
|---|
| 814 | + |
|---|
| 815 | + if (es8316->irq > 0) { |
|---|
| 816 | + ret = devm_request_threaded_irq(dev, es8316->irq, NULL, es8316_irq, |
|---|
| 817 | + IRQF_TRIGGER_HIGH | IRQF_ONESHOT | IRQF_NO_AUTOEN, |
|---|
| 818 | + "es8316", es8316); |
|---|
| 819 | + if (ret) { |
|---|
| 820 | + dev_warn(dev, "Failed to get IRQ %d: %d\n", es8316->irq, ret); |
|---|
| 821 | + es8316->irq = -ENXIO; |
|---|
| 713 | 822 | } |
|---|
| 714 | 823 | } |
|---|
| 824 | + |
|---|
| 715 | 825 | return devm_snd_soc_register_component(&i2c_client->dev, |
|---|
| 716 | | - &soc_component_dev_es8316, |
|---|
| 717 | | - &es8316_dai, 1); |
|---|
| 826 | + &soc_component_dev_es8316, |
|---|
| 827 | + &es8316_dai, 1); |
|---|
| 718 | 828 | } |
|---|
| 719 | 829 | |
|---|
| 720 | 830 | static const struct i2c_device_id es8316_i2c_id[] = { |
|---|
| .. | .. |
|---|
| 729 | 839 | }; |
|---|
| 730 | 840 | MODULE_DEVICE_TABLE(of, es8316_of_match); |
|---|
| 731 | 841 | |
|---|
| 842 | +#ifdef CONFIG_ACPI |
|---|
| 732 | 843 | static const struct acpi_device_id es8316_acpi_match[] = { |
|---|
| 733 | 844 | {"ESSX8316", 0}, |
|---|
| 734 | 845 | {}, |
|---|
| 735 | 846 | }; |
|---|
| 736 | 847 | MODULE_DEVICE_TABLE(acpi, es8316_acpi_match); |
|---|
| 848 | +#endif |
|---|
| 737 | 849 | |
|---|
| 738 | 850 | static struct i2c_driver es8316_i2c_driver = { |
|---|
| 739 | 851 | .driver = { |
|---|