patch_si3054.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * Universal Interface for Intel High Definition Audio Codec
  3. *
  4. * HD audio interface patch for Silicon Labs 3054/5 modem codec
  5. *
  6. * Copyright (c) 2005 Sasha Khapyorsky <sashak@alsa-project.org>
  7. * Takashi Iwai <tiwai@suse.de>
  8. *
  9. *
  10. * This driver is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This driver is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. #include <linux/init.h>
  25. #include <linux/delay.h>
  26. #include <linux/slab.h>
  27. #include <sound/core.h>
  28. #include "hda_codec.h"
  29. #include "hda_local.h"
  30. /* si3054 verbs */
  31. #define SI3054_VERB_READ_NODE 0x900
  32. #define SI3054_VERB_WRITE_NODE 0x100
  33. /* si3054 nodes (registers) */
  34. #define SI3054_EXTENDED_MID 2
  35. #define SI3054_LINE_RATE 3
  36. #define SI3054_LINE_LEVEL 4
  37. #define SI3054_GPIO_CFG 5
  38. #define SI3054_GPIO_POLARITY 6
  39. #define SI3054_GPIO_STICKY 7
  40. #define SI3054_GPIO_WAKEUP 8
  41. #define SI3054_GPIO_STATUS 9
  42. #define SI3054_GPIO_CONTROL 10
  43. #define SI3054_MISC_AFE 11
  44. #define SI3054_CHIPID 12
  45. #define SI3054_LINE_CFG1 13
  46. #define SI3054_LINE_STATUS 14
  47. #define SI3054_DC_TERMINATION 15
  48. #define SI3054_LINE_CONFIG 16
  49. #define SI3054_CALLPROG_ATT 17
  50. #define SI3054_SQ_CONTROL 18
  51. #define SI3054_MISC_CONTROL 19
  52. #define SI3054_RING_CTRL1 20
  53. #define SI3054_RING_CTRL2 21
  54. /* extended MID */
  55. #define SI3054_MEI_READY 0xf
  56. /* line level */
  57. #define SI3054_ATAG_MASK 0x00f0
  58. #define SI3054_DTAG_MASK 0xf000
  59. /* GPIO bits */
  60. #define SI3054_GPIO_OH 0x0001
  61. #define SI3054_GPIO_CID 0x0002
  62. /* chipid and revisions */
  63. #define SI3054_CHIPID_CODEC_REV_MASK 0x000f
  64. #define SI3054_CHIPID_DAA_REV_MASK 0x00f0
  65. #define SI3054_CHIPID_INTERNATIONAL 0x0100
  66. #define SI3054_CHIPID_DAA_ID 0x0f00
  67. #define SI3054_CHIPID_CODEC_ID (1<<12)
  68. /* si3054 codec registers (nodes) access macros */
  69. #define GET_REG(codec,reg) (snd_hda_codec_read(codec,reg,0,SI3054_VERB_READ_NODE,0))
  70. #define SET_REG(codec,reg,val) (snd_hda_codec_write(codec,reg,0,SI3054_VERB_WRITE_NODE,val))
  71. #define SET_REG_CACHE(codec,reg,val) \
  72. snd_hda_codec_write_cache(codec,reg,0,SI3054_VERB_WRITE_NODE,val)
  73. struct si3054_spec {
  74. unsigned international;
  75. struct hda_pcm pcm;
  76. };
  77. /*
  78. * Modem mixer
  79. */
  80. #define PRIVATE_VALUE(reg,mask) ((reg<<16)|(mask&0xffff))
  81. #define PRIVATE_REG(val) ((val>>16)&0xffff)
  82. #define PRIVATE_MASK(val) (val&0xffff)
  83. #define si3054_switch_info snd_ctl_boolean_mono_info
  84. static int si3054_switch_get(struct snd_kcontrol *kcontrol,
  85. struct snd_ctl_elem_value *uvalue)
  86. {
  87. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  88. u16 reg = PRIVATE_REG(kcontrol->private_value);
  89. u16 mask = PRIVATE_MASK(kcontrol->private_value);
  90. uvalue->value.integer.value[0] = (GET_REG(codec, reg)) & mask ? 1 : 0 ;
  91. return 0;
  92. }
  93. static int si3054_switch_put(struct snd_kcontrol *kcontrol,
  94. struct snd_ctl_elem_value *uvalue)
  95. {
  96. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  97. u16 reg = PRIVATE_REG(kcontrol->private_value);
  98. u16 mask = PRIVATE_MASK(kcontrol->private_value);
  99. if (uvalue->value.integer.value[0])
  100. SET_REG_CACHE(codec, reg, (GET_REG(codec, reg)) | mask);
  101. else
  102. SET_REG_CACHE(codec, reg, (GET_REG(codec, reg)) & ~mask);
  103. return 0;
  104. }
  105. #define SI3054_KCONTROL(kname,reg,mask) { \
  106. .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  107. .name = kname, \
  108. .subdevice = HDA_SUBDEV_NID_FLAG | reg, \
  109. .info = si3054_switch_info, \
  110. .get = si3054_switch_get, \
  111. .put = si3054_switch_put, \
  112. .private_value = PRIVATE_VALUE(reg,mask), \
  113. }
  114. static const struct snd_kcontrol_new si3054_modem_mixer[] = {
  115. SI3054_KCONTROL("Off-hook Switch", SI3054_GPIO_CONTROL, SI3054_GPIO_OH),
  116. SI3054_KCONTROL("Caller ID Switch", SI3054_GPIO_CONTROL, SI3054_GPIO_CID),
  117. {}
  118. };
  119. static int si3054_build_controls(struct hda_codec *codec)
  120. {
  121. return snd_hda_add_new_ctls(codec, si3054_modem_mixer);
  122. }
  123. /*
  124. * PCM callbacks
  125. */
  126. static int si3054_pcm_prepare(struct hda_pcm_stream *hinfo,
  127. struct hda_codec *codec,
  128. unsigned int stream_tag,
  129. unsigned int format,
  130. struct snd_pcm_substream *substream)
  131. {
  132. u16 val;
  133. SET_REG(codec, SI3054_LINE_RATE, substream->runtime->rate);
  134. val = GET_REG(codec, SI3054_LINE_LEVEL);
  135. val &= 0xff << (8 * (substream->stream != SNDRV_PCM_STREAM_PLAYBACK));
  136. val |= ((stream_tag & 0xf) << 4) << (8 * (substream->stream == SNDRV_PCM_STREAM_PLAYBACK));
  137. SET_REG(codec, SI3054_LINE_LEVEL, val);
  138. snd_hda_codec_setup_stream(codec, hinfo->nid,
  139. stream_tag, 0, format);
  140. return 0;
  141. }
  142. static int si3054_pcm_open(struct hda_pcm_stream *hinfo,
  143. struct hda_codec *codec,
  144. struct snd_pcm_substream *substream)
  145. {
  146. static unsigned int rates[] = { 8000, 9600, 16000 };
  147. static struct snd_pcm_hw_constraint_list hw_constraints_rates = {
  148. .count = ARRAY_SIZE(rates),
  149. .list = rates,
  150. .mask = 0,
  151. };
  152. substream->runtime->hw.period_bytes_min = 80;
  153. return snd_pcm_hw_constraint_list(substream->runtime, 0,
  154. SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
  155. }
  156. static const struct hda_pcm_stream si3054_pcm = {
  157. .substreams = 1,
  158. .channels_min = 1,
  159. .channels_max = 1,
  160. .nid = 0x1,
  161. .rates = SNDRV_PCM_RATE_8000|SNDRV_PCM_RATE_16000|SNDRV_PCM_RATE_KNOT,
  162. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  163. .maxbps = 16,
  164. .ops = {
  165. .open = si3054_pcm_open,
  166. .prepare = si3054_pcm_prepare,
  167. },
  168. };
  169. static int si3054_build_pcms(struct hda_codec *codec)
  170. {
  171. struct si3054_spec *spec = codec->spec;
  172. struct hda_pcm *info = &spec->pcm;
  173. codec->num_pcms = 1;
  174. codec->pcm_info = info;
  175. info->name = "Si3054 Modem";
  176. info->stream[SNDRV_PCM_STREAM_PLAYBACK] = si3054_pcm;
  177. info->stream[SNDRV_PCM_STREAM_CAPTURE] = si3054_pcm;
  178. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = codec->mfg;
  179. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = codec->mfg;
  180. info->pcm_type = HDA_PCM_TYPE_MODEM;
  181. return 0;
  182. }
  183. /*
  184. * Init part
  185. */
  186. static int si3054_init(struct hda_codec *codec)
  187. {
  188. struct si3054_spec *spec = codec->spec;
  189. unsigned wait_count;
  190. u16 val;
  191. snd_hda_codec_write(codec, AC_NODE_ROOT, 0, AC_VERB_SET_CODEC_RESET, 0);
  192. snd_hda_codec_write(codec, codec->mfg, 0, AC_VERB_SET_STREAM_FORMAT, 0);
  193. SET_REG(codec, SI3054_LINE_RATE, 9600);
  194. SET_REG(codec, SI3054_LINE_LEVEL, SI3054_DTAG_MASK|SI3054_ATAG_MASK);
  195. SET_REG(codec, SI3054_EXTENDED_MID, 0);
  196. wait_count = 10;
  197. do {
  198. msleep(2);
  199. val = GET_REG(codec, SI3054_EXTENDED_MID);
  200. } while ((val & SI3054_MEI_READY) != SI3054_MEI_READY && wait_count--);
  201. if((val&SI3054_MEI_READY) != SI3054_MEI_READY) {
  202. snd_printk(KERN_ERR "si3054: cannot initialize. EXT MID = %04x\n", val);
  203. /* let's pray that this is no fatal error */
  204. /* return -EACCES; */
  205. }
  206. SET_REG(codec, SI3054_GPIO_POLARITY, 0xffff);
  207. SET_REG(codec, SI3054_GPIO_CFG, 0x0);
  208. SET_REG(codec, SI3054_MISC_AFE, 0);
  209. SET_REG(codec, SI3054_LINE_CFG1,0x200);
  210. if((GET_REG(codec,SI3054_LINE_STATUS) & (1<<6)) == 0) {
  211. snd_printd("Link Frame Detect(FDT) is not ready (line status: %04x)\n",
  212. GET_REG(codec,SI3054_LINE_STATUS));
  213. }
  214. spec->international = GET_REG(codec, SI3054_CHIPID) & SI3054_CHIPID_INTERNATIONAL;
  215. return 0;
  216. }
  217. static void si3054_free(struct hda_codec *codec)
  218. {
  219. kfree(codec->spec);
  220. }
  221. /*
  222. */
  223. static const struct hda_codec_ops si3054_patch_ops = {
  224. .build_controls = si3054_build_controls,
  225. .build_pcms = si3054_build_pcms,
  226. .init = si3054_init,
  227. .free = si3054_free,
  228. };
  229. static int patch_si3054(struct hda_codec *codec)
  230. {
  231. struct si3054_spec *spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  232. if (spec == NULL)
  233. return -ENOMEM;
  234. codec->spec = spec;
  235. codec->patch_ops = si3054_patch_ops;
  236. return 0;
  237. }
  238. /*
  239. * patch entries
  240. */
  241. static const struct hda_codec_preset snd_hda_preset_si3054[] = {
  242. { .id = 0x163c3055, .name = "Si3054", .patch = patch_si3054 },
  243. { .id = 0x163c3155, .name = "Si3054", .patch = patch_si3054 },
  244. { .id = 0x11c13026, .name = "Si3054", .patch = patch_si3054 },
  245. { .id = 0x11c13055, .name = "Si3054", .patch = patch_si3054 },
  246. { .id = 0x11c13155, .name = "Si3054", .patch = patch_si3054 },
  247. { .id = 0x10573055, .name = "Si3054", .patch = patch_si3054 },
  248. { .id = 0x10573057, .name = "Si3054", .patch = patch_si3054 },
  249. { .id = 0x10573155, .name = "Si3054", .patch = patch_si3054 },
  250. /* VIA HDA on Clevo m540 */
  251. { .id = 0x11063288, .name = "Si3054", .patch = patch_si3054 },
  252. /* Asus A8J Modem (SM56) */
  253. { .id = 0x15433155, .name = "Si3054", .patch = patch_si3054 },
  254. /* LG LW20 modem */
  255. { .id = 0x18540018, .name = "Si3054", .patch = patch_si3054 },
  256. {}
  257. };
  258. MODULE_ALIAS("snd-hda-codec-id:163c3055");
  259. MODULE_ALIAS("snd-hda-codec-id:163c3155");
  260. MODULE_ALIAS("snd-hda-codec-id:11c13026");
  261. MODULE_ALIAS("snd-hda-codec-id:11c13055");
  262. MODULE_ALIAS("snd-hda-codec-id:11c13155");
  263. MODULE_ALIAS("snd-hda-codec-id:10573055");
  264. MODULE_ALIAS("snd-hda-codec-id:10573057");
  265. MODULE_ALIAS("snd-hda-codec-id:10573155");
  266. MODULE_ALIAS("snd-hda-codec-id:11063288");
  267. MODULE_ALIAS("snd-hda-codec-id:15433155");
  268. MODULE_ALIAS("snd-hda-codec-id:18540018");
  269. MODULE_LICENSE("GPL");
  270. MODULE_DESCRIPTION("Si3054 HD-audio modem codec");
  271. static struct hda_codec_preset_list si3054_list = {
  272. .preset = snd_hda_preset_si3054,
  273. .owner = THIS_MODULE,
  274. };
  275. static int __init patch_si3054_init(void)
  276. {
  277. return snd_hda_add_codec_preset(&si3054_list);
  278. }
  279. static void __exit patch_si3054_exit(void)
  280. {
  281. snd_hda_delete_codec_preset(&si3054_list);
  282. }
  283. module_init(patch_si3054_init)
  284. module_exit(patch_si3054_exit)