1*d0fa1179SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2b65f824cSSasha Khapyorsky /*
3b65f824cSSasha Khapyorsky * Universal Interface for Intel High Definition Audio Codec
4b65f824cSSasha Khapyorsky *
5b65f824cSSasha Khapyorsky * HD audio interface patch for Silicon Labs 3054/5 modem codec
6b65f824cSSasha Khapyorsky *
7f01cc521SSasha Khapyorsky * Copyright (c) 2005 Sasha Khapyorsky <sashak@alsa-project.org>
8b65f824cSSasha Khapyorsky * Takashi Iwai <tiwai@suse.de>
9b65f824cSSasha Khapyorsky */
10b65f824cSSasha Khapyorsky
11b65f824cSSasha Khapyorsky #include <linux/init.h>
12b65f824cSSasha Khapyorsky #include <linux/delay.h>
13b65f824cSSasha Khapyorsky #include <linux/slab.h>
14da155d5bSPaul Gortmaker #include <linux/module.h>
15b65f824cSSasha Khapyorsky #include <sound/core.h>
16be57bfffSPierre-Louis Bossart #include <sound/hda_codec.h>
17b65f824cSSasha Khapyorsky #include "hda_local.h"
18b65f824cSSasha Khapyorsky
19b65f824cSSasha Khapyorsky /* si3054 verbs */
20b65f824cSSasha Khapyorsky #define SI3054_VERB_READ_NODE 0x900
21b65f824cSSasha Khapyorsky #define SI3054_VERB_WRITE_NODE 0x100
22b65f824cSSasha Khapyorsky
23b65f824cSSasha Khapyorsky /* si3054 nodes (registers) */
24b65f824cSSasha Khapyorsky #define SI3054_EXTENDED_MID 2
25b65f824cSSasha Khapyorsky #define SI3054_LINE_RATE 3
26b65f824cSSasha Khapyorsky #define SI3054_LINE_LEVEL 4
27b65f824cSSasha Khapyorsky #define SI3054_GPIO_CFG 5
28b65f824cSSasha Khapyorsky #define SI3054_GPIO_POLARITY 6
29b65f824cSSasha Khapyorsky #define SI3054_GPIO_STICKY 7
30b65f824cSSasha Khapyorsky #define SI3054_GPIO_WAKEUP 8
31b65f824cSSasha Khapyorsky #define SI3054_GPIO_STATUS 9
32b65f824cSSasha Khapyorsky #define SI3054_GPIO_CONTROL 10
33b65f824cSSasha Khapyorsky #define SI3054_MISC_AFE 11
34b65f824cSSasha Khapyorsky #define SI3054_CHIPID 12
35b65f824cSSasha Khapyorsky #define SI3054_LINE_CFG1 13
36b65f824cSSasha Khapyorsky #define SI3054_LINE_STATUS 14
37b65f824cSSasha Khapyorsky #define SI3054_DC_TERMINATION 15
38b65f824cSSasha Khapyorsky #define SI3054_LINE_CONFIG 16
39b65f824cSSasha Khapyorsky #define SI3054_CALLPROG_ATT 17
40b65f824cSSasha Khapyorsky #define SI3054_SQ_CONTROL 18
41b65f824cSSasha Khapyorsky #define SI3054_MISC_CONTROL 19
42b65f824cSSasha Khapyorsky #define SI3054_RING_CTRL1 20
43b65f824cSSasha Khapyorsky #define SI3054_RING_CTRL2 21
44b65f824cSSasha Khapyorsky
45b65f824cSSasha Khapyorsky /* extended MID */
46b65f824cSSasha Khapyorsky #define SI3054_MEI_READY 0xf
47b65f824cSSasha Khapyorsky
48b65f824cSSasha Khapyorsky /* line level */
49b65f824cSSasha Khapyorsky #define SI3054_ATAG_MASK 0x00f0
50b65f824cSSasha Khapyorsky #define SI3054_DTAG_MASK 0xf000
51b65f824cSSasha Khapyorsky
52b65f824cSSasha Khapyorsky /* GPIO bits */
53b65f824cSSasha Khapyorsky #define SI3054_GPIO_OH 0x0001
54b65f824cSSasha Khapyorsky #define SI3054_GPIO_CID 0x0002
55b65f824cSSasha Khapyorsky
56b65f824cSSasha Khapyorsky /* chipid and revisions */
57b65f824cSSasha Khapyorsky #define SI3054_CHIPID_CODEC_REV_MASK 0x000f
58b65f824cSSasha Khapyorsky #define SI3054_CHIPID_DAA_REV_MASK 0x00f0
59b65f824cSSasha Khapyorsky #define SI3054_CHIPID_INTERNATIONAL 0x0100
60b65f824cSSasha Khapyorsky #define SI3054_CHIPID_DAA_ID 0x0f00
61b65f824cSSasha Khapyorsky #define SI3054_CHIPID_CODEC_ID (1<<12)
62b65f824cSSasha Khapyorsky
63b65f824cSSasha Khapyorsky /* si3054 codec registers (nodes) access macros */
64b65f824cSSasha Khapyorsky #define GET_REG(codec,reg) (snd_hda_codec_read(codec,reg,0,SI3054_VERB_READ_NODE,0))
65b65f824cSSasha Khapyorsky #define SET_REG(codec,reg,val) (snd_hda_codec_write(codec,reg,0,SI3054_VERB_WRITE_NODE,val))
6682beb8fdSTakashi Iwai #define SET_REG_CACHE(codec,reg,val) \
6782beb8fdSTakashi Iwai snd_hda_codec_write_cache(codec,reg,0,SI3054_VERB_WRITE_NODE,val)
68b65f824cSSasha Khapyorsky
69b65f824cSSasha Khapyorsky
70b65f824cSSasha Khapyorsky struct si3054_spec {
71b65f824cSSasha Khapyorsky unsigned international;
72b65f824cSSasha Khapyorsky };
73b65f824cSSasha Khapyorsky
74b65f824cSSasha Khapyorsky
75b65f824cSSasha Khapyorsky /*
76b65f824cSSasha Khapyorsky * Modem mixer
77b65f824cSSasha Khapyorsky */
78b65f824cSSasha Khapyorsky
79b65f824cSSasha Khapyorsky #define PRIVATE_VALUE(reg,mask) ((reg<<16)|(mask&0xffff))
80b65f824cSSasha Khapyorsky #define PRIVATE_REG(val) ((val>>16)&0xffff)
81b65f824cSSasha Khapyorsky #define PRIVATE_MASK(val) (val&0xffff)
82b65f824cSSasha Khapyorsky
83a5ce8890STakashi Iwai #define si3054_switch_info snd_ctl_boolean_mono_info
84b65f824cSSasha Khapyorsky
si3054_switch_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * uvalue)85c8b6bf9bSTakashi Iwai static int si3054_switch_get(struct snd_kcontrol *kcontrol,
86c8b6bf9bSTakashi Iwai struct snd_ctl_elem_value *uvalue)
87b65f824cSSasha Khapyorsky {
88b65f824cSSasha Khapyorsky struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
89b65f824cSSasha Khapyorsky u16 reg = PRIVATE_REG(kcontrol->private_value);
90b65f824cSSasha Khapyorsky u16 mask = PRIVATE_MASK(kcontrol->private_value);
91b65f824cSSasha Khapyorsky uvalue->value.integer.value[0] = (GET_REG(codec, reg)) & mask ? 1 : 0 ;
92b65f824cSSasha Khapyorsky return 0;
93b65f824cSSasha Khapyorsky }
94b65f824cSSasha Khapyorsky
si3054_switch_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * uvalue)95c8b6bf9bSTakashi Iwai static int si3054_switch_put(struct snd_kcontrol *kcontrol,
96c8b6bf9bSTakashi Iwai struct snd_ctl_elem_value *uvalue)
97b65f824cSSasha Khapyorsky {
98b65f824cSSasha Khapyorsky struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
99b65f824cSSasha Khapyorsky u16 reg = PRIVATE_REG(kcontrol->private_value);
100b65f824cSSasha Khapyorsky u16 mask = PRIVATE_MASK(kcontrol->private_value);
101b65f824cSSasha Khapyorsky if (uvalue->value.integer.value[0])
10282beb8fdSTakashi Iwai SET_REG_CACHE(codec, reg, (GET_REG(codec, reg)) | mask);
103b65f824cSSasha Khapyorsky else
10482beb8fdSTakashi Iwai SET_REG_CACHE(codec, reg, (GET_REG(codec, reg)) & ~mask);
105b65f824cSSasha Khapyorsky return 0;
106b65f824cSSasha Khapyorsky }
107b65f824cSSasha Khapyorsky
108b65f824cSSasha Khapyorsky #define SI3054_KCONTROL(kname,reg,mask) { \
109b65f824cSSasha Khapyorsky .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
110b65f824cSSasha Khapyorsky .name = kname, \
1115b0cb1d8SJaroslav Kysela .subdevice = HDA_SUBDEV_NID_FLAG | reg, \
112b65f824cSSasha Khapyorsky .info = si3054_switch_info, \
113b65f824cSSasha Khapyorsky .get = si3054_switch_get, \
114b65f824cSSasha Khapyorsky .put = si3054_switch_put, \
115b65f824cSSasha Khapyorsky .private_value = PRIVATE_VALUE(reg,mask), \
116b65f824cSSasha Khapyorsky }
117b65f824cSSasha Khapyorsky
118b65f824cSSasha Khapyorsky
1199cf0aa9eSTakashi Iwai static const struct snd_kcontrol_new si3054_modem_mixer[] = {
120b65f824cSSasha Khapyorsky SI3054_KCONTROL("Off-hook Switch", SI3054_GPIO_CONTROL, SI3054_GPIO_OH),
121b65f824cSSasha Khapyorsky SI3054_KCONTROL("Caller ID Switch", SI3054_GPIO_CONTROL, SI3054_GPIO_CID),
122b65f824cSSasha Khapyorsky {}
123b65f824cSSasha Khapyorsky };
124b65f824cSSasha Khapyorsky
si3054_build_controls(struct hda_codec * codec)125b65f824cSSasha Khapyorsky static int si3054_build_controls(struct hda_codec *codec)
126b65f824cSSasha Khapyorsky {
127b65f824cSSasha Khapyorsky return snd_hda_add_new_ctls(codec, si3054_modem_mixer);
128b65f824cSSasha Khapyorsky }
129b65f824cSSasha Khapyorsky
130b65f824cSSasha Khapyorsky
131b65f824cSSasha Khapyorsky /*
132b65f824cSSasha Khapyorsky * PCM callbacks
133b65f824cSSasha Khapyorsky */
134b65f824cSSasha Khapyorsky
si3054_pcm_prepare(struct hda_pcm_stream * hinfo,struct hda_codec * codec,unsigned int stream_tag,unsigned int format,struct snd_pcm_substream * substream)135b65f824cSSasha Khapyorsky static int si3054_pcm_prepare(struct hda_pcm_stream *hinfo,
136b65f824cSSasha Khapyorsky struct hda_codec *codec,
137b65f824cSSasha Khapyorsky unsigned int stream_tag,
138b65f824cSSasha Khapyorsky unsigned int format,
139c8b6bf9bSTakashi Iwai struct snd_pcm_substream *substream)
140b65f824cSSasha Khapyorsky {
141b65f824cSSasha Khapyorsky u16 val;
142b65f824cSSasha Khapyorsky
143b65f824cSSasha Khapyorsky SET_REG(codec, SI3054_LINE_RATE, substream->runtime->rate);
144b65f824cSSasha Khapyorsky val = GET_REG(codec, SI3054_LINE_LEVEL);
145b65f824cSSasha Khapyorsky val &= 0xff << (8 * (substream->stream != SNDRV_PCM_STREAM_PLAYBACK));
146b65f824cSSasha Khapyorsky val |= ((stream_tag & 0xf) << 4) << (8 * (substream->stream == SNDRV_PCM_STREAM_PLAYBACK));
147b65f824cSSasha Khapyorsky SET_REG(codec, SI3054_LINE_LEVEL, val);
148b65f824cSSasha Khapyorsky
149b65f824cSSasha Khapyorsky snd_hda_codec_setup_stream(codec, hinfo->nid,
150b65f824cSSasha Khapyorsky stream_tag, 0, format);
151b65f824cSSasha Khapyorsky return 0;
152b65f824cSSasha Khapyorsky }
153b65f824cSSasha Khapyorsky
si3054_pcm_open(struct hda_pcm_stream * hinfo,struct hda_codec * codec,struct snd_pcm_substream * substream)154b65f824cSSasha Khapyorsky static int si3054_pcm_open(struct hda_pcm_stream *hinfo,
155b65f824cSSasha Khapyorsky struct hda_codec *codec,
156c8b6bf9bSTakashi Iwai struct snd_pcm_substream *substream)
157b65f824cSSasha Khapyorsky {
15850c697adSTakashi Iwai static const unsigned int rates[] = { 8000, 9600, 16000 };
15950c697adSTakashi Iwai static const struct snd_pcm_hw_constraint_list hw_constraints_rates = {
160b65f824cSSasha Khapyorsky .count = ARRAY_SIZE(rates),
161b65f824cSSasha Khapyorsky .list = rates,
162b65f824cSSasha Khapyorsky .mask = 0,
163b65f824cSSasha Khapyorsky };
164b65f824cSSasha Khapyorsky substream->runtime->hw.period_bytes_min = 80;
165b65f824cSSasha Khapyorsky return snd_pcm_hw_constraint_list(substream->runtime, 0,
166b65f824cSSasha Khapyorsky SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
167b65f824cSSasha Khapyorsky }
168b65f824cSSasha Khapyorsky
169b65f824cSSasha Khapyorsky
1709cf0aa9eSTakashi Iwai static const struct hda_pcm_stream si3054_pcm = {
171b65f824cSSasha Khapyorsky .substreams = 1,
172b65f824cSSasha Khapyorsky .channels_min = 1,
173b65f824cSSasha Khapyorsky .channels_max = 1,
174b65f824cSSasha Khapyorsky .nid = 0x1,
175b65f824cSSasha Khapyorsky .rates = SNDRV_PCM_RATE_8000|SNDRV_PCM_RATE_16000|SNDRV_PCM_RATE_KNOT,
176b65f824cSSasha Khapyorsky .formats = SNDRV_PCM_FMTBIT_S16_LE,
177b65f824cSSasha Khapyorsky .maxbps = 16,
178b65f824cSSasha Khapyorsky .ops = {
179b65f824cSSasha Khapyorsky .open = si3054_pcm_open,
180b65f824cSSasha Khapyorsky .prepare = si3054_pcm_prepare,
181b65f824cSSasha Khapyorsky },
182b65f824cSSasha Khapyorsky };
183b65f824cSSasha Khapyorsky
184b65f824cSSasha Khapyorsky
si3054_build_pcms(struct hda_codec * codec)185b65f824cSSasha Khapyorsky static int si3054_build_pcms(struct hda_codec *codec)
186b65f824cSSasha Khapyorsky {
187bbbc7e85STakashi Iwai struct hda_pcm *info;
188bbbc7e85STakashi Iwai
189bbbc7e85STakashi Iwai info = snd_hda_codec_pcm_new(codec, "Si3054 Modem");
190bbbc7e85STakashi Iwai if (!info)
191bbbc7e85STakashi Iwai return -ENOMEM;
192b65f824cSSasha Khapyorsky info->stream[SNDRV_PCM_STREAM_PLAYBACK] = si3054_pcm;
193b65f824cSSasha Khapyorsky info->stream[SNDRV_PCM_STREAM_CAPTURE] = si3054_pcm;
1947639a06cSTakashi Iwai info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = codec->core.mfg;
1957639a06cSTakashi Iwai info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = codec->core.mfg;
1967ba72ba1STakashi Iwai info->pcm_type = HDA_PCM_TYPE_MODEM;
197b65f824cSSasha Khapyorsky return 0;
198b65f824cSSasha Khapyorsky }
199b65f824cSSasha Khapyorsky
200b65f824cSSasha Khapyorsky
201b65f824cSSasha Khapyorsky /*
202b65f824cSSasha Khapyorsky * Init part
203b65f824cSSasha Khapyorsky */
204b65f824cSSasha Khapyorsky
si3054_init(struct hda_codec * codec)205b65f824cSSasha Khapyorsky static int si3054_init(struct hda_codec *codec)
206b65f824cSSasha Khapyorsky {
207b65f824cSSasha Khapyorsky struct si3054_spec *spec = codec->spec;
208b65f824cSSasha Khapyorsky unsigned wait_count;
209b65f824cSSasha Khapyorsky u16 val;
210b65f824cSSasha Khapyorsky
211a551d914STakashi Iwai if (snd_hdac_regmap_add_vendor_verb(&codec->core,
212a551d914STakashi Iwai SI3054_VERB_WRITE_NODE))
213a551d914STakashi Iwai return -ENOMEM;
214a551d914STakashi Iwai
215b65f824cSSasha Khapyorsky snd_hda_codec_write(codec, AC_NODE_ROOT, 0, AC_VERB_SET_CODEC_RESET, 0);
2167639a06cSTakashi Iwai snd_hda_codec_write(codec, codec->core.mfg, 0, AC_VERB_SET_STREAM_FORMAT, 0);
217b65f824cSSasha Khapyorsky SET_REG(codec, SI3054_LINE_RATE, 9600);
218b65f824cSSasha Khapyorsky SET_REG(codec, SI3054_LINE_LEVEL, SI3054_DTAG_MASK|SI3054_ATAG_MASK);
219b65f824cSSasha Khapyorsky SET_REG(codec, SI3054_EXTENDED_MID, 0);
220b65f824cSSasha Khapyorsky
221b65f824cSSasha Khapyorsky wait_count = 10;
222b65f824cSSasha Khapyorsky do {
223b65f824cSSasha Khapyorsky msleep(2);
224b65f824cSSasha Khapyorsky val = GET_REG(codec, SI3054_EXTENDED_MID);
225b65f824cSSasha Khapyorsky } while ((val & SI3054_MEI_READY) != SI3054_MEI_READY && wait_count--);
226b65f824cSSasha Khapyorsky
227b65f824cSSasha Khapyorsky if((val&SI3054_MEI_READY) != SI3054_MEI_READY) {
2284e76a883STakashi Iwai codec_err(codec, "si3054: cannot initialize. EXT MID = %04x\n", val);
22983af036dSTakashi Iwai /* let's pray that this is no fatal error */
23083af036dSTakashi Iwai /* return -EACCES; */
231b65f824cSSasha Khapyorsky }
232b65f824cSSasha Khapyorsky
233b65f824cSSasha Khapyorsky SET_REG(codec, SI3054_GPIO_POLARITY, 0xffff);
234b65f824cSSasha Khapyorsky SET_REG(codec, SI3054_GPIO_CFG, 0x0);
235b65f824cSSasha Khapyorsky SET_REG(codec, SI3054_MISC_AFE, 0);
236b65f824cSSasha Khapyorsky SET_REG(codec, SI3054_LINE_CFG1,0x200);
237b65f824cSSasha Khapyorsky
238b65f824cSSasha Khapyorsky if((GET_REG(codec,SI3054_LINE_STATUS) & (1<<6)) == 0) {
2394e76a883STakashi Iwai codec_dbg(codec,
2404e76a883STakashi Iwai "Link Frame Detect(FDT) is not ready (line status: %04x)\n",
241b65f824cSSasha Khapyorsky GET_REG(codec,SI3054_LINE_STATUS));
242b65f824cSSasha Khapyorsky }
243b65f824cSSasha Khapyorsky
244b65f824cSSasha Khapyorsky spec->international = GET_REG(codec, SI3054_CHIPID) & SI3054_CHIPID_INTERNATIONAL;
245b65f824cSSasha Khapyorsky
246b65f824cSSasha Khapyorsky return 0;
247b65f824cSSasha Khapyorsky }
248b65f824cSSasha Khapyorsky
si3054_free(struct hda_codec * codec)249b65f824cSSasha Khapyorsky static void si3054_free(struct hda_codec *codec)
250b65f824cSSasha Khapyorsky {
251b65f824cSSasha Khapyorsky kfree(codec->spec);
252b65f824cSSasha Khapyorsky }
253b65f824cSSasha Khapyorsky
254b65f824cSSasha Khapyorsky
255b65f824cSSasha Khapyorsky /*
256b65f824cSSasha Khapyorsky */
257b65f824cSSasha Khapyorsky
2589cf0aa9eSTakashi Iwai static const struct hda_codec_ops si3054_patch_ops = {
259b65f824cSSasha Khapyorsky .build_controls = si3054_build_controls,
260b65f824cSSasha Khapyorsky .build_pcms = si3054_build_pcms,
261b65f824cSSasha Khapyorsky .init = si3054_init,
262b65f824cSSasha Khapyorsky .free = si3054_free,
263b65f824cSSasha Khapyorsky };
264b65f824cSSasha Khapyorsky
patch_si3054(struct hda_codec * codec)265b65f824cSSasha Khapyorsky static int patch_si3054(struct hda_codec *codec)
266b65f824cSSasha Khapyorsky {
267e560d8d8STakashi Iwai struct si3054_spec *spec = kzalloc(sizeof(*spec), GFP_KERNEL);
268b65f824cSSasha Khapyorsky if (spec == NULL)
269b65f824cSSasha Khapyorsky return -ENOMEM;
270b65f824cSSasha Khapyorsky codec->spec = spec;
271b65f824cSSasha Khapyorsky codec->patch_ops = si3054_patch_ops;
272b65f824cSSasha Khapyorsky return 0;
273b65f824cSSasha Khapyorsky }
274b65f824cSSasha Khapyorsky
275b65f824cSSasha Khapyorsky /*
276b65f824cSSasha Khapyorsky * patch entries
277b65f824cSSasha Khapyorsky */
278b9a94a9cSTakashi Iwai static const struct hda_device_id snd_hda_id_si3054[] = {
279b9a94a9cSTakashi Iwai HDA_CODEC_ENTRY(0x163c3055, "Si3054", patch_si3054),
280b9a94a9cSTakashi Iwai HDA_CODEC_ENTRY(0x163c3155, "Si3054", patch_si3054),
281b9a94a9cSTakashi Iwai HDA_CODEC_ENTRY(0x11c13026, "Si3054", patch_si3054),
282b9a94a9cSTakashi Iwai HDA_CODEC_ENTRY(0x11c13055, "Si3054", patch_si3054),
283b9a94a9cSTakashi Iwai HDA_CODEC_ENTRY(0x11c13155, "Si3054", patch_si3054),
284b9a94a9cSTakashi Iwai HDA_CODEC_ENTRY(0x10573055, "Si3054", patch_si3054),
285b9a94a9cSTakashi Iwai HDA_CODEC_ENTRY(0x10573057, "Si3054", patch_si3054),
286b9a94a9cSTakashi Iwai HDA_CODEC_ENTRY(0x10573155, "Si3054", patch_si3054),
287be38114aSClaudio Matsuoka /* VIA HDA on Clevo m540 */
288b9a94a9cSTakashi Iwai HDA_CODEC_ENTRY(0x11063288, "Si3054", patch_si3054),
2893b542985SChristian Rothlaender /* Asus A8J Modem (SM56) */
290b9a94a9cSTakashi Iwai HDA_CODEC_ENTRY(0x15433155, "Si3054", patch_si3054),
291ef2ec0ddSClaudio Matsuoka /* LG LW20 modem */
292b9a94a9cSTakashi Iwai HDA_CODEC_ENTRY(0x18540018, "Si3054", patch_si3054),
293b65f824cSSasha Khapyorsky {}
294b65f824cSSasha Khapyorsky };
295b9a94a9cSTakashi Iwai MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_si3054);
2961289e9e8STakashi Iwai
2971289e9e8STakashi Iwai MODULE_LICENSE("GPL");
2981289e9e8STakashi Iwai MODULE_DESCRIPTION("Si3054 HD-audio modem codec");
2991289e9e8STakashi Iwai
300d8a766a1STakashi Iwai static struct hda_codec_driver si3054_driver = {
301b9a94a9cSTakashi Iwai .id = snd_hda_id_si3054,
3021289e9e8STakashi Iwai };
3031289e9e8STakashi Iwai
304d8a766a1STakashi Iwai module_hda_codec_driver(si3054_driver);
305