forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-01-31 f70575805708cabdedea7498aaa3f710fde4d920
kernel/sound/soc/codecs/ak4458.c
....@@ -12,6 +12,7 @@
1212 #include <linux/of_device.h>
1313 #include <linux/of_gpio.h>
1414 #include <linux/pm_runtime.h>
15
+#include <linux/regulator/consumer.h>
1516 #include <linux/slab.h>
1617 #include <sound/initval.h>
1718 #include <sound/pcm_params.h>
....@@ -21,8 +22,27 @@
2122
2223 #include "ak4458.h"
2324
25
+#define AK4458_NUM_SUPPLIES 2
26
+static const char *ak4458_supply_names[AK4458_NUM_SUPPLIES] = {
27
+ "DVDD",
28
+ "AVDD",
29
+};
30
+
31
+enum ak4458_type {
32
+ AK4458 = 0,
33
+ AK4497 = 1,
34
+};
35
+
36
+struct ak4458_drvdata {
37
+ struct snd_soc_dai_driver *dai_drv;
38
+ const struct snd_soc_component_driver *comp_drv;
39
+ enum ak4458_type type;
40
+};
41
+
2442 /* AK4458 Codec Private Data */
2543 struct ak4458_priv {
44
+ struct regulator_bulk_data supplies[AK4458_NUM_SUPPLIES];
45
+ const struct ak4458_drvdata *drvdata;
2646 struct device *dev;
2747 struct regmap *regmap;
2848 struct gpio_desc *reset_gpiod;
....@@ -32,6 +52,7 @@
3252 int fmt;
3353 int slots;
3454 int slot_width;
55
+ u32 dsd_path; /* For ak4497 */
3556 };
3657
3758 static const struct reg_default ak4458_reg_defaults[] = {
....@@ -258,6 +279,33 @@
258279 {"AK4458 AOUTD", NULL, "AK4458 DAC4"},
259280 };
260281
282
+/* ak4497 controls */
283
+static const struct snd_kcontrol_new ak4497_snd_controls[] = {
284
+ SOC_DOUBLE_R_TLV("DAC Playback Volume", AK4458_03_LCHATT,
285
+ AK4458_04_RCHATT, 0, 0xFF, 0, dac_tlv),
286
+ SOC_ENUM("AK4497 De-emphasis Response DAC", ak4458_dac1_dem_enum),
287
+ SOC_ENUM_EXT("AK4497 Digital Filter Setting", ak4458_digfil_enum,
288
+ get_digfil, set_digfil),
289
+ SOC_ENUM("AK4497 Inverting Enable of DZFB", ak4458_dzfb_enum),
290
+ SOC_ENUM("AK4497 Sound Mode", ak4458_sm_enum),
291
+ SOC_ENUM("AK4497 Attenuation transition Time Setting",
292
+ ak4458_ats_enum),
293
+};
294
+
295
+/* ak4497 dapm widgets */
296
+static const struct snd_soc_dapm_widget ak4497_dapm_widgets[] = {
297
+ SND_SOC_DAPM_DAC("AK4497 DAC", NULL, AK4458_0A_CONTROL6, 2, 0),
298
+ SND_SOC_DAPM_AIF_IN("AK4497 SDTI", "Playback", 0, SND_SOC_NOPM, 0, 0),
299
+ SND_SOC_DAPM_OUTPUT("AK4497 AOUT"),
300
+};
301
+
302
+/* ak4497 dapm routes */
303
+static const struct snd_soc_dapm_route ak4497_intercon[] = {
304
+ {"AK4497 DAC", NULL, "AK4497 SDTI"},
305
+ {"AK4497 AOUT", NULL, "AK4497 DAC"},
306
+
307
+};
308
+
261309 static int ak4458_rstn_control(struct snd_soc_component *component, int bit)
262310 {
263311 int ret;
....@@ -285,11 +333,53 @@
285333 struct snd_soc_component *component = dai->component;
286334 struct ak4458_priv *ak4458 = snd_soc_component_get_drvdata(component);
287335 int pcm_width = max(params_physical_width(params), ak4458->slot_width);
288
- int nfs1;
289
- u8 format;
336
+ u8 format, dsdsel0, dsdsel1;
337
+ int nfs1, dsd_bclk;
290338
291339 nfs1 = params_rate(params);
292340 ak4458->fs = nfs1;
341
+
342
+ /* calculate bit clock */
343
+ switch (params_format(params)) {
344
+ case SNDRV_PCM_FORMAT_DSD_U8:
345
+ case SNDRV_PCM_FORMAT_DSD_U16_LE:
346
+ case SNDRV_PCM_FORMAT_DSD_U16_BE:
347
+ case SNDRV_PCM_FORMAT_DSD_U32_LE:
348
+ case SNDRV_PCM_FORMAT_DSD_U32_BE:
349
+ dsd_bclk = nfs1 * params_physical_width(params);
350
+ switch (dsd_bclk) {
351
+ case 2822400:
352
+ dsdsel0 = 0;
353
+ dsdsel1 = 0;
354
+ break;
355
+ case 5644800:
356
+ dsdsel0 = 1;
357
+ dsdsel1 = 0;
358
+ break;
359
+ case 11289600:
360
+ dsdsel0 = 0;
361
+ dsdsel1 = 1;
362
+ break;
363
+ case 22579200:
364
+ if (ak4458->drvdata->type == AK4497) {
365
+ dsdsel0 = 1;
366
+ dsdsel1 = 1;
367
+ } else {
368
+ dev_err(dai->dev, "DSD512 not supported.\n");
369
+ return -EINVAL;
370
+ }
371
+ break;
372
+ default:
373
+ dev_err(dai->dev, "Unsupported dsd bclk.\n");
374
+ return -EINVAL;
375
+ }
376
+
377
+ snd_soc_component_update_bits(component, AK4458_06_DSD1,
378
+ AK4458_DSDSEL_MASK, dsdsel0);
379
+ snd_soc_component_update_bits(component, AK4458_09_DSD2,
380
+ AK4458_DSDSEL_MASK, dsdsel1);
381
+ break;
382
+ }
293383
294384 /* Master Clock Frequency Auto Setting Mode Enable */
295385 snd_soc_component_update_bits(component, AK4458_00_CONTROL1, 0x80, 0x80);
....@@ -313,6 +403,9 @@
313403 format = AK4458_DIF_32BIT_LSB;
314404 break;
315405 case SND_SOC_DAIFMT_DSP_B:
406
+ format = AK4458_DIF_32BIT_MSB;
407
+ break;
408
+ case SND_SOC_DAIFMT_PDM:
316409 format = AK4458_DIF_32BIT_MSB;
317410 break;
318411 default:
....@@ -353,6 +446,7 @@
353446 case SND_SOC_DAIFMT_LEFT_J:
354447 case SND_SOC_DAIFMT_RIGHT_J:
355448 case SND_SOC_DAIFMT_DSP_B:
449
+ case SND_SOC_DAIFMT_PDM:
356450 ak4458->fmt = fmt & SND_SOC_DAIFMT_FORMAT_MASK;
357451 break;
358452 default:
....@@ -360,6 +454,12 @@
360454 fmt & SND_SOC_DAIFMT_FORMAT_MASK);
361455 return -EINVAL;
362456 }
457
+
458
+ /* DSD mode */
459
+ snd_soc_component_update_bits(component, AK4458_02_CONTROL3,
460
+ AK4458_DP_MASK,
461
+ ak4458->fmt == SND_SOC_DAIFMT_PDM ?
462
+ AK4458_DP_MASK : 0);
363463
364464 ak4458_rstn_control(component, 0);
365465 ak4458_rstn_control(component, 1);
....@@ -369,29 +469,29 @@
369469
370470 static const int att_speed[] = { 4080, 2040, 510, 255 };
371471
372
-static int ak4458_set_dai_mute(struct snd_soc_dai *dai, int mute)
472
+static int ak4458_set_dai_mute(struct snd_soc_dai *dai, int mute, int direction)
373473 {
374474 struct snd_soc_component *component = dai->component;
375475 struct ak4458_priv *ak4458 = snd_soc_component_get_drvdata(component);
376
- int nfs, ndt, ret, reg;
476
+ int nfs, ndt, reg;
377477 int ats;
378478
379479 nfs = ak4458->fs;
380480
381
- reg = snd_soc_component_read32(component, AK4458_0B_CONTROL7);
481
+ reg = snd_soc_component_read(component, AK4458_0B_CONTROL7);
382482 ats = (reg & AK4458_ATS_MASK) >> AK4458_ATS_SHIFT;
383483
384484 ndt = att_speed[ats] / (nfs / 1000);
385485
386486 if (mute) {
387
- ret = snd_soc_component_update_bits(component, AK4458_01_CONTROL2, 0x01, 1);
487
+ snd_soc_component_update_bits(component, AK4458_01_CONTROL2, 0x01, 1);
388488 mdelay(ndt);
389489 if (ak4458->mute_gpiod)
390490 gpiod_set_value_cansleep(ak4458->mute_gpiod, 1);
391491 } else {
392492 if (ak4458->mute_gpiod)
393493 gpiod_set_value_cansleep(ak4458->mute_gpiod, 0);
394
- ret = snd_soc_component_update_bits(component, AK4458_01_CONTROL2, 0x01, 0);
494
+ snd_soc_component_update_bits(component, AK4458_01_CONTROL2, 0x01, 0);
395495 mdelay(ndt);
396496 }
397497
....@@ -432,7 +532,10 @@
432532
433533 #define AK4458_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
434534 SNDRV_PCM_FMTBIT_S24_LE |\
435
- SNDRV_PCM_FMTBIT_S32_LE)
535
+ SNDRV_PCM_FMTBIT_S32_LE |\
536
+ SNDRV_PCM_FMTBIT_DSD_U8 |\
537
+ SNDRV_PCM_FMTBIT_DSD_U16_LE |\
538
+ SNDRV_PCM_FMTBIT_DSD_U32_LE)
436539
437540 static const unsigned int ak4458_rates[] = {
438541 8000, 11025, 16000, 22050,
....@@ -459,12 +562,13 @@
459562 return ret;
460563 }
461564
462
-static struct snd_soc_dai_ops ak4458_dai_ops = {
565
+static const struct snd_soc_dai_ops ak4458_dai_ops = {
463566 .startup = ak4458_startup,
464567 .hw_params = ak4458_hw_params,
465568 .set_fmt = ak4458_set_dai_fmt,
466
- .digital_mute = ak4458_set_dai_mute,
569
+ .mute_stream = ak4458_set_dai_mute,
467570 .set_tdm_slot = ak4458_set_tdm_slot,
571
+ .no_capture_mute = 1,
468572 };
469573
470574 static struct snd_soc_dai_driver ak4458_dai = {
....@@ -479,18 +583,22 @@
479583 .ops = &ak4458_dai_ops,
480584 };
481585
482
-static void ak4458_power_off(struct ak4458_priv *ak4458)
483
-{
484
- if (ak4458->reset_gpiod) {
485
- gpiod_set_value_cansleep(ak4458->reset_gpiod, 0);
486
- usleep_range(1000, 2000);
487
- }
488
-}
586
+static struct snd_soc_dai_driver ak4497_dai = {
587
+ .name = "ak4497-aif",
588
+ .playback = {
589
+ .stream_name = "Playback",
590
+ .channels_min = 1,
591
+ .channels_max = 2,
592
+ .rates = SNDRV_PCM_RATE_KNOT,
593
+ .formats = AK4458_FORMATS,
594
+ },
595
+ .ops = &ak4458_dai_ops,
596
+};
489597
490
-static void ak4458_power_on(struct ak4458_priv *ak4458)
598
+static void ak4458_reset(struct ak4458_priv *ak4458, bool active)
491599 {
492600 if (ak4458->reset_gpiod) {
493
- gpiod_set_value_cansleep(ak4458->reset_gpiod, 1);
601
+ gpiod_set_value_cansleep(ak4458->reset_gpiod, active);
494602 usleep_range(1000, 2000);
495603 }
496604 }
....@@ -504,12 +612,19 @@
504612 if (ak4458->mute_gpiod)
505613 gpiod_set_value_cansleep(ak4458->mute_gpiod, 1);
506614
507
- ak4458_power_on(ak4458);
615
+ ak4458_reset(ak4458, false);
508616
509617 ret = snd_soc_component_update_bits(component, AK4458_00_CONTROL1,
510618 0x80, 0x80); /* ACKS bit = 1; 10000000 */
511619 if (ret < 0)
512620 return ret;
621
+
622
+ if (ak4458->drvdata->type == AK4497) {
623
+ ret = snd_soc_component_update_bits(component, AK4458_09_DSD2,
624
+ 0x4, (ak4458->dsd_path << 2));
625
+ if (ret < 0)
626
+ return ret;
627
+ }
513628
514629 return ak4458_rstn_control(component, 1);
515630 }
....@@ -527,7 +642,7 @@
527642 {
528643 struct ak4458_priv *ak4458 = snd_soc_component_get_drvdata(component);
529644
530
- ak4458_power_off(ak4458);
645
+ ak4458_reset(ak4458, true);
531646 }
532647
533648 #ifdef CONFIG_PM
....@@ -537,23 +652,33 @@
537652
538653 regcache_cache_only(ak4458->regmap, true);
539654
540
- ak4458_power_off(ak4458);
655
+ ak4458_reset(ak4458, true);
541656
542657 if (ak4458->mute_gpiod)
543658 gpiod_set_value_cansleep(ak4458->mute_gpiod, 0);
544659
660
+ regulator_bulk_disable(ARRAY_SIZE(ak4458->supplies),
661
+ ak4458->supplies);
545662 return 0;
546663 }
547664
548665 static int __maybe_unused ak4458_runtime_resume(struct device *dev)
549666 {
550667 struct ak4458_priv *ak4458 = dev_get_drvdata(dev);
668
+ int ret;
669
+
670
+ ret = regulator_bulk_enable(ARRAY_SIZE(ak4458->supplies),
671
+ ak4458->supplies);
672
+ if (ret != 0) {
673
+ dev_err(ak4458->dev, "Failed to enable supplies: %d\n", ret);
674
+ return ret;
675
+ }
551676
552677 if (ak4458->mute_gpiod)
553678 gpiod_set_value_cansleep(ak4458->mute_gpiod, 1);
554679
555
- ak4458_power_off(ak4458);
556
- ak4458_power_on(ak4458);
680
+ ak4458_reset(ak4458, true);
681
+ ak4458_reset(ak4458, false);
557682
558683 regcache_cache_only(ak4458->regmap, false);
559684 regcache_mark_dirty(ak4458->regmap);
....@@ -577,6 +702,21 @@
577702 .non_legacy_dai_naming = 1,
578703 };
579704
705
+static const struct snd_soc_component_driver soc_codec_dev_ak4497 = {
706
+ .probe = ak4458_probe,
707
+ .remove = ak4458_remove,
708
+ .controls = ak4497_snd_controls,
709
+ .num_controls = ARRAY_SIZE(ak4497_snd_controls),
710
+ .dapm_widgets = ak4497_dapm_widgets,
711
+ .num_dapm_widgets = ARRAY_SIZE(ak4497_dapm_widgets),
712
+ .dapm_routes = ak4497_intercon,
713
+ .num_dapm_routes = ARRAY_SIZE(ak4497_intercon),
714
+ .idle_bias_on = 1,
715
+ .use_pmdown_time = 1,
716
+ .endianness = 1,
717
+ .non_legacy_dai_naming = 1,
718
+};
719
+
580720 static const struct regmap_config ak4458_regmap = {
581721 .reg_bits = 8,
582722 .val_bits = 8,
....@@ -585,6 +725,18 @@
585725 .reg_defaults = ak4458_reg_defaults,
586726 .num_reg_defaults = ARRAY_SIZE(ak4458_reg_defaults),
587727 .cache_type = REGCACHE_RBTREE,
728
+};
729
+
730
+static const struct ak4458_drvdata ak4458_drvdata = {
731
+ .dai_drv = &ak4458_dai,
732
+ .comp_drv = &soc_codec_dev_ak4458,
733
+ .type = AK4458,
734
+};
735
+
736
+static const struct ak4458_drvdata ak4497_drvdata = {
737
+ .dai_drv = &ak4497_dai,
738
+ .comp_drv = &soc_codec_dev_ak4497,
739
+ .type = AK4497,
588740 };
589741
590742 static const struct dev_pm_ops ak4458_pm = {
....@@ -596,7 +748,7 @@
596748 static int ak4458_i2c_probe(struct i2c_client *i2c)
597749 {
598750 struct ak4458_priv *ak4458;
599
- int ret;
751
+ int ret, i;
600752
601753 ak4458 = devm_kzalloc(&i2c->dev, sizeof(*ak4458), GFP_KERNEL);
602754 if (!ak4458)
....@@ -609,6 +761,8 @@
609761 i2c_set_clientdata(i2c, ak4458);
610762 ak4458->dev = &i2c->dev;
611763
764
+ ak4458->drvdata = of_device_get_match_data(&i2c->dev);
765
+
612766 ak4458->reset_gpiod = devm_gpiod_get_optional(ak4458->dev, "reset",
613767 GPIOD_OUT_LOW);
614768 if (IS_ERR(ak4458->reset_gpiod))
....@@ -619,14 +773,29 @@
619773 if (IS_ERR(ak4458->mute_gpiod))
620774 return PTR_ERR(ak4458->mute_gpiod);
621775
622
- ret = devm_snd_soc_register_component(ak4458->dev, &soc_codec_dev_ak4458,
623
- &ak4458_dai, 1);
776
+ /* Optional property for ak4497 */
777
+ of_property_read_u32(i2c->dev.of_node, "dsd-path", &ak4458->dsd_path);
778
+
779
+ for (i = 0; i < ARRAY_SIZE(ak4458->supplies); i++)
780
+ ak4458->supplies[i].supply = ak4458_supply_names[i];
781
+
782
+ ret = devm_regulator_bulk_get(ak4458->dev, ARRAY_SIZE(ak4458->supplies),
783
+ ak4458->supplies);
784
+ if (ret != 0) {
785
+ dev_err(ak4458->dev, "Failed to request supplies: %d\n", ret);
786
+ return ret;
787
+ }
788
+
789
+ ret = devm_snd_soc_register_component(ak4458->dev,
790
+ ak4458->drvdata->comp_drv,
791
+ ak4458->drvdata->dai_drv, 1);
624792 if (ret < 0) {
625793 dev_err(ak4458->dev, "Failed to register CODEC: %d\n", ret);
626794 return ret;
627795 }
628796
629797 pm_runtime_enable(&i2c->dev);
798
+ regcache_cache_only(ak4458->regmap, true);
630799
631800 return 0;
632801 }
....@@ -639,7 +808,8 @@
639808 }
640809
641810 static const struct of_device_id ak4458_of_match[] = {
642
- { .compatible = "asahi-kasei,ak4458", },
811
+ { .compatible = "asahi-kasei,ak4458", .data = &ak4458_drvdata},
812
+ { .compatible = "asahi-kasei,ak4497", .data = &ak4497_drvdata},
643813 { },
644814 };
645815 MODULE_DEVICE_TABLE(of, ak4458_of_match);