hc
2023-12-08 01573e231f18eb2d99162747186f59511f56b64d
kernel/sound/soc/codecs/es8316.c
....@@ -1,33 +1,28 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * es8316.c -- es8316 ALSA SoC audio driver
34 * Copyright Everest Semiconductor Co.,Ltd
45 *
56 * Authors: David Yang <yangxiaohua@everest-semi.com>,
67 * 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.
118 */
129
1310 #include <linux/module.h>
1411 #include <linux/acpi.h>
1512 #include <linux/clk.h>
1613 #include <linux/delay.h>
17
-#include <linux/extcon.h>
18
-#include <linux/gpio.h>
1914 #include <linux/i2c.h>
2015 #include <linux/mod_devicetable.h>
21
-#include <linux/of_gpio.h>
16
+#include <linux/mutex.h>
2217 #include <linux/regmap.h>
2318 #include <sound/pcm.h>
2419 #include <sound/pcm_params.h>
2520 #include <sound/soc.h>
2621 #include <sound/soc-dapm.h>
2722 #include <sound/tlv.h>
23
+#include <sound/jack.h>
2824 #include "es8316.h"
2925
30
-#define ES8316_EXTCON_ID EXTCON_JACK_HEADPHONE
3126 /* In slave mode at single speed, the codec is documented as accepting 5
3227 * MCLK/LRCK ratios, but we also add ratio 400, which is commonly used on
3328 * Intel Cherry Trail platforms (19.2MHz MCLK, 48kHz LRCK).
....@@ -38,16 +33,16 @@
3833 };
3934
4035 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;
4142 unsigned int sysclk;
4243 unsigned int allowed_rates[NR_SUPPORTED_MCLK_LRCK_RATIOS];
4344 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;
5146 };
5247
5348 /*
....@@ -94,7 +89,7 @@
9489 SOC_DOUBLE_TLV("Headphone Playback Volume", ES8316_CPHP_ICAL_VOL,
9590 4, 0, 3, 1, hpout_vol_tlv),
9691 SOC_DOUBLE_TLV("Headphone Mixer Volume", ES8316_HPMIX_VOL,
97
- 0, 4, 11, 0, hpmixer_gain_tlv),
92
+ 4, 0, 11, 0, hpmixer_gain_tlv),
9893
9994 SOC_ENUM("Playback Polarity", dacpol),
10095 SOC_DOUBLE_R_TLV("DAC Playback Volume", ES8316_DAC_VOLL,
....@@ -104,6 +99,7 @@
10499 SOC_SINGLE("DAC Notch Filter Switch", ES8316_DAC_SET2, 6, 1, 0),
105100 SOC_SINGLE("DAC Double Fs Switch", ES8316_DAC_SET2, 7, 1, 0),
106101 SOC_SINGLE("DAC Stereo Enhancement", ES8316_DAC_SET3, 0, 7, 0),
102
+ SOC_SINGLE("DAC Mono Mix Switch", ES8316_DAC_SET3, 3, 1, 0),
107103
108104 SOC_ENUM("Capture Polarity", adcpol),
109105 SOC_SINGLE("Mic Boost Switch", ES8316_ADC_D2SEPGA, 0, 1, 0),
....@@ -169,8 +165,6 @@
169165 "lin-rin with Boost and PGA"
170166 };
171167
172
-static const unsigned int es8316_hpmux_values[] = { 0, 1, 2, 3 };
173
-
174168 static SOC_ENUM_SINGLE_DECL(es8316_left_hpmux_enum, ES8316_HPMIX_SEL,
175169 4, es8316_hpmux_texts);
176170
....@@ -200,8 +194,6 @@
200194 "RDATA TO LDAC, RDATA TO RDAC",
201195 "RDATA TO LDAC, LDATA TO RDAC",
202196 };
203
-
204
-static const unsigned int es8316_dacsrc_values[] = { 0, 1, 2, 3 };
205197
206198 static SOC_ENUM_SINGLE_DECL(es8316_dacsrc_mux_enum, ES8316_DAC_SET1,
207199 6, es8316_dacsrc_texts);
....@@ -368,13 +360,21 @@
368360 {
369361 struct snd_soc_component *component = codec_dai->component;
370362 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
371
- int i;
363
+ int i, ret;
372364 int count = 0;
373365
374366 es8316->sysclk = freq;
375367
376
- if (freq == 0)
368
+ if (freq == 0) {
369
+ es8316->sysclk_constraints.list = NULL;
370
+ es8316->sysclk_constraints.count = 0;
371
+
377372 return 0;
373
+ }
374
+
375
+ ret = clk_set_rate(es8316->mclk, freq);
376
+ if (ret)
377
+ return ret;
378378
379379 /* Limit supported sample rates to ones that can be autodetected
380380 * by the codec running in slave mode.
....@@ -449,17 +449,10 @@
449449 struct snd_soc_component *component = dai->component;
450450 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
451451
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);
452
+ if (es8316->sysclk_constraints.list)
453
+ snd_pcm_hw_constraint_list(substream->runtime, 0,
454
+ SNDRV_PCM_HW_PARAM_RATE,
455
+ &es8316->sysclk_constraints);
463456
464457 return 0;
465458 }
....@@ -471,11 +464,19 @@
471464 struct snd_soc_component *component = dai->component;
472465 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
473466 u8 wordlen = 0;
467
+ int i;
474468
475
- if (!es8316->sysclk) {
476
- dev_err(component->dev, "No MCLK configured\n");
477
- return -EINVAL;
469
+ /* Validate supported sample rates that are autodetected from MCLK */
470
+ for (i = 0; i < NR_SUPPORTED_MCLK_LRCK_RATIOS; i++) {
471
+ const unsigned int ratio = supported_mclk_lrck_ratios[i];
472
+
473
+ if (es8316->sysclk % ratio != 0)
474
+ continue;
475
+ if (es8316->sysclk / ratio == params_rate(params))
476
+ break;
478477 }
478
+ if (i == NR_SUPPORTED_MCLK_LRCK_RATIOS)
479
+ return -EINVAL;
479480
480481 switch (params_format(params)) {
481482 case SNDRV_PCM_FORMAT_S16_LE:
....@@ -501,75 +502,10 @@
501502 return 0;
502503 }
503504
504
-static void es8316_enable_spk(struct es8316_priv *es8316, bool enable)
505
+static int es8316_mute(struct snd_soc_dai *dai, int mute, int direction)
505506 {
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
- }
507
+ snd_soc_component_update_bits(dai->component, ES8316_DAC_SET1, 0x20,
508
+ mute ? 0x20 : 0);
573509 return 0;
574510 }
575511
....@@ -581,7 +517,8 @@
581517 .hw_params = es8316_pcm_hw_params,
582518 .set_fmt = es8316_set_dai_fmt,
583519 .set_sysclk = es8316_set_dai_sysclk,
584
- .digital_mute = es8316_mute,
520
+ .mute_stream = es8316_mute,
521
+ .no_capture_mute = 1,
585522 };
586523
587524 static struct snd_soc_dai_driver es8316_dai = {
....@@ -604,18 +541,189 @@
604541 .symmetric_rates = 1,
605542 };
606543
544
+static void es8316_enable_micbias_for_mic_gnd_short_detect(
545
+ struct snd_soc_component *component)
546
+{
547
+ struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
548
+
549
+ snd_soc_dapm_mutex_lock(dapm);
550
+ snd_soc_dapm_force_enable_pin_unlocked(dapm, "Bias");
551
+ snd_soc_dapm_force_enable_pin_unlocked(dapm, "Analog power");
552
+ snd_soc_dapm_force_enable_pin_unlocked(dapm, "Mic Bias");
553
+ snd_soc_dapm_sync_unlocked(dapm);
554
+ snd_soc_dapm_mutex_unlock(dapm);
555
+
556
+ msleep(20);
557
+}
558
+
559
+static void es8316_disable_micbias_for_mic_gnd_short_detect(
560
+ struct snd_soc_component *component)
561
+{
562
+ struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
563
+
564
+ snd_soc_dapm_mutex_lock(dapm);
565
+ snd_soc_dapm_disable_pin_unlocked(dapm, "Mic Bias");
566
+ snd_soc_dapm_disable_pin_unlocked(dapm, "Analog power");
567
+ snd_soc_dapm_disable_pin_unlocked(dapm, "Bias");
568
+ snd_soc_dapm_sync_unlocked(dapm);
569
+ snd_soc_dapm_mutex_unlock(dapm);
570
+}
571
+
572
+static irqreturn_t es8316_irq(int irq, void *data)
573
+{
574
+ struct es8316_priv *es8316 = data;
575
+ struct snd_soc_component *comp = es8316->component;
576
+ unsigned int flags;
577
+
578
+ mutex_lock(&es8316->lock);
579
+
580
+ regmap_read(es8316->regmap, ES8316_GPIO_FLAG, &flags);
581
+ if (flags == 0x00)
582
+ goto out; /* Powered-down / reset */
583
+
584
+ /* Catch spurious IRQ before set_jack is called */
585
+ if (!es8316->jack)
586
+ goto out;
587
+
588
+ if (es8316->jd_inverted)
589
+ flags ^= ES8316_GPIO_FLAG_HP_NOT_INSERTED;
590
+
591
+ dev_dbg(comp->dev, "gpio flags %#04x\n", flags);
592
+ if (flags & ES8316_GPIO_FLAG_HP_NOT_INSERTED) {
593
+ /* Jack removed, or spurious IRQ? */
594
+ if (es8316->jack->status & SND_JACK_MICROPHONE)
595
+ es8316_disable_micbias_for_mic_gnd_short_detect(comp);
596
+
597
+ if (es8316->jack->status & SND_JACK_HEADPHONE) {
598
+ snd_soc_jack_report(es8316->jack, 0,
599
+ SND_JACK_HEADSET | SND_JACK_BTN_0);
600
+ dev_dbg(comp->dev, "jack unplugged\n");
601
+ }
602
+ } else if (!(es8316->jack->status & SND_JACK_HEADPHONE)) {
603
+ /* Jack inserted, determine type */
604
+ es8316_enable_micbias_for_mic_gnd_short_detect(comp);
605
+ regmap_read(es8316->regmap, ES8316_GPIO_FLAG, &flags);
606
+ if (es8316->jd_inverted)
607
+ flags ^= ES8316_GPIO_FLAG_HP_NOT_INSERTED;
608
+ dev_dbg(comp->dev, "gpio flags %#04x\n", flags);
609
+ if (flags & ES8316_GPIO_FLAG_HP_NOT_INSERTED) {
610
+ /* Jack unplugged underneath us */
611
+ es8316_disable_micbias_for_mic_gnd_short_detect(comp);
612
+ } else if (flags & ES8316_GPIO_FLAG_GM_NOT_SHORTED) {
613
+ /* Open, headset */
614
+ snd_soc_jack_report(es8316->jack,
615
+ SND_JACK_HEADSET,
616
+ SND_JACK_HEADSET);
617
+ /* Keep mic-gnd-short detection on for button press */
618
+ } else {
619
+ /* Shorted, headphones */
620
+ snd_soc_jack_report(es8316->jack,
621
+ SND_JACK_HEADPHONE,
622
+ SND_JACK_HEADSET);
623
+ /* No longer need mic-gnd-short detection */
624
+ es8316_disable_micbias_for_mic_gnd_short_detect(comp);
625
+ }
626
+ } else if (es8316->jack->status & SND_JACK_MICROPHONE) {
627
+ /* Interrupt while jack inserted, report button state */
628
+ if (flags & ES8316_GPIO_FLAG_GM_NOT_SHORTED) {
629
+ /* Open, button release */
630
+ snd_soc_jack_report(es8316->jack, 0, SND_JACK_BTN_0);
631
+ } else {
632
+ /* Short, button press */
633
+ snd_soc_jack_report(es8316->jack,
634
+ SND_JACK_BTN_0,
635
+ SND_JACK_BTN_0);
636
+ }
637
+ }
638
+
639
+out:
640
+ mutex_unlock(&es8316->lock);
641
+ return IRQ_HANDLED;
642
+}
643
+
644
+static void es8316_enable_jack_detect(struct snd_soc_component *component,
645
+ struct snd_soc_jack *jack)
646
+{
647
+ struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
648
+
649
+ /*
650
+ * Init es8316->jd_inverted here and not in the probe, as we cannot
651
+ * guarantee that the bytchr-es8316 driver, which might set this
652
+ * property, will probe before us.
653
+ */
654
+ es8316->jd_inverted = device_property_read_bool(component->dev,
655
+ "everest,jack-detect-inverted");
656
+
657
+ mutex_lock(&es8316->lock);
658
+
659
+ es8316->jack = jack;
660
+
661
+ if (es8316->jack->status & SND_JACK_MICROPHONE)
662
+ es8316_enable_micbias_for_mic_gnd_short_detect(component);
663
+
664
+ snd_soc_component_update_bits(component, ES8316_GPIO_DEBOUNCE,
665
+ ES8316_GPIO_ENABLE_INTERRUPT,
666
+ ES8316_GPIO_ENABLE_INTERRUPT);
667
+
668
+ mutex_unlock(&es8316->lock);
669
+
670
+ /* Enable irq and sync initial jack state */
671
+ enable_irq(es8316->irq);
672
+ es8316_irq(es8316->irq, es8316);
673
+}
674
+
675
+static void es8316_disable_jack_detect(struct snd_soc_component *component)
676
+{
677
+ struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
678
+
679
+ disable_irq(es8316->irq);
680
+
681
+ mutex_lock(&es8316->lock);
682
+
683
+ snd_soc_component_update_bits(component, ES8316_GPIO_DEBOUNCE,
684
+ ES8316_GPIO_ENABLE_INTERRUPT, 0);
685
+
686
+ if (es8316->jack->status & SND_JACK_MICROPHONE) {
687
+ es8316_disable_micbias_for_mic_gnd_short_detect(component);
688
+ snd_soc_jack_report(es8316->jack, 0, SND_JACK_BTN_0);
689
+ }
690
+
691
+ es8316->jack = NULL;
692
+
693
+ mutex_unlock(&es8316->lock);
694
+}
695
+
696
+static int es8316_set_jack(struct snd_soc_component *component,
697
+ struct snd_soc_jack *jack, void *data)
698
+{
699
+ if (jack)
700
+ es8316_enable_jack_detect(component, jack);
701
+ else
702
+ es8316_disable_jack_detect(component);
703
+
704
+ return 0;
705
+}
706
+
607707 static int es8316_probe(struct snd_soc_component *component)
608708 {
609709 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
610
- int ret = 0;
710
+ int ret;
611711
612
- es8316->mclk = devm_clk_get(component->dev, "mclk");
613
- if (PTR_ERR(es8316->mclk) == -EPROBE_DEFER)
614
- return -EPROBE_DEFER;
712
+ es8316->component = component;
713
+
714
+ es8316->mclk = devm_clk_get_optional(component->dev, "mclk");
715
+ if (IS_ERR(es8316->mclk)) {
716
+ dev_err(component->dev, "unable to get mclk\n");
717
+ return PTR_ERR(es8316->mclk);
718
+ }
719
+ if (!es8316->mclk)
720
+ dev_warn(component->dev, "assuming static mclk\n");
615721
616722 ret = clk_prepare_enable(es8316->mclk);
617
- if (ret)
723
+ if (ret) {
724
+ dev_err(component->dev, "unable to enable mclk\n");
618725 return ret;
726
+ }
619727
620728 /* Reset codec and enable current state machine */
621729 snd_soc_component_write(component, ES8316_RESET, 0x3f);
....@@ -649,7 +757,7 @@
649757 static const struct snd_soc_component_driver soc_component_dev_es8316 = {
650758 .probe = es8316_probe,
651759 .remove = es8316_remove,
652
- .set_bias_level = es8316_set_bias_level,
760
+ .set_jack = es8316_set_jack,
653761 .controls = es8316_snd_controls,
654762 .num_controls = ARRAY_SIZE(es8316_snd_controls),
655763 .dapm_widgets = es8316_dapm_widgets,
....@@ -661,60 +769,58 @@
661769 .non_legacy_dai_naming = 1,
662770 };
663771
772
+static const struct regmap_range es8316_volatile_ranges[] = {
773
+ regmap_reg_range(ES8316_GPIO_FLAG, ES8316_GPIO_FLAG),
774
+};
775
+
776
+static const struct regmap_access_table es8316_volatile_table = {
777
+ .yes_ranges = es8316_volatile_ranges,
778
+ .n_yes_ranges = ARRAY_SIZE(es8316_volatile_ranges),
779
+};
780
+
664781 static const struct regmap_config es8316_regmap = {
665782 .reg_bits = 8,
666783 .val_bits = 8,
667784 .max_register = 0x53,
785
+ .volatile_table = &es8316_volatile_table,
668786 .cache_type = REGCACHE_RBTREE,
669787 };
670788
671789 static int es8316_i2c_probe(struct i2c_client *i2c_client,
672790 const struct i2c_device_id *id)
673791 {
792
+ struct device *dev = &i2c_client->dev;
674793 struct es8316_priv *es8316;
675
- struct extcon_dev *edev;
676
- struct regmap *regmap;
677
- int ret = -1;
794
+ int ret;
678795
679796 es8316 = devm_kzalloc(&i2c_client->dev, sizeof(struct es8316_priv),
680797 GFP_KERNEL);
681798 if (es8316 == NULL)
682799 return -ENOMEM;
683
- es8316->hp_inserted = false;
684
- es8316->muted = true;
800
+
685801 i2c_set_clientdata(i2c_client, es8316);
686802
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);
803
+ es8316->regmap = devm_regmap_init_i2c(i2c_client, &es8316_regmap);
804
+ if (IS_ERR(es8316->regmap))
805
+ return PTR_ERR(es8316->regmap);
806
+
807
+ es8316->irq = i2c_client->irq;
808
+ mutex_init(&es8316->lock);
809
+
810
+ ret = devm_request_threaded_irq(dev, es8316->irq, NULL, es8316_irq,
811
+ IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
812
+ "es8316", es8316);
813
+ if (ret == 0) {
814
+ /* Gets re-enabled by es8316_set_jack() */
815
+ disable_irq(es8316->irq);
816
+ } else {
817
+ dev_warn(dev, "Failed to get IRQ %d: %d\n", es8316->irq, ret);
818
+ es8316->irq = -ENXIO;
697819 }
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;
713
- }
714
- }
820
+
715821 return devm_snd_soc_register_component(&i2c_client->dev,
716
- &soc_component_dev_es8316,
717
- &es8316_dai, 1);
822
+ &soc_component_dev_es8316,
823
+ &es8316_dai, 1);
718824 }
719825
720826 static const struct i2c_device_id es8316_i2c_id[] = {
....@@ -729,11 +835,13 @@
729835 };
730836 MODULE_DEVICE_TABLE(of, es8316_of_match);
731837
838
+#ifdef CONFIG_ACPI
732839 static const struct acpi_device_id es8316_acpi_match[] = {
733840 {"ESSX8316", 0},
734841 {},
735842 };
736843 MODULE_DEVICE_TABLE(acpi, es8316_acpi_match);
844
+#endif
737845
738846 static struct i2c_driver es8316_i2c_driver = {
739847 .driver = {