30
30
31
31
#include "nau8825.h"
32
32
33
+
34
+ #define NUVOTON_CODEC_DAI "nau8825-hifi"
35
+
33
36
#define NAU_FREF_MAX 13500000
34
37
#define NAU_FVCO_MAX 124000000
35
38
#define NAU_FVCO_MIN 90000000
36
39
40
+ static int nau8825_configure_sysclk (struct nau8825 * nau8825 ,
41
+ int clk_id , unsigned int freq );
42
+
37
43
struct nau8825_fll {
38
44
int mclk_src ;
39
45
int ratio ;
@@ -670,9 +676,6 @@ int nau8825_enable_jack_detect(struct snd_soc_codec *codec,
670
676
NAU8825_HSD_AUTO_MODE | NAU8825_SPKR_DWN1R | NAU8825_SPKR_DWN1L ,
671
677
NAU8825_HSD_AUTO_MODE | NAU8825_SPKR_DWN1R | NAU8825_SPKR_DWN1L );
672
678
673
- regmap_update_bits (regmap , NAU8825_REG_INTERRUPT_MASK ,
674
- NAU8825_IRQ_HEADSET_COMPLETE_EN | NAU8825_IRQ_EJECT_EN , 0 );
675
-
676
679
return 0 ;
677
680
}
678
681
EXPORT_SYMBOL_GPL (nau8825_enable_jack_detect );
@@ -688,16 +691,6 @@ static bool nau8825_is_jack_inserted(struct regmap *regmap)
688
691
689
692
static void nau8825_restart_jack_detection (struct regmap * regmap )
690
693
{
691
- /* Chip needs one FSCLK cycle in order to generate interrupts,
692
- * as we cannot guarantee one will be provided by the system. Turning
693
- * master mode on then off enables us to generate that FSCLK cycle
694
- * with a minimum of contention on the clock bus.
695
- */
696
- regmap_update_bits (regmap , NAU8825_REG_I2S_PCM_CTRL2 ,
697
- NAU8825_I2S_MS_MASK , NAU8825_I2S_MS_MASTER );
698
- regmap_update_bits (regmap , NAU8825_REG_I2S_PCM_CTRL2 ,
699
- NAU8825_I2S_MS_MASK , NAU8825_I2S_MS_SLAVE );
700
-
701
694
/* this will restart the entire jack detection process including MIC/GND
702
695
* switching and create interrupts. We have to go from 0 to 1 and back
703
696
* to 0 to restart.
@@ -708,6 +701,22 @@ static void nau8825_restart_jack_detection(struct regmap *regmap)
708
701
NAU8825_JACK_DET_RESTART , 0 );
709
702
}
710
703
704
+ static void nau8825_int_status_clear_all (struct regmap * regmap )
705
+ {
706
+ int active_irq , clear_irq , i ;
707
+
708
+ /* Reset the intrruption status from rightmost bit if the corres-
709
+ * ponding irq event occurs.
710
+ */
711
+ regmap_read (regmap , NAU8825_REG_IRQ_STATUS , & active_irq );
712
+ for (i = 0 ; i < NAU8825_REG_DATA_LEN ; i ++ ) {
713
+ clear_irq = (0x1 << i );
714
+ if (active_irq & clear_irq )
715
+ regmap_write (regmap ,
716
+ NAU8825_REG_INT_CLR_KEY_STATUS , clear_irq );
717
+ }
718
+ }
719
+
711
720
static void nau8825_eject_jack (struct nau8825 * nau8825 )
712
721
{
713
722
struct snd_soc_dapm_context * dapm = nau8825 -> dapm ;
@@ -722,6 +731,69 @@ static void nau8825_eject_jack(struct nau8825 *nau8825)
722
731
regmap_update_bits (regmap , NAU8825_REG_HSD_CTRL , 0xf , 0xf );
723
732
724
733
snd_soc_dapm_sync (dapm );
734
+
735
+ /* Clear all interruption status */
736
+ nau8825_int_status_clear_all (regmap );
737
+
738
+ /* Enable the insertion interruption, disable the ejection inter-
739
+ * ruption, and then bypass de-bounce circuit.
740
+ */
741
+ regmap_update_bits (regmap , NAU8825_REG_INTERRUPT_DIS_CTRL ,
742
+ NAU8825_IRQ_EJECT_DIS | NAU8825_IRQ_INSERT_DIS ,
743
+ NAU8825_IRQ_EJECT_DIS );
744
+ regmap_update_bits (regmap , NAU8825_REG_INTERRUPT_MASK ,
745
+ NAU8825_IRQ_OUTPUT_EN | NAU8825_IRQ_EJECT_EN |
746
+ NAU8825_IRQ_HEADSET_COMPLETE_EN | NAU8825_IRQ_INSERT_EN ,
747
+ NAU8825_IRQ_OUTPUT_EN | NAU8825_IRQ_EJECT_EN |
748
+ NAU8825_IRQ_HEADSET_COMPLETE_EN );
749
+ regmap_update_bits (regmap , NAU8825_REG_JACK_DET_CTRL ,
750
+ NAU8825_JACK_DET_DB_BYPASS , NAU8825_JACK_DET_DB_BYPASS );
751
+
752
+ /* Disable ADC needed for interruptions at audo mode */
753
+ regmap_update_bits (regmap , NAU8825_REG_ENA_CTRL ,
754
+ NAU8825_ENABLE_ADC , 0 );
755
+
756
+ /* Close clock for jack type detection at manual mode */
757
+ nau8825_configure_sysclk (nau8825 , NAU8825_CLK_DIS , 0 );
758
+ }
759
+
760
+ /* Enable audo mode interruptions with internal clock. */
761
+ static void nau8825_setup_auto_irq (struct nau8825 * nau8825 )
762
+ {
763
+ struct regmap * regmap = nau8825 -> regmap ;
764
+
765
+ /* Enable headset jack type detection complete interruption and
766
+ * jack ejection interruption.
767
+ */
768
+ regmap_update_bits (regmap , NAU8825_REG_INTERRUPT_MASK ,
769
+ NAU8825_IRQ_HEADSET_COMPLETE_EN | NAU8825_IRQ_EJECT_EN , 0 );
770
+
771
+ /* Enable internal VCO needed for interruptions */
772
+ nau8825_configure_sysclk (nau8825 , NAU8825_CLK_INTERNAL , 0 );
773
+
774
+ /* Enable ADC needed for interruptions */
775
+ regmap_update_bits (regmap , NAU8825_REG_ENA_CTRL ,
776
+ NAU8825_ENABLE_ADC , NAU8825_ENABLE_ADC );
777
+
778
+ /* Chip needs one FSCLK cycle in order to generate interruptions,
779
+ * as we cannot guarantee one will be provided by the system. Turning
780
+ * master mode on then off enables us to generate that FSCLK cycle
781
+ * with a minimum of contention on the clock bus.
782
+ */
783
+ regmap_update_bits (regmap , NAU8825_REG_I2S_PCM_CTRL2 ,
784
+ NAU8825_I2S_MS_MASK , NAU8825_I2S_MS_MASTER );
785
+ regmap_update_bits (regmap , NAU8825_REG_I2S_PCM_CTRL2 ,
786
+ NAU8825_I2S_MS_MASK , NAU8825_I2S_MS_SLAVE );
787
+
788
+ /* Not bypass de-bounce circuit */
789
+ regmap_update_bits (regmap , NAU8825_REG_JACK_DET_CTRL ,
790
+ NAU8825_JACK_DET_DB_BYPASS , 0 );
791
+
792
+ /* Unmask all interruptions */
793
+ regmap_write (regmap , NAU8825_REG_INTERRUPT_DIS_CTRL , 0 );
794
+
795
+ /* Restart the jack detection process at auto mode */
796
+ nau8825_restart_jack_detection (regmap );
725
797
}
726
798
727
799
static int nau8825_button_decode (int value )
@@ -858,6 +930,26 @@ static irqreturn_t nau8825_interrupt(int irq, void *data)
858
930
859
931
event_mask |= SND_JACK_HEADSET ;
860
932
clear_irq = NAU8825_HEADSET_COMPLETION_IRQ ;
933
+ } else if ((active_irq & NAU8825_JACK_INSERTION_IRQ_MASK ) ==
934
+ NAU8825_JACK_INSERTION_DETECTED ) {
935
+ /* One more step to check GPIO status directly. Thus, the
936
+ * driver can confirm the real insertion interruption because
937
+ * the intrruption at manual mode has bypassed debounce
938
+ * circuit which can get rid of unstable status.
939
+ */
940
+ if (nau8825_is_jack_inserted (regmap )) {
941
+ /* Turn off insertion interruption at manual mode */
942
+ regmap_update_bits (regmap ,
943
+ NAU8825_REG_INTERRUPT_DIS_CTRL ,
944
+ NAU8825_IRQ_INSERT_DIS ,
945
+ NAU8825_IRQ_INSERT_DIS );
946
+ regmap_update_bits (regmap , NAU8825_REG_INTERRUPT_MASK ,
947
+ NAU8825_IRQ_INSERT_EN , NAU8825_IRQ_INSERT_EN );
948
+ /* Enable interruption for jack type detection at audo
949
+ * mode which can detect microphone and jack type.
950
+ */
951
+ nau8825_setup_auto_irq (nau8825 );
952
+ }
861
953
}
862
954
863
955
if (!clear_irq )
@@ -1007,8 +1099,8 @@ static void nau8825_init_regs(struct nau8825 *nau8825)
1007
1099
}
1008
1100
1009
1101
static const struct regmap_config nau8825_regmap_config = {
1010
- .val_bits = 16 ,
1011
- .reg_bits = 16 ,
1102
+ .val_bits = NAU8825_REG_DATA_LEN ,
1103
+ .reg_bits = NAU8825_REG_ADDR_LEN ,
1012
1104
1013
1105
.max_register = NAU8825_REG_MAX ,
1014
1106
.readable_reg = nau8825_readable_reg ,
@@ -1027,12 +1119,6 @@ static int nau8825_codec_probe(struct snd_soc_codec *codec)
1027
1119
1028
1120
nau8825 -> dapm = dapm ;
1029
1121
1030
- /* Unmask interruptions. Handler uses dapm object so we can enable
1031
- * interruptions only after dapm is fully initialized.
1032
- */
1033
- regmap_write (nau8825 -> regmap , NAU8825_REG_INTERRUPT_DIS_CTRL , 0 );
1034
- nau8825_restart_jack_detection (nau8825 -> regmap );
1035
-
1036
1122
return 0 ;
1037
1123
}
1038
1124
@@ -1197,17 +1283,32 @@ static int nau8825_mclk_prepare(struct nau8825 *nau8825, unsigned int freq)
1197
1283
return 0 ;
1198
1284
}
1199
1285
1286
+ static void nau8825_configure_mclk_as_sysclk (struct regmap * regmap )
1287
+ {
1288
+ regmap_update_bits (regmap , NAU8825_REG_CLK_DIVIDER ,
1289
+ NAU8825_CLK_SRC_MASK , NAU8825_CLK_SRC_MCLK );
1290
+ regmap_update_bits (regmap , NAU8825_REG_FLL6 ,
1291
+ NAU8825_DCO_EN , 0 );
1292
+ }
1293
+
1200
1294
static int nau8825_configure_sysclk (struct nau8825 * nau8825 , int clk_id ,
1201
1295
unsigned int freq )
1202
1296
{
1203
1297
struct regmap * regmap = nau8825 -> regmap ;
1204
1298
int ret ;
1205
1299
1206
1300
switch (clk_id ) {
1301
+ case NAU8825_CLK_DIS :
1302
+ /* Clock provided externally and disable internal VCO clock */
1303
+ nau8825_configure_mclk_as_sysclk (regmap );
1304
+ if (nau8825 -> mclk_freq ) {
1305
+ clk_disable_unprepare (nau8825 -> mclk );
1306
+ nau8825 -> mclk_freq = 0 ;
1307
+ }
1308
+
1309
+ break ;
1207
1310
case NAU8825_CLK_MCLK :
1208
- regmap_update_bits (regmap , NAU8825_REG_CLK_DIVIDER ,
1209
- NAU8825_CLK_SRC_MASK , NAU8825_CLK_SRC_MCLK );
1210
- regmap_update_bits (regmap , NAU8825_REG_FLL6 , NAU8825_DCO_EN , 0 );
1311
+ nau8825_configure_mclk_as_sysclk (regmap );
1211
1312
/* MCLK not changed by clock tree */
1212
1313
regmap_update_bits (regmap , NAU8825_REG_CLK_DIVIDER ,
1213
1314
NAU8825_CLK_MCLK_SRC_MASK , 0 );
@@ -1217,17 +1318,25 @@ static int nau8825_configure_sysclk(struct nau8825 *nau8825, int clk_id,
1217
1318
1218
1319
break ;
1219
1320
case NAU8825_CLK_INTERNAL :
1220
- regmap_update_bits (regmap , NAU8825_REG_FLL6 , NAU8825_DCO_EN ,
1221
- NAU8825_DCO_EN );
1222
- regmap_update_bits (regmap , NAU8825_REG_CLK_DIVIDER ,
1223
- NAU8825_CLK_SRC_MASK , NAU8825_CLK_SRC_VCO );
1224
- /* Decrease the VCO frequency for power saving */
1225
- regmap_update_bits (regmap , NAU8825_REG_CLK_DIVIDER ,
1226
- NAU8825_CLK_MCLK_SRC_MASK , 0xf );
1227
- regmap_update_bits (regmap , NAU8825_REG_FLL1 ,
1228
- NAU8825_FLL_RATIO_MASK , 0x10 );
1229
- regmap_update_bits (regmap , NAU8825_REG_FLL6 ,
1230
- NAU8825_SDM_EN , NAU8825_SDM_EN );
1321
+ if (nau8825_is_jack_inserted (nau8825 -> regmap )) {
1322
+ regmap_update_bits (regmap , NAU8825_REG_FLL6 ,
1323
+ NAU8825_DCO_EN , NAU8825_DCO_EN );
1324
+ regmap_update_bits (regmap , NAU8825_REG_CLK_DIVIDER ,
1325
+ NAU8825_CLK_SRC_MASK , NAU8825_CLK_SRC_VCO );
1326
+ /* Decrease the VCO frequency for power saving */
1327
+ regmap_update_bits (regmap , NAU8825_REG_CLK_DIVIDER ,
1328
+ NAU8825_CLK_MCLK_SRC_MASK , 0xf );
1329
+ regmap_update_bits (regmap , NAU8825_REG_FLL1 ,
1330
+ NAU8825_FLL_RATIO_MASK , 0x10 );
1331
+ regmap_update_bits (regmap , NAU8825_REG_FLL6 ,
1332
+ NAU8825_SDM_EN , NAU8825_SDM_EN );
1333
+ } else {
1334
+ /* The clock turns off intentionally for power saving
1335
+ * when no headset connected.
1336
+ */
1337
+ nau8825_configure_mclk_as_sysclk (regmap );
1338
+ dev_warn (nau8825 -> dev , "Disable clock for power saving when no headset connected\n" );
1339
+ }
1231
1340
if (nau8825 -> mclk_freq ) {
1232
1341
clk_disable_unprepare (nau8825 -> mclk );
1233
1342
nau8825 -> mclk_freq = 0 ;
@@ -1278,6 +1387,31 @@ static int nau8825_set_sysclk(struct snd_soc_codec *codec, int clk_id,
1278
1387
return nau8825_configure_sysclk (nau8825 , clk_id , freq );
1279
1388
}
1280
1389
1390
+ static int nau8825_resume_setup (struct nau8825 * nau8825 )
1391
+ {
1392
+ struct regmap * regmap = nau8825 -> regmap ;
1393
+
1394
+ /* Close clock when jack type detection at manual mode */
1395
+ nau8825_configure_sysclk (nau8825 , NAU8825_CLK_DIS , 0 );
1396
+
1397
+ /* Clear all interruption status */
1398
+ nau8825_int_status_clear_all (regmap );
1399
+
1400
+ /* Enable both insertion and ejection interruptions, and then
1401
+ * bypass de-bounce circuit.
1402
+ */
1403
+ regmap_update_bits (regmap , NAU8825_REG_INTERRUPT_MASK ,
1404
+ NAU8825_IRQ_OUTPUT_EN | NAU8825_IRQ_HEADSET_COMPLETE_EN |
1405
+ NAU8825_IRQ_EJECT_EN | NAU8825_IRQ_INSERT_EN ,
1406
+ NAU8825_IRQ_OUTPUT_EN | NAU8825_IRQ_HEADSET_COMPLETE_EN );
1407
+ regmap_update_bits (regmap , NAU8825_REG_JACK_DET_CTRL ,
1408
+ NAU8825_JACK_DET_DB_BYPASS , NAU8825_JACK_DET_DB_BYPASS );
1409
+ regmap_update_bits (regmap , NAU8825_REG_INTERRUPT_DIS_CTRL ,
1410
+ NAU8825_IRQ_INSERT_DIS | NAU8825_IRQ_EJECT_DIS , 0 );
1411
+
1412
+ return 0 ;
1413
+ }
1414
+
1281
1415
static int nau8825_set_bias_level (struct snd_soc_codec * codec ,
1282
1416
enum snd_soc_bias_level level )
1283
1417
{
@@ -1300,10 +1434,20 @@ static int nau8825_set_bias_level(struct snd_soc_codec *codec,
1300
1434
return ret ;
1301
1435
}
1302
1436
}
1437
+ /* Setup codec configuration after resume */
1438
+ nau8825_resume_setup (nau8825 );
1303
1439
}
1304
1440
break ;
1305
1441
1306
1442
case SND_SOC_BIAS_OFF :
1443
+ /* Turn off all interruptions before system shutdown. Keep the
1444
+ * interruption quiet before resume setup completes.
1445
+ */
1446
+ regmap_write (nau8825 -> regmap ,
1447
+ NAU8825_REG_INTERRUPT_DIS_CTRL , 0xffff );
1448
+ /* Disable ADC needed for interruptions at audo mode */
1449
+ regmap_update_bits (nau8825 -> regmap , NAU8825_REG_ENA_CTRL ,
1450
+ NAU8825_ENABLE_ADC , 0 );
1307
1451
if (nau8825 -> mclk_freq )
1308
1452
clk_disable_unprepare (nau8825 -> mclk );
1309
1453
break ;
@@ -1317,6 +1461,7 @@ static int nau8825_suspend(struct snd_soc_codec *codec)
1317
1461
struct nau8825 * nau8825 = snd_soc_codec_get_drvdata (codec );
1318
1462
1319
1463
disable_irq (nau8825 -> irq );
1464
+ snd_soc_codec_force_bias_level (codec , SND_SOC_BIAS_OFF );
1320
1465
regcache_cache_only (nau8825 -> regmap , true);
1321
1466
regcache_mark_dirty (nau8825 -> regmap );
1322
1467
@@ -1327,32 +1472,10 @@ static int nau8825_resume(struct snd_soc_codec *codec)
1327
1472
{
1328
1473
struct nau8825 * nau8825 = snd_soc_codec_get_drvdata (codec );
1329
1474
1330
- /* The chip may lose power and reset in S3. regcache_sync restores
1331
- * register values including configurations for sysclk, irq, and
1332
- * jack/button detection.
1333
- */
1334
1475
regcache_cache_only (nau8825 -> regmap , false);
1335
1476
regcache_sync (nau8825 -> regmap );
1336
-
1337
- /* Check the jack plug status directly. If the headset is unplugged
1338
- * during S3 when the chip has no power, there will be no jack
1339
- * detection irq even after the nau8825_restart_jack_detection below,
1340
- * because the chip just thinks no headset has ever been plugged in.
1341
- */
1342
- if (!nau8825_is_jack_inserted (nau8825 -> regmap )) {
1343
- nau8825_eject_jack (nau8825 );
1344
- snd_soc_jack_report (nau8825 -> jack , 0 , SND_JACK_HEADSET );
1345
- }
1346
-
1347
1477
enable_irq (nau8825 -> irq );
1348
1478
1349
- /* Run jack detection to check the type (OMTP or CTIA) of the headset
1350
- * if there is one. This handles the case where a different type of
1351
- * headset is plugged in during S3. This triggers an IRQ iff a headset
1352
- * is already plugged in.
1353
- */
1354
- nau8825_restart_jack_detection (nau8825 -> regmap );
1355
-
1356
1479
return 0 ;
1357
1480
}
1358
1481
#else
@@ -1461,20 +1584,8 @@ static int nau8825_read_device_properties(struct device *dev,
1461
1584
1462
1585
static int nau8825_setup_irq (struct nau8825 * nau8825 )
1463
1586
{
1464
- struct regmap * regmap = nau8825 -> regmap ;
1465
1587
int ret ;
1466
1588
1467
- /* IRQ Output Enable */
1468
- regmap_update_bits (regmap , NAU8825_REG_INTERRUPT_MASK ,
1469
- NAU8825_IRQ_OUTPUT_EN , NAU8825_IRQ_OUTPUT_EN );
1470
-
1471
- /* Enable internal VCO needed for interruptions */
1472
- nau8825_configure_sysclk (nau8825 , NAU8825_CLK_INTERNAL , 0 );
1473
-
1474
- /* Enable ADC needed for interrupts */
1475
- regmap_update_bits (regmap , NAU8825_REG_ENA_CTRL ,
1476
- NAU8825_ENABLE_ADC , NAU8825_ENABLE_ADC );
1477
-
1478
1589
ret = devm_request_threaded_irq (nau8825 -> dev , nau8825 -> irq , NULL ,
1479
1590
nau8825_interrupt , IRQF_TRIGGER_LOW | IRQF_ONESHOT ,
1480
1591
"nau8825" , nau8825 );
0 commit comments