forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-11 1f93a7dfd1f8d5ff7a5c53246c7534fe2332d6f4
kernel/sound/soc/codecs/tlv320aic31xx.c
....@@ -2,7 +2,7 @@
22 /*
33 * ALSA SoC TLV320AIC31xx CODEC Driver
44 *
5
- * Copyright (C) 2014-2017 Texas Instruments Incorporated - http://www.ti.com/
5
+ * Copyright (C) 2014-2017 Texas Instruments Incorporated - https://www.ti.com/
66 * Jyri Sarha <jsarha@ti.com>
77 *
88 * Based on ground work by: Ajit Kulkarni <x0175765@ti.com>
....@@ -25,6 +25,7 @@
2525 #include <linux/of_gpio.h>
2626 #include <linux/slab.h>
2727 #include <sound/core.h>
28
+#include <sound/jack.h>
2829 #include <sound/pcm.h>
2930 #include <sound/pcm_params.h>
3031 #include <sound/soc.h>
....@@ -33,6 +34,9 @@
3334 #include <dt-bindings/sound/tlv320aic31xx-micbias.h>
3435
3536 #include "tlv320aic31xx.h"
37
+
38
+static int aic31xx_set_jack(struct snd_soc_component *component,
39
+ struct snd_soc_jack *jack, void *data);
3640
3741 static const struct reg_default aic31xx_reg_defaults[] = {
3842 { AIC31XX_CLKMUX, 0x00 },
....@@ -89,6 +93,7 @@
8993 case AIC31XX_INTRADCFLAG: /* Sticky interrupt flags */
9094 case AIC31XX_INTRDACFLAG2:
9195 case AIC31XX_INTRADCFLAG2:
96
+ case AIC31XX_HSDETECT:
9297 return true;
9398 }
9499 return false;
....@@ -163,10 +168,13 @@
163168 struct aic31xx_pdata pdata;
164169 struct regulator_bulk_data supplies[AIC31XX_NUM_SUPPLIES];
165170 struct aic31xx_disable_nb disable_nb[AIC31XX_NUM_SUPPLIES];
171
+ struct snd_soc_jack *jack;
166172 unsigned int sysclk;
167173 u8 p_div;
168174 int rate_div_line;
169175 bool master_dapm_route_applied;
176
+ int irq;
177
+ u8 ocmv; /* output common-mode voltage */
170178 };
171179
172180 struct aic31xx_rate_divs {
....@@ -254,9 +262,27 @@
254262 static SOC_ENUM_SINGLE_DECL(mic1lm_p_enum, AIC31XX_MICPGAPI, 2,
255263 mic_select_text);
256264
257
-static SOC_ENUM_SINGLE_DECL(cm_m_enum, AIC31XX_MICPGAMI, 6, mic_select_text);
258265 static SOC_ENUM_SINGLE_DECL(mic1lm_m_enum, AIC31XX_MICPGAMI, 4,
259266 mic_select_text);
267
+
268
+static const char * const hp_poweron_time_text[] = {
269
+ "0us", "15.3us", "153us", "1.53ms", "15.3ms", "76.2ms",
270
+ "153ms", "304ms", "610ms", "1.22s", "3.04s", "6.1s" };
271
+
272
+static SOC_ENUM_SINGLE_DECL(hp_poweron_time_enum, AIC31XX_HPPOP, 3,
273
+ hp_poweron_time_text);
274
+
275
+static const char * const hp_rampup_step_text[] = {
276
+ "0ms", "0.98ms", "1.95ms", "3.9ms" };
277
+
278
+static SOC_ENUM_SINGLE_DECL(hp_rampup_step_enum, AIC31XX_HPPOP, 1,
279
+ hp_rampup_step_text);
280
+
281
+static const char * const vol_soft_step_mode_text[] = {
282
+ "fast", "slow", "disabled" };
283
+
284
+static SOC_ENUM_SINGLE_DECL(vol_soft_step_mode_enum, AIC31XX_DACSETUP, 0,
285
+ vol_soft_step_mode_text);
260286
261287 static const DECLARE_TLV_DB_SCALE(dac_vol_tlv, -6350, 50, 0);
262288 static const DECLARE_TLV_DB_SCALE(adc_fgain_tlv, 0, 10, 0);
....@@ -281,6 +307,16 @@
281307
282308 SOC_DOUBLE_R_TLV("HP Analog Playback Volume", AIC31XX_LANALOGHPL,
283309 AIC31XX_RANALOGHPR, 0, 0x7F, 1, hp_vol_tlv),
310
+
311
+ /* HP de-pop control: apply power not immediately but via ramp
312
+ * function with these psarameters. Note that power up sequence
313
+ * has to wait for this to complete; this is implemented by
314
+ * polling HP driver status in aic31xx_dapm_power_event()
315
+ */
316
+ SOC_ENUM("HP Output Driver Power-On time", hp_poweron_time_enum),
317
+ SOC_ENUM("HP Output Driver Ramp-up step", hp_rampup_step_enum),
318
+
319
+ SOC_ENUM("Volume Soft Stepping", vol_soft_step_mode_enum),
284320 };
285321
286322 static const struct snd_kcontrol_new aic31xx_snd_controls[] = {
....@@ -353,6 +389,7 @@
353389 struct aic31xx_priv *aic31xx = snd_soc_component_get_drvdata(component);
354390 unsigned int reg = AIC31XX_DACFLAG1;
355391 unsigned int mask;
392
+ unsigned int timeout = 500 * USEC_PER_MSEC;
356393
357394 switch (WIDGET_BIT(w->reg, w->shift)) {
358395 case WIDGET_BIT(AIC31XX_DACSETUP, 7):
....@@ -363,9 +400,13 @@
363400 break;
364401 case WIDGET_BIT(AIC31XX_HPDRIVER, 7):
365402 mask = AIC31XX_HPLDRVPWRSTATUS_MASK;
403
+ if (event == SND_SOC_DAPM_POST_PMU)
404
+ timeout = 7 * USEC_PER_SEC;
366405 break;
367406 case WIDGET_BIT(AIC31XX_HPDRIVER, 6):
368407 mask = AIC31XX_HPRDRVPWRSTATUS_MASK;
408
+ if (event == SND_SOC_DAPM_POST_PMU)
409
+ timeout = 7 * USEC_PER_SEC;
369410 break;
370411 case WIDGET_BIT(AIC31XX_SPKAMP, 7):
371412 mask = AIC31XX_SPLDRVPWRSTATUS_MASK;
....@@ -385,9 +426,11 @@
385426
386427 switch (event) {
387428 case SND_SOC_DAPM_POST_PMU:
388
- return aic31xx_wait_bits(aic31xx, reg, mask, mask, 5000, 100);
429
+ return aic31xx_wait_bits(aic31xx, reg, mask, mask,
430
+ 5000, timeout / 5000);
389431 case SND_SOC_DAPM_POST_PMD:
390
- return aic31xx_wait_bits(aic31xx, reg, mask, 0, 5000, 100);
432
+ return aic31xx_wait_bits(aic31xx, reg, mask, 0,
433
+ 5000, timeout / 5000);
391434 default:
392435 dev_dbg(component->dev,
393436 "Unhandled dapm widget event %d from %s\n",
....@@ -837,7 +880,7 @@
837880 there may be trouble. To fix the issue edit the
838881 aic31xx_divs table for your mclk and sample
839882 rate. Details can be found from:
840
- http://www.ti.com/lit/ds/symlink/tlv320aic3100.pdf
883
+ https://www.ti.com/lit/ds/symlink/tlv320aic3100.pdf
841884 Section: 5.6 CLOCK Generation and PLL
842885 */
843886 }
....@@ -932,7 +975,8 @@
932975 return aic31xx_setup_pll(component, params);
933976 }
934977
935
-static int aic31xx_dac_mute(struct snd_soc_dai *codec_dai, int mute)
978
+static int aic31xx_dac_mute(struct snd_soc_dai *codec_dai, int mute,
979
+ int direction)
936980 {
937981 struct snd_soc_component *component = codec_dai->component;
938982
....@@ -1040,7 +1084,8 @@
10401084 case SND_SOC_DAIFMT_I2S:
10411085 break;
10421086 case SND_SOC_DAIFMT_DSP_A:
1043
- dsp_a_val = 0x1; /* fall through */
1087
+ dsp_a_val = 0x1;
1088
+ fallthrough;
10441089 case SND_SOC_DAIFMT_DSP_B:
10451090 /*
10461091 * NOTE: This CODEC samples on the falling edge of BCLK in
....@@ -1094,7 +1139,7 @@
10941139 if (freq/i > 20000000) {
10951140 dev_err(aic31xx->dev, "%s: Too high mclk frequency %u\n",
10961141 __func__, freq);
1097
- return -EINVAL;
1142
+ return -EINVAL;
10981143 }
10991144 aic31xx->p_div = i;
11001145
....@@ -1214,6 +1259,13 @@
12141259 return ret;
12151260 }
12161261
1262
+ /*
1263
+ * The jack detection configuration is in the same register
1264
+ * that is used to report jack detect status so is volatile
1265
+ * and not covered by the cache sync, restore it separately.
1266
+ */
1267
+ aic31xx_set_jack(component, aic31xx->jack, NULL);
1268
+
12171269 return 0;
12181270 }
12191271
....@@ -1260,6 +1312,20 @@
12601312 return 0;
12611313 }
12621314
1315
+static int aic31xx_set_jack(struct snd_soc_component *component,
1316
+ struct snd_soc_jack *jack, void *data)
1317
+{
1318
+ struct aic31xx_priv *aic31xx = snd_soc_component_get_drvdata(component);
1319
+
1320
+ aic31xx->jack = jack;
1321
+
1322
+ /* Enable/Disable jack detection */
1323
+ regmap_write(aic31xx->regmap, AIC31XX_HSDETECT,
1324
+ jack ? AIC31XX_HSD_ENABLE : 0);
1325
+
1326
+ return 0;
1327
+}
1328
+
12631329 static int aic31xx_codec_probe(struct snd_soc_component *component)
12641330 {
12651331 struct aic31xx_priv *aic31xx = snd_soc_component_get_drvdata(component);
....@@ -1273,8 +1339,9 @@
12731339 aic31xx->disable_nb[i].nb.notifier_call =
12741340 aic31xx_regulator_event;
12751341 aic31xx->disable_nb[i].aic31xx = aic31xx;
1276
- ret = regulator_register_notifier(aic31xx->supplies[i].consumer,
1277
- &aic31xx->disable_nb[i].nb);
1342
+ ret = devm_regulator_register_notifier(
1343
+ aic31xx->supplies[i].consumer,
1344
+ &aic31xx->disable_nb[i].nb);
12781345 if (ret) {
12791346 dev_err(component->dev,
12801347 "Failed to request regulator notifier: %d\n",
....@@ -1294,22 +1361,17 @@
12941361 if (ret)
12951362 return ret;
12961363
1364
+ /* set output common-mode voltage */
1365
+ snd_soc_component_update_bits(component, AIC31XX_HPDRIVER,
1366
+ AIC31XX_HPD_OCMV_MASK,
1367
+ aic31xx->ocmv << AIC31XX_HPD_OCMV_SHIFT);
1368
+
12971369 return 0;
1298
-}
1299
-
1300
-static void aic31xx_codec_remove(struct snd_soc_component *component)
1301
-{
1302
- struct aic31xx_priv *aic31xx = snd_soc_component_get_drvdata(component);
1303
- int i;
1304
-
1305
- for (i = 0; i < ARRAY_SIZE(aic31xx->supplies); i++)
1306
- regulator_unregister_notifier(aic31xx->supplies[i].consumer,
1307
- &aic31xx->disable_nb[i].nb);
13081370 }
13091371
13101372 static const struct snd_soc_component_driver soc_codec_driver_aic31xx = {
13111373 .probe = aic31xx_codec_probe,
1312
- .remove = aic31xx_codec_remove,
1374
+ .set_jack = aic31xx_set_jack,
13131375 .set_bias_level = aic31xx_set_bias_level,
13141376 .controls = common31xx_snd_controls,
13151377 .num_controls = ARRAY_SIZE(common31xx_snd_controls),
....@@ -1328,7 +1390,8 @@
13281390 .hw_params = aic31xx_hw_params,
13291391 .set_sysclk = aic31xx_set_dai_sysclk,
13301392 .set_fmt = aic31xx_set_dai_fmt,
1331
- .digital_mute = aic31xx_dac_mute,
1393
+ .mute_stream = aic31xx_dac_mute,
1394
+ .no_capture_mute = 1,
13321395 };
13331396
13341397 static struct snd_soc_dai_driver dac31xx_dai_driver[] = {
....@@ -1391,6 +1454,145 @@
13911454 MODULE_DEVICE_TABLE(acpi, aic31xx_acpi_match);
13921455 #endif
13931456
1457
+static irqreturn_t aic31xx_irq(int irq, void *data)
1458
+{
1459
+ struct aic31xx_priv *aic31xx = data;
1460
+ struct device *dev = aic31xx->dev;
1461
+ unsigned int value;
1462
+ bool handled = false;
1463
+ int ret;
1464
+
1465
+ ret = regmap_read(aic31xx->regmap, AIC31XX_INTRDACFLAG, &value);
1466
+ if (ret) {
1467
+ dev_err(dev, "Failed to read interrupt mask: %d\n", ret);
1468
+ goto exit;
1469
+ }
1470
+
1471
+ if (value)
1472
+ handled = true;
1473
+ else
1474
+ goto read_overflow;
1475
+
1476
+ if (value & AIC31XX_HPLSCDETECT)
1477
+ dev_err(dev, "Short circuit on Left output is detected\n");
1478
+ if (value & AIC31XX_HPRSCDETECT)
1479
+ dev_err(dev, "Short circuit on Right output is detected\n");
1480
+ if (value & (AIC31XX_HSPLUG | AIC31XX_BUTTONPRESS)) {
1481
+ unsigned int val;
1482
+ int status = 0;
1483
+
1484
+ ret = regmap_read(aic31xx->regmap, AIC31XX_INTRDACFLAG2,
1485
+ &val);
1486
+ if (ret) {
1487
+ dev_err(dev, "Failed to read interrupt mask: %d\n",
1488
+ ret);
1489
+ goto exit;
1490
+ }
1491
+
1492
+ if (val & AIC31XX_BUTTONPRESS)
1493
+ status |= SND_JACK_BTN_0;
1494
+
1495
+ ret = regmap_read(aic31xx->regmap, AIC31XX_HSDETECT, &val);
1496
+ if (ret) {
1497
+ dev_err(dev, "Failed to read headset type: %d\n", ret);
1498
+ goto exit;
1499
+ }
1500
+
1501
+ switch ((val & AIC31XX_HSD_TYPE_MASK) >>
1502
+ AIC31XX_HSD_TYPE_SHIFT) {
1503
+ case AIC31XX_HSD_HP:
1504
+ status |= SND_JACK_HEADPHONE;
1505
+ break;
1506
+ case AIC31XX_HSD_HS:
1507
+ status |= SND_JACK_HEADSET;
1508
+ break;
1509
+ default:
1510
+ break;
1511
+ }
1512
+
1513
+ if (aic31xx->jack)
1514
+ snd_soc_jack_report(aic31xx->jack, status,
1515
+ AIC31XX_JACK_MASK);
1516
+ }
1517
+ if (value & ~(AIC31XX_HPLSCDETECT |
1518
+ AIC31XX_HPRSCDETECT |
1519
+ AIC31XX_HSPLUG |
1520
+ AIC31XX_BUTTONPRESS))
1521
+ dev_err(dev, "Unknown DAC interrupt flags: 0x%08x\n", value);
1522
+
1523
+read_overflow:
1524
+ ret = regmap_read(aic31xx->regmap, AIC31XX_OFFLAG, &value);
1525
+ if (ret) {
1526
+ dev_err(dev, "Failed to read overflow flag: %d\n", ret);
1527
+ goto exit;
1528
+ }
1529
+
1530
+ if (value)
1531
+ handled = true;
1532
+ else
1533
+ goto exit;
1534
+
1535
+ if (value & AIC31XX_DAC_OF_LEFT)
1536
+ dev_warn(dev, "Left-channel DAC overflow has occurred\n");
1537
+ if (value & AIC31XX_DAC_OF_RIGHT)
1538
+ dev_warn(dev, "Right-channel DAC overflow has occurred\n");
1539
+ if (value & AIC31XX_DAC_OF_SHIFTER)
1540
+ dev_warn(dev, "DAC barrel shifter overflow has occurred\n");
1541
+ if (value & AIC31XX_ADC_OF)
1542
+ dev_warn(dev, "ADC overflow has occurred\n");
1543
+ if (value & AIC31XX_ADC_OF_SHIFTER)
1544
+ dev_warn(dev, "ADC barrel shifter overflow has occurred\n");
1545
+ if (value & ~(AIC31XX_DAC_OF_LEFT |
1546
+ AIC31XX_DAC_OF_RIGHT |
1547
+ AIC31XX_DAC_OF_SHIFTER |
1548
+ AIC31XX_ADC_OF |
1549
+ AIC31XX_ADC_OF_SHIFTER))
1550
+ dev_warn(dev, "Unknown overflow interrupt flags: 0x%08x\n", value);
1551
+
1552
+exit:
1553
+ if (handled)
1554
+ return IRQ_HANDLED;
1555
+ else
1556
+ return IRQ_NONE;
1557
+}
1558
+
1559
+static void aic31xx_configure_ocmv(struct aic31xx_priv *priv)
1560
+{
1561
+ struct device *dev = priv->dev;
1562
+ int dvdd, avdd;
1563
+ u32 value;
1564
+
1565
+ if (dev->fwnode &&
1566
+ fwnode_property_read_u32(dev->fwnode, "ai31xx-ocmv", &value)) {
1567
+ /* OCMV setting is forced by DT */
1568
+ if (value <= 3) {
1569
+ priv->ocmv = value;
1570
+ return;
1571
+ }
1572
+ }
1573
+
1574
+ avdd = regulator_get_voltage(priv->supplies[3].consumer);
1575
+ dvdd = regulator_get_voltage(priv->supplies[5].consumer);
1576
+
1577
+ if (avdd > 3600000 || dvdd > 1950000) {
1578
+ dev_warn(dev,
1579
+ "Too high supply voltage(s) AVDD: %d, DVDD: %d\n",
1580
+ avdd, dvdd);
1581
+ } else if (avdd == 3600000 && dvdd == 1950000) {
1582
+ priv->ocmv = AIC31XX_HPD_OCMV_1_8V;
1583
+ } else if (avdd >= 3300000 && dvdd >= 1800000) {
1584
+ priv->ocmv = AIC31XX_HPD_OCMV_1_65V;
1585
+ } else if (avdd >= 3000000 && dvdd >= 1650000) {
1586
+ priv->ocmv = AIC31XX_HPD_OCMV_1_5V;
1587
+ } else if (avdd >= 2700000 && dvdd >= 1525000) {
1588
+ priv->ocmv = AIC31XX_HPD_OCMV_1_35V;
1589
+ } else {
1590
+ dev_warn(dev,
1591
+ "Invalid supply voltage(s) AVDD: %d, DVDD: %d\n",
1592
+ avdd, dvdd);
1593
+ }
1594
+}
1595
+
13941596 static int aic31xx_i2c_probe(struct i2c_client *i2c,
13951597 const struct i2c_device_id *id)
13961598 {
....@@ -1413,6 +1615,7 @@
14131615 return ret;
14141616 }
14151617 aic31xx->dev = &i2c->dev;
1618
+ aic31xx->irq = i2c->irq;
14161619
14171620 aic31xx->codec_type = id->driver_data;
14181621
....@@ -1459,6 +1662,30 @@
14591662 return ret;
14601663 }
14611664
1665
+ aic31xx_configure_ocmv(aic31xx);
1666
+
1667
+ if (aic31xx->irq > 0) {
1668
+ regmap_update_bits(aic31xx->regmap, AIC31XX_GPIO1,
1669
+ AIC31XX_GPIO1_FUNC_MASK,
1670
+ AIC31XX_GPIO1_INT1 <<
1671
+ AIC31XX_GPIO1_FUNC_SHIFT);
1672
+
1673
+ regmap_write(aic31xx->regmap, AIC31XX_INT1CTRL,
1674
+ AIC31XX_HSPLUGDET |
1675
+ AIC31XX_BUTTONPRESSDET |
1676
+ AIC31XX_SC |
1677
+ AIC31XX_ENGINE);
1678
+
1679
+ ret = devm_request_threaded_irq(aic31xx->dev, aic31xx->irq,
1680
+ NULL, aic31xx_irq,
1681
+ IRQF_ONESHOT, "aic31xx-irq",
1682
+ aic31xx);
1683
+ if (ret) {
1684
+ dev_err(aic31xx->dev, "Unable to request IRQ\n");
1685
+ return ret;
1686
+ }
1687
+ }
1688
+
14621689 if (aic31xx->codec_type & DAC31XX_BIT)
14631690 return devm_snd_soc_register_component(&i2c->dev,
14641691 &soc_codec_driver_aic31xx,