bcm27xx: add support for linux v5.15
[openwrt/staging/noltari.git] / target / linux / bcm27xx / patches-5.15 / 950-0527-Hifiberry-DAC-ADCPro-adding-optional-headphone-amp-c.patch
1 From 74de23a947ad5964b25e9f9f15329d3174488fea Mon Sep 17 00:00:00 2001
2 From: Joerg Schambacher <joerg@hifiberry.com>
3 Date: Wed, 6 Oct 2021 17:21:07 +0200
4 Subject: [PATCH] Hifiberry DAC+ADCPro: adding optional headphone amp
5 control
6
7 This is a copy of the code and approach from our DAC+ driver.
8 It allows to probe (and activate) an optional TPA6130A2 headphone
9 amplifier. Updated email address.
10
11 Signed-off-by: Joerg Schambacher <joerg@hifiberry.com>
12 ---
13 sound/soc/bcm/Kconfig | 3 +-
14 sound/soc/bcm/hifiberry_dacplusadcpro.c | 74 ++++++++++++++++++++++++-
15 2 files changed, 73 insertions(+), 4 deletions(-)
16
17 --- a/sound/soc/bcm/Kconfig
18 +++ b/sound/soc/bcm/Kconfig
19 @@ -79,7 +79,8 @@ config SND_BCM2708_SOC_HIFIBERRY_DACPLUS
20 tristate "Support for HifiBerry DAC+ADC PRO"
21 depends on SND_BCM2708_SOC_I2S || SND_BCM2835_SOC_I2S
22 select SND_SOC_PCM512x_I2C
23 - select SND_SOC_PCM186X_I2C
24 + select SND_SOC_PCM186X_I2C
25 + select SND_SOC_TPA6130A2
26 select COMMON_CLK_HIFIBERRY_DACPRO
27 help
28 Say Y or M if you want to add support for HifiBerry DAC+ADC PRO.
29 --- a/sound/soc/bcm/hifiberry_dacplusadcpro.c
30 +++ b/sound/soc/bcm/hifiberry_dacplusadcpro.c
31 @@ -4,8 +4,8 @@
32 * Author: Daniel Matuschek, Stuart MacLean <stuart@hifiberry.com>
33 * Copyright 2014-2015
34 * based on code by Florian Meier <florian.meier@koalo.de>
35 - * ADC added by Joerg Schambacher <joerg@i2audio.com>
36 - * Copyright 2018-19
37 + * ADC, HP added by Joerg Schambacher <joerg@hifiberry.com>
38 + * Copyright 2018-21
39 *
40 * This program is free software; you can redistribute it and/or
41 * modify it under the terms of the GNU General Public License
42 @@ -26,6 +26,7 @@
43 #include <linux/of.h>
44 #include <linux/slab.h>
45 #include <linux/delay.h>
46 +#include <linux/i2c.h>
47
48 #include <sound/core.h>
49 #include <sound/pcm.h>
50 @@ -468,6 +469,15 @@ static struct snd_soc_dai_link snd_rpi_h
51 },
52 };
53
54 +/* aux device for optional headphone amp */
55 +static struct snd_soc_aux_dev hifiberry_dacplusadcpro_aux_devs[] = {
56 + {
57 + .dlc = {
58 + .name = "tpa6130a2.1-0060",
59 + },
60 + },
61 +};
62 +
63 /* audio machine driver */
64 static struct snd_soc_card snd_rpi_hifiberry_dacplusadcpro = {
65 .name = "snd_rpi_hifiberry_dacplusadcpro",
66 @@ -477,10 +487,68 @@ static struct snd_soc_card snd_rpi_hifib
67 .num_links = ARRAY_SIZE(snd_rpi_hifiberry_dacplusadcpro_dai),
68 };
69
70 +static int hb_hp_detect(void)
71 +{
72 + struct i2c_adapter *adap = i2c_get_adapter(1);
73 + int ret;
74 + struct i2c_client tpa_i2c_client = {
75 + .addr = 0x60,
76 + .adapter = adap,
77 + };
78 +
79 + if (!adap)
80 + return -EPROBE_DEFER; /* I2C module not yet available */
81 +
82 + ret = i2c_smbus_read_byte(&tpa_i2c_client) >= 0;
83 + i2c_put_adapter(adap);
84 + return ret;
85 +};
86 +
87 +static struct property tpa_enable_prop = {
88 + .name = "status",
89 + .length = 4 + 1, /* length 'okay' + 1 */
90 + .value = "okay",
91 + };
92 +
93 static int snd_rpi_hifiberry_dacplusadcpro_probe(struct platform_device *pdev)
94 {
95 int ret = 0, i = 0;
96 struct snd_soc_card *card = &snd_rpi_hifiberry_dacplusadcpro;
97 + struct device_node *tpa_node;
98 + struct property *tpa_prop;
99 + struct of_changeset ocs;
100 + int len;
101 +
102 + /* probe for head phone amp */
103 + ret = hb_hp_detect();
104 + if (ret < 0)
105 + return ret;
106 + if (ret) {
107 + card->aux_dev = hifiberry_dacplusadcpro_aux_devs;
108 + card->num_aux_devs =
109 + ARRAY_SIZE(hifiberry_dacplusadcpro_aux_devs);
110 + tpa_node = of_find_compatible_node(NULL, NULL, "ti,tpa6130a2");
111 + tpa_prop = of_find_property(tpa_node, "status", &len);
112 +
113 + if (strcmp((char *)tpa_prop->value, "okay")) {
114 + /* and activate headphone using change_sets */
115 + dev_info(&pdev->dev, "activating headphone amplifier");
116 + of_changeset_init(&ocs);
117 + ret = of_changeset_update_property(&ocs, tpa_node,
118 + &tpa_enable_prop);
119 + if (ret) {
120 + dev_err(&pdev->dev,
121 + "cannot activate headphone amplifier\n");
122 + return -ENODEV;
123 + }
124 + ret = of_changeset_apply(&ocs);
125 + if (ret) {
126 + dev_err(&pdev->dev,
127 + "cannot activate headphone amplifier\n");
128 + return -ENODEV;
129 + }
130 + }
131 + }
132
133 snd_rpi_hifiberry_dacplusadcpro.dev = &pdev->dev;
134 if (pdev->dev.of_node) {
135 @@ -531,7 +599,7 @@ static struct platform_driver snd_rpi_hi
136
137 module_platform_driver(snd_rpi_hifiberry_dacplusadcpro_driver);
138
139 -MODULE_AUTHOR("Joerg Schambacher <joerg@i2audio.com>");
140 +MODULE_AUTHOR("Joerg Schambacher <joerg@hifiberry.com>");
141 MODULE_AUTHOR("Daniel Matuschek <daniel@hifiberry.com>");
142 MODULE_DESCRIPTION("ASoC Driver for HiFiBerry DAC+ADC");
143 MODULE_LICENSE("GPL v2");