patch_si3054.c 8.3 KB

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