File Index
Files and directories
Sign in
FILE

senarytech.c

File preview with syntax highlighting.

linux-6.19.14/sound/hda/codecs/senarytech.c C
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * HD audio codec driver for Senary HDA audio codec
  4 *
  5 * Initially based on conexant.c
  6 */
  7
  8#include <linux/init.h>
  9#include <linux/delay.h>
 10#include <linux/slab.h>
 11#include <linux/module.h>
 12#include <sound/core.h>
 13#include <sound/jack.h>
 14
 15#include <sound/hda_codec.h>
 16#include "hda_local.h"
 17#include "hda_auto_parser.h"
 18#include "hda_beep.h"
 19#include "hda_jack.h"
 20#include "generic.h"
 21
 22/* GPIO node ID */
 23#define SENARY_GPIO_NODE	0x01
 24
 25struct senary_spec {
 26	struct hda_gen_spec gen;
 27
 28	/* extra EAPD pins */
 29	unsigned int num_eapds;
 30	hda_nid_t eapds[4];
 31	bool dynamic_eapd;
 32	hda_nid_t mute_led_eapd;
 33
 34	unsigned int parse_flags; /* flag for snd_hda_parse_pin_defcfg() */
 35
 36	int mute_led_polarity;
 37	unsigned int gpio_led;
 38	unsigned int gpio_mute_led_mask;
 39	unsigned int gpio_mic_led_mask;
 40};
 41
 42#ifdef CONFIG_SND_HDA_INPUT_BEEP
 43/* additional beep mixers; private_value will be overwritten */
 44static const struct snd_kcontrol_new senary_beep_mixer[] = {
 45	HDA_CODEC_VOLUME_MONO("Beep Playback Volume", 0, 1, 0, HDA_OUTPUT),
 46	HDA_CODEC_MUTE_BEEP_MONO("Beep Playback Switch", 0, 1, 0, HDA_OUTPUT),
 47};
 48
 49static int set_beep_amp(struct senary_spec *spec, hda_nid_t nid,
 50			int idx, int dir)
 51{
 52	struct snd_kcontrol_new *knew;
 53	unsigned int beep_amp = HDA_COMPOSE_AMP_VAL(nid, 1, idx, dir);
 54	int i;
 55
 56	spec->gen.beep_nid = nid;
 57	for (i = 0; i < ARRAY_SIZE(senary_beep_mixer); i++) {
 58		knew = snd_hda_gen_add_kctl(&spec->gen, NULL,
 59					    &senary_beep_mixer[i]);
 60		if (!knew)
 61			return -ENOMEM;
 62		knew->private_value = beep_amp;
 63	}
 64	return 0;
 65}
 66
 67static int senary_auto_parse_beep(struct hda_codec *codec)
 68{
 69	struct senary_spec *spec = codec->spec;
 70	hda_nid_t nid;
 71
 72	for_each_hda_codec_node(nid, codec)
 73		if ((get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_BEEP) &&
 74			(get_wcaps(codec, nid) & (AC_WCAP_OUT_AMP | AC_WCAP_AMP_OVRD)))
 75			return set_beep_amp(spec, nid, 0, HDA_OUTPUT);
 76	return 0;
 77}
 78#else
 79#define senary_auto_parse_beep(codec)	0
 80#endif
 81
 82/* parse EAPDs */
 83static void senary_auto_parse_eapd(struct hda_codec *codec)
 84{
 85	struct senary_spec *spec = codec->spec;
 86	hda_nid_t nid;
 87
 88	for_each_hda_codec_node(nid, codec) {
 89		if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
 90			continue;
 91		if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD))
 92			continue;
 93		spec->eapds[spec->num_eapds++] = nid;
 94		if (spec->num_eapds >= ARRAY_SIZE(spec->eapds))
 95			break;
 96	}
 97}
 98
 99static void senary_auto_turn_eapd(struct hda_codec *codec, int num_pins,
100			      const hda_nid_t *pins, bool on)
101{
102	int i;
103
104	for (i = 0; i < num_pins; i++) {
105		if (snd_hda_query_pin_caps(codec, pins[i]) & AC_PINCAP_EAPD)
106			snd_hda_codec_write(codec, pins[i], 0,
107					    AC_VERB_SET_EAPD_BTLENABLE,
108					    on ? 0x02 : 0);
109	}
110}
111
112/* turn on/off EAPD according to Master switch */
113static void senary_auto_vmaster_hook(void *private_data, int enabled)
114{
115	struct hda_codec *codec = private_data;
116	struct senary_spec *spec = codec->spec;
117
118	senary_auto_turn_eapd(codec, spec->num_eapds, spec->eapds, enabled);
119}
120
121static void senary_init_gpio_led(struct hda_codec *codec)
122{
123	struct senary_spec *spec = codec->spec;
124	unsigned int mask = spec->gpio_mute_led_mask | spec->gpio_mic_led_mask;
125
126	if (mask) {
127		snd_hda_codec_write(codec, SENARY_GPIO_NODE, 0, AC_VERB_SET_GPIO_MASK,
128				    mask);
129		snd_hda_codec_write(codec, SENARY_GPIO_NODE, 0, AC_VERB_SET_GPIO_DIRECTION,
130				    mask);
131		snd_hda_codec_write(codec, SENARY_GPIO_NODE, 0, AC_VERB_SET_GPIO_DATA,
132				    spec->gpio_led);
133	}
134}
135
136static int senary_init(struct hda_codec *codec)
137{
138	struct senary_spec *spec = codec->spec;
139
140	snd_hda_gen_init(codec);
141	senary_init_gpio_led(codec);
142	if (!spec->dynamic_eapd)
143		senary_auto_turn_eapd(codec, spec->num_eapds, spec->eapds, true);
144	snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_INIT);
145
146	return 0;
147}
148
149static void senary_shutdown(struct hda_codec *codec)
150{
151	struct senary_spec *spec = codec->spec;
152
153	/* Turn the problematic codec into D3 to avoid spurious noises
154	 * from the internal speaker during (and after) reboot
155	 */
156	senary_auto_turn_eapd(codec, spec->num_eapds, spec->eapds, false);
157}
158
159static void senary_remove(struct hda_codec *codec)
160{
161	senary_shutdown(codec);
162	snd_hda_gen_remove(codec);
163}
164
165static int senary_suspend(struct hda_codec *codec)
166{
167	senary_shutdown(codec);
168	return 0;
169}
170
171static int senary_probe(struct hda_codec *codec, const struct hda_device_id *id)
172{
173	struct senary_spec *spec;
174	int err;
175
176	codec_info(codec, "%s: BIOS auto-probing.\n", codec->core.chip_name);
177
178	spec = kzalloc(sizeof(*spec), GFP_KERNEL);
179	if (!spec)
180		return -ENOMEM;
181	snd_hda_gen_spec_init(&spec->gen);
182	codec->spec = spec;
183
184	senary_auto_parse_eapd(codec);
185	spec->gen.own_eapd_ctl = 1;
186
187	if (!spec->gen.vmaster_mute.hook)
188		spec->gen.vmaster_mute.hook = senary_auto_vmaster_hook;
189
190	snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
191
192	err = snd_hda_parse_pin_defcfg(codec, &spec->gen.autocfg, NULL,
193				       spec->parse_flags);
194	if (err < 0)
195		goto error;
196
197	err = senary_auto_parse_beep(codec);
198	if (err < 0)
199		goto error;
200
201	err = snd_hda_gen_parse_auto_config(codec, &spec->gen.autocfg);
202	if (err < 0)
203		goto error;
204
205	/* Some laptops with Senary chips show stalls in S3 resume,
206	 * which falls into the single-cmd mode.
207	 * Better to make reset, then.
208	 */
209	if (!codec->bus->core.sync_write) {
210		codec_info(codec,
211			   "Enable sync_write for stable communication\n");
212		codec->bus->core.sync_write = 1;
213		codec->bus->allow_bus_reset = 1;
214	}
215
216	snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
217
218	return 0;
219
220 error:
221	senary_remove(codec);
222	return err;
223}
224
225static const struct hda_codec_ops senary_codec_ops = {
226	.probe = senary_probe,
227	.remove = senary_remove,
228	.build_controls = snd_hda_gen_build_controls,
229	.build_pcms = snd_hda_gen_build_pcms,
230	.init = senary_init,
231	.unsol_event = snd_hda_jack_unsol_event,
232	.suspend = senary_suspend,
233	.check_power_status = snd_hda_gen_check_power_status,
234	.stream_pm = snd_hda_gen_stream_pm,
235};
236
237/*
238 */
239
240static const struct hda_device_id snd_hda_id_senary[] = {
241	HDA_CODEC_ID(0x1fa86186, "SN6186"),
242	{} /* terminator */
243};
244MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_senary);
245
246MODULE_LICENSE("GPL");
247MODULE_DESCRIPTION("Senarytech HD-audio codec");
248
249static struct hda_codec_driver senary_driver = {
250	.id = snd_hda_id_senary,
251	.ops = &senary_codec_ops,
252};
253
254module_hda_codec_driver(senary_driver);
~/ Read-only file preview.