ac97_codec.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578
  1. /*
  2. * ac97_codec.c: Generic AC97 mixer/modem module
  3. *
  4. * Derived from ac97 mixer in maestro and trident driver.
  5. *
  6. * Copyright 2000 Silicon Integrated System Corporation
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. *
  22. **************************************************************************
  23. *
  24. * The Intel Audio Codec '97 specification is available at the Intel
  25. * audio homepage: http://developer.intel.com/ial/scalableplatforms/audio/
  26. *
  27. * The specification itself is currently available at:
  28. * ftp://download.intel.com/ial/scalableplatforms/ac97r22.pdf
  29. *
  30. **************************************************************************
  31. *
  32. * History
  33. * May 02, 2003 Liam Girdwood <liam.girdwood@wolfsonmicro.com>
  34. * Removed non existant WM9700
  35. * Added support for WM9705, WM9708, WM9709, WM9710, WM9711
  36. * WM9712 and WM9717
  37. * Mar 28, 2002 Randolph Bentson <bentson@holmsjoen.com>
  38. * corrections to support WM9707 in ViewPad 1000
  39. * v0.4 Mar 15 2000 Ollie Lho
  40. * dual codecs support verified with 4 channels output
  41. * v0.3 Feb 22 2000 Ollie Lho
  42. * bug fix for record mask setting
  43. * v0.2 Feb 10 2000 Ollie Lho
  44. * add ac97_read_proc for /proc/driver/{vendor}/ac97
  45. * v0.1 Jan 14 2000 Ollie Lho <ollie@sis.com.tw>
  46. * Isolated from trident.c to support multiple ac97 codec
  47. */
  48. #include <linux/module.h>
  49. #include <linux/kernel.h>
  50. #include <linux/slab.h>
  51. #include <linux/string.h>
  52. #include <linux/errno.h>
  53. #include <linux/bitops.h>
  54. #include <linux/delay.h>
  55. #include <linux/pci.h>
  56. #include <linux/ac97_codec.h>
  57. #include <asm/uaccess.h>
  58. #include <linux/mutex.h>
  59. #define CODEC_ID_BUFSZ 14
  60. static int ac97_read_mixer(struct ac97_codec *codec, int oss_channel);
  61. static void ac97_write_mixer(struct ac97_codec *codec, int oss_channel,
  62. unsigned int left, unsigned int right);
  63. static void ac97_set_mixer(struct ac97_codec *codec, unsigned int oss_mixer, unsigned int val );
  64. static int ac97_recmask_io(struct ac97_codec *codec, int rw, int mask);
  65. static int ac97_mixer_ioctl(struct ac97_codec *codec, unsigned int cmd, unsigned long arg);
  66. static int ac97_init_mixer(struct ac97_codec *codec);
  67. static int wolfson_init03(struct ac97_codec * codec);
  68. static int wolfson_init04(struct ac97_codec * codec);
  69. static int wolfson_init05(struct ac97_codec * codec);
  70. static int wolfson_init11(struct ac97_codec * codec);
  71. static int wolfson_init13(struct ac97_codec * codec);
  72. static int tritech_init(struct ac97_codec * codec);
  73. static int tritech_maestro_init(struct ac97_codec * codec);
  74. static int sigmatel_9708_init(struct ac97_codec *codec);
  75. static int sigmatel_9721_init(struct ac97_codec *codec);
  76. static int sigmatel_9744_init(struct ac97_codec *codec);
  77. static int ad1886_init(struct ac97_codec *codec);
  78. static int eapd_control(struct ac97_codec *codec, int);
  79. static int crystal_digital_control(struct ac97_codec *codec, int slots, int rate, int mode);
  80. static int cmedia_init(struct ac97_codec * codec);
  81. static int cmedia_digital_control(struct ac97_codec *codec, int slots, int rate, int mode);
  82. static int generic_digital_control(struct ac97_codec *codec, int slots, int rate, int mode);
  83. /*
  84. * AC97 operations.
  85. *
  86. * If you are adding a codec then you should be able to use
  87. * eapd_ops - any codec that supports EAPD amp control (most)
  88. * null_ops - any ancient codec that supports nothing
  89. *
  90. * The three functions are
  91. * init - used for non AC97 standard initialisation
  92. * amplifier - used to do amplifier control (1=on 0=off)
  93. * digital - switch to digital modes (0 = analog)
  94. *
  95. * Not all codecs support all features, not all drivers use all the
  96. * operations yet
  97. */
  98. static struct ac97_ops null_ops = { NULL, NULL, NULL };
  99. static struct ac97_ops default_ops = { NULL, eapd_control, NULL };
  100. static struct ac97_ops default_digital_ops = { NULL, eapd_control, generic_digital_control};
  101. static struct ac97_ops wolfson_ops03 = { wolfson_init03, NULL, NULL };
  102. static struct ac97_ops wolfson_ops04 = { wolfson_init04, NULL, NULL };
  103. static struct ac97_ops wolfson_ops05 = { wolfson_init05, NULL, NULL };
  104. static struct ac97_ops wolfson_ops11 = { wolfson_init11, NULL, NULL };
  105. static struct ac97_ops wolfson_ops13 = { wolfson_init13, NULL, NULL };
  106. static struct ac97_ops tritech_ops = { tritech_init, NULL, NULL };
  107. static struct ac97_ops tritech_m_ops = { tritech_maestro_init, NULL, NULL };
  108. static struct ac97_ops sigmatel_9708_ops = { sigmatel_9708_init, NULL, NULL };
  109. static struct ac97_ops sigmatel_9721_ops = { sigmatel_9721_init, NULL, NULL };
  110. static struct ac97_ops sigmatel_9744_ops = { sigmatel_9744_init, NULL, NULL };
  111. static struct ac97_ops crystal_digital_ops = { NULL, eapd_control, crystal_digital_control };
  112. static struct ac97_ops ad1886_ops = { ad1886_init, eapd_control, NULL };
  113. static struct ac97_ops cmedia_ops = { NULL, eapd_control, NULL};
  114. static struct ac97_ops cmedia_digital_ops = { cmedia_init, eapd_control, cmedia_digital_control};
  115. /* sorted by vendor/device id */
  116. static const struct {
  117. u32 id;
  118. char *name;
  119. struct ac97_ops *ops;
  120. int flags;
  121. } ac97_codec_ids[] = {
  122. {0x41445303, "Analog Devices AD1819", &null_ops},
  123. {0x41445340, "Analog Devices AD1881", &null_ops},
  124. {0x41445348, "Analog Devices AD1881A", &null_ops},
  125. {0x41445360, "Analog Devices AD1885", &default_ops},
  126. {0x41445361, "Analog Devices AD1886", &ad1886_ops},
  127. {0x41445370, "Analog Devices AD1981", &null_ops},
  128. {0x41445372, "Analog Devices AD1981A", &null_ops},
  129. {0x41445374, "Analog Devices AD1981B", &null_ops},
  130. {0x41445460, "Analog Devices AD1885", &default_ops},
  131. {0x41445461, "Analog Devices AD1886", &ad1886_ops},
  132. {0x414B4D00, "Asahi Kasei AK4540", &null_ops},
  133. {0x414B4D01, "Asahi Kasei AK4542", &null_ops},
  134. {0x414B4D02, "Asahi Kasei AK4543", &null_ops},
  135. {0x414C4326, "ALC100P", &null_ops},
  136. {0x414C4710, "ALC200/200P", &null_ops},
  137. {0x414C4720, "ALC650", &default_digital_ops},
  138. {0x434D4941, "CMedia", &cmedia_ops, AC97_NO_PCM_VOLUME },
  139. {0x434D4942, "CMedia", &cmedia_ops, AC97_NO_PCM_VOLUME },
  140. {0x434D4961, "CMedia", &cmedia_digital_ops, AC97_NO_PCM_VOLUME },
  141. {0x43525900, "Cirrus Logic CS4297", &default_ops},
  142. {0x43525903, "Cirrus Logic CS4297", &default_ops},
  143. {0x43525913, "Cirrus Logic CS4297A rev A", &default_ops},
  144. {0x43525914, "Cirrus Logic CS4297A rev B", &default_ops},
  145. {0x43525923, "Cirrus Logic CS4298", &null_ops},
  146. {0x4352592B, "Cirrus Logic CS4294", &null_ops},
  147. {0x4352592D, "Cirrus Logic CS4294", &null_ops},
  148. {0x43525931, "Cirrus Logic CS4299 rev A", &crystal_digital_ops},
  149. {0x43525933, "Cirrus Logic CS4299 rev C", &crystal_digital_ops},
  150. {0x43525934, "Cirrus Logic CS4299 rev D", &crystal_digital_ops},
  151. {0x43585430, "CXT48", &default_ops, AC97_DELUDED_MODEM },
  152. {0x43585442, "CXT66", &default_ops, AC97_DELUDED_MODEM },
  153. {0x44543031, "Diamond Technology DT0893", &default_ops},
  154. {0x45838308, "ESS Allegro ES1988", &null_ops},
  155. {0x49434511, "ICE1232", &null_ops}, /* I hope --jk */
  156. {0x4e534331, "National Semiconductor LM4549", &null_ops},
  157. {0x53494c22, "Silicon Laboratory Si3036", &null_ops},
  158. {0x53494c23, "Silicon Laboratory Si3038", &null_ops},
  159. {0x545200FF, "TriTech TR?????", &tritech_m_ops},
  160. {0x54524102, "TriTech TR28022", &null_ops},
  161. {0x54524103, "TriTech TR28023", &null_ops},
  162. {0x54524106, "TriTech TR28026", &null_ops},
  163. {0x54524108, "TriTech TR28028", &tritech_ops},
  164. {0x54524123, "TriTech TR A5", &null_ops},
  165. {0x574D4C03, "Wolfson WM9703/07/08/17", &wolfson_ops03},
  166. {0x574D4C04, "Wolfson WM9704M/WM9704Q", &wolfson_ops04},
  167. {0x574D4C05, "Wolfson WM9705/WM9710", &wolfson_ops05},
  168. {0x574D4C09, "Wolfson WM9709", &null_ops},
  169. {0x574D4C12, "Wolfson WM9711/9712", &wolfson_ops11},
  170. {0x574D4C13, "Wolfson WM9713", &wolfson_ops13, AC97_DEFAULT_POWER_OFF},
  171. {0x83847600, "SigmaTel STAC????", &null_ops},
  172. {0x83847604, "SigmaTel STAC9701/3/4/5", &null_ops},
  173. {0x83847605, "SigmaTel STAC9704", &null_ops},
  174. {0x83847608, "SigmaTel STAC9708", &sigmatel_9708_ops},
  175. {0x83847609, "SigmaTel STAC9721/23", &sigmatel_9721_ops},
  176. {0x83847644, "SigmaTel STAC9744/45", &sigmatel_9744_ops},
  177. {0x83847652, "SigmaTel STAC9752/53", &default_ops},
  178. {0x83847656, "SigmaTel STAC9756/57", &sigmatel_9744_ops},
  179. {0x83847666, "SigmaTel STAC9750T", &sigmatel_9744_ops},
  180. {0x83847684, "SigmaTel STAC9783/84?", &null_ops},
  181. {0x57454301, "Winbond 83971D", &null_ops},
  182. };
  183. static const char *ac97_stereo_enhancements[] =
  184. {
  185. /* 0 */ "No 3D Stereo Enhancement",
  186. /* 1 */ "Analog Devices Phat Stereo",
  187. /* 2 */ "Creative Stereo Enhancement",
  188. /* 3 */ "National Semi 3D Stereo Enhancement",
  189. /* 4 */ "YAMAHA Ymersion",
  190. /* 5 */ "BBE 3D Stereo Enhancement",
  191. /* 6 */ "Crystal Semi 3D Stereo Enhancement",
  192. /* 7 */ "Qsound QXpander",
  193. /* 8 */ "Spatializer 3D Stereo Enhancement",
  194. /* 9 */ "SRS 3D Stereo Enhancement",
  195. /* 10 */ "Platform Tech 3D Stereo Enhancement",
  196. /* 11 */ "AKM 3D Audio",
  197. /* 12 */ "Aureal Stereo Enhancement",
  198. /* 13 */ "Aztech 3D Enhancement",
  199. /* 14 */ "Binaura 3D Audio Enhancement",
  200. /* 15 */ "ESS Technology Stereo Enhancement",
  201. /* 16 */ "Harman International VMAx",
  202. /* 17 */ "Nvidea 3D Stereo Enhancement",
  203. /* 18 */ "Philips Incredible Sound",
  204. /* 19 */ "Texas Instruments 3D Stereo Enhancement",
  205. /* 20 */ "VLSI Technology 3D Stereo Enhancement",
  206. /* 21 */ "TriTech 3D Stereo Enhancement",
  207. /* 22 */ "Realtek 3D Stereo Enhancement",
  208. /* 23 */ "Samsung 3D Stereo Enhancement",
  209. /* 24 */ "Wolfson Microelectronics 3D Enhancement",
  210. /* 25 */ "Delta Integration 3D Enhancement",
  211. /* 26 */ "SigmaTel 3D Enhancement",
  212. /* 27 */ "Winbond 3D Stereo Enhancement",
  213. /* 28 */ "Rockwell 3D Stereo Enhancement",
  214. /* 29 */ "Reserved 29",
  215. /* 30 */ "Reserved 30",
  216. /* 31 */ "Reserved 31"
  217. };
  218. /* this table has default mixer values for all OSS mixers. */
  219. static struct mixer_defaults {
  220. int mixer;
  221. unsigned int value;
  222. } mixer_defaults[SOUND_MIXER_NRDEVICES] = {
  223. /* all values 0 -> 100 in bytes */
  224. {SOUND_MIXER_VOLUME, 0x4343},
  225. {SOUND_MIXER_BASS, 0x4343},
  226. {SOUND_MIXER_TREBLE, 0x4343},
  227. {SOUND_MIXER_PCM, 0x4343},
  228. {SOUND_MIXER_SPEAKER, 0x4343},
  229. {SOUND_MIXER_LINE, 0x4343},
  230. {SOUND_MIXER_MIC, 0x0000},
  231. {SOUND_MIXER_CD, 0x4343},
  232. {SOUND_MIXER_ALTPCM, 0x4343},
  233. {SOUND_MIXER_IGAIN, 0x4343},
  234. {SOUND_MIXER_LINE1, 0x4343},
  235. {SOUND_MIXER_PHONEIN, 0x4343},
  236. {SOUND_MIXER_PHONEOUT, 0x4343},
  237. {SOUND_MIXER_VIDEO, 0x4343},
  238. {-1,0}
  239. };
  240. /* table to scale scale from OSS mixer value to AC97 mixer register value */
  241. static struct ac97_mixer_hw {
  242. unsigned char offset;
  243. int scale;
  244. } ac97_hw[SOUND_MIXER_NRDEVICES]= {
  245. [SOUND_MIXER_VOLUME] = {AC97_MASTER_VOL_STEREO,64},
  246. [SOUND_MIXER_BASS] = {AC97_MASTER_TONE, 16},
  247. [SOUND_MIXER_TREBLE] = {AC97_MASTER_TONE, 16},
  248. [SOUND_MIXER_PCM] = {AC97_PCMOUT_VOL, 32},
  249. [SOUND_MIXER_SPEAKER] = {AC97_PCBEEP_VOL, 16},
  250. [SOUND_MIXER_LINE] = {AC97_LINEIN_VOL, 32},
  251. [SOUND_MIXER_MIC] = {AC97_MIC_VOL, 32},
  252. [SOUND_MIXER_CD] = {AC97_CD_VOL, 32},
  253. [SOUND_MIXER_ALTPCM] = {AC97_HEADPHONE_VOL, 64},
  254. [SOUND_MIXER_IGAIN] = {AC97_RECORD_GAIN, 16},
  255. [SOUND_MIXER_LINE1] = {AC97_AUX_VOL, 32},
  256. [SOUND_MIXER_PHONEIN] = {AC97_PHONE_VOL, 32},
  257. [SOUND_MIXER_PHONEOUT] = {AC97_MASTER_VOL_MONO, 64},
  258. [SOUND_MIXER_VIDEO] = {AC97_VIDEO_VOL, 32},
  259. };
  260. /* the following tables allow us to go from OSS <-> ac97 quickly. */
  261. enum ac97_recsettings {
  262. AC97_REC_MIC=0,
  263. AC97_REC_CD,
  264. AC97_REC_VIDEO,
  265. AC97_REC_AUX,
  266. AC97_REC_LINE,
  267. AC97_REC_STEREO, /* combination of all enabled outputs.. */
  268. AC97_REC_MONO, /*.. or the mono equivalent */
  269. AC97_REC_PHONE
  270. };
  271. static const unsigned int ac97_rm2oss[] = {
  272. [AC97_REC_MIC] = SOUND_MIXER_MIC,
  273. [AC97_REC_CD] = SOUND_MIXER_CD,
  274. [AC97_REC_VIDEO] = SOUND_MIXER_VIDEO,
  275. [AC97_REC_AUX] = SOUND_MIXER_LINE1,
  276. [AC97_REC_LINE] = SOUND_MIXER_LINE,
  277. [AC97_REC_STEREO]= SOUND_MIXER_IGAIN,
  278. [AC97_REC_PHONE] = SOUND_MIXER_PHONEIN
  279. };
  280. /* indexed by bit position */
  281. static const unsigned int ac97_oss_rm[] = {
  282. [SOUND_MIXER_MIC] = AC97_REC_MIC,
  283. [SOUND_MIXER_CD] = AC97_REC_CD,
  284. [SOUND_MIXER_VIDEO] = AC97_REC_VIDEO,
  285. [SOUND_MIXER_LINE1] = AC97_REC_AUX,
  286. [SOUND_MIXER_LINE] = AC97_REC_LINE,
  287. [SOUND_MIXER_IGAIN] = AC97_REC_STEREO,
  288. [SOUND_MIXER_PHONEIN] = AC97_REC_PHONE
  289. };
  290. static LIST_HEAD(codecs);
  291. static LIST_HEAD(codec_drivers);
  292. static DEFINE_MUTEX(codec_mutex);
  293. /* reads the given OSS mixer from the ac97 the caller must have insured that the ac97 knows
  294. about that given mixer, and should be holding a spinlock for the card */
  295. static int ac97_read_mixer(struct ac97_codec *codec, int oss_channel)
  296. {
  297. u16 val;
  298. int ret = 0;
  299. int scale;
  300. struct ac97_mixer_hw *mh = &ac97_hw[oss_channel];
  301. val = codec->codec_read(codec , mh->offset);
  302. if (val & AC97_MUTE) {
  303. ret = 0;
  304. } else if (AC97_STEREO_MASK & (1 << oss_channel)) {
  305. /* nice stereo mixers .. */
  306. int left,right;
  307. left = (val >> 8) & 0x7f;
  308. right = val & 0x7f;
  309. if (oss_channel == SOUND_MIXER_IGAIN) {
  310. right = (right * 100) / mh->scale;
  311. left = (left * 100) / mh->scale;
  312. } else {
  313. /* these may have 5 or 6 bit resolution */
  314. if(oss_channel == SOUND_MIXER_VOLUME || oss_channel == SOUND_MIXER_ALTPCM)
  315. scale = (1 << codec->bit_resolution);
  316. else
  317. scale = mh->scale;
  318. right = 100 - ((right * 100) / scale);
  319. left = 100 - ((left * 100) / scale);
  320. }
  321. ret = left | (right << 8);
  322. } else if (oss_channel == SOUND_MIXER_SPEAKER) {
  323. ret = 100 - ((((val & 0x1e)>>1) * 100) / mh->scale);
  324. } else if (oss_channel == SOUND_MIXER_PHONEIN) {
  325. ret = 100 - (((val & 0x1f) * 100) / mh->scale);
  326. } else if (oss_channel == SOUND_MIXER_PHONEOUT) {
  327. scale = (1 << codec->bit_resolution);
  328. ret = 100 - (((val & 0x1f) * 100) / scale);
  329. } else if (oss_channel == SOUND_MIXER_MIC) {
  330. ret = 100 - (((val & 0x1f) * 100) / mh->scale);
  331. /* the low bit is optional in the tone sliders and masking
  332. it lets us avoid the 0xf 'bypass'.. */
  333. } else if (oss_channel == SOUND_MIXER_BASS) {
  334. ret = 100 - ((((val >> 8) & 0xe) * 100) / mh->scale);
  335. } else if (oss_channel == SOUND_MIXER_TREBLE) {
  336. ret = 100 - (((val & 0xe) * 100) / mh->scale);
  337. }
  338. #ifdef DEBUG
  339. printk("ac97_codec: read OSS mixer %2d (%s ac97 register 0x%02x), "
  340. "0x%04x -> 0x%04x\n",
  341. oss_channel, codec->id ? "Secondary" : "Primary",
  342. mh->offset, val, ret);
  343. #endif
  344. return ret;
  345. }
  346. /* write the OSS encoded volume to the given OSS encoded mixer, again caller's job to
  347. make sure all is well in arg land, call with spinlock held */
  348. static void ac97_write_mixer(struct ac97_codec *codec, int oss_channel,
  349. unsigned int left, unsigned int right)
  350. {
  351. u16 val = 0;
  352. int scale;
  353. struct ac97_mixer_hw *mh = &ac97_hw[oss_channel];
  354. #ifdef DEBUG
  355. printk("ac97_codec: wrote OSS mixer %2d (%s ac97 register 0x%02x), "
  356. "left vol:%2d, right vol:%2d:",
  357. oss_channel, codec->id ? "Secondary" : "Primary",
  358. mh->offset, left, right);
  359. #endif
  360. if (AC97_STEREO_MASK & (1 << oss_channel)) {
  361. /* stereo mixers */
  362. if (left == 0 && right == 0) {
  363. val = AC97_MUTE;
  364. } else {
  365. if (oss_channel == SOUND_MIXER_IGAIN) {
  366. right = (right * mh->scale) / 100;
  367. left = (left * mh->scale) / 100;
  368. if (right >= mh->scale)
  369. right = mh->scale-1;
  370. if (left >= mh->scale)
  371. left = mh->scale-1;
  372. } else {
  373. /* these may have 5 or 6 bit resolution */
  374. if (oss_channel == SOUND_MIXER_VOLUME ||
  375. oss_channel == SOUND_MIXER_ALTPCM)
  376. scale = (1 << codec->bit_resolution);
  377. else
  378. scale = mh->scale;
  379. right = ((100 - right) * scale) / 100;
  380. left = ((100 - left) * scale) / 100;
  381. if (right >= scale)
  382. right = scale-1;
  383. if (left >= scale)
  384. left = scale-1;
  385. }
  386. val = (left << 8) | right;
  387. }
  388. } else if (oss_channel == SOUND_MIXER_BASS) {
  389. val = codec->codec_read(codec , mh->offset) & ~0x0f00;
  390. left = ((100 - left) * mh->scale) / 100;
  391. if (left >= mh->scale)
  392. left = mh->scale-1;
  393. val |= (left << 8) & 0x0e00;
  394. } else if (oss_channel == SOUND_MIXER_TREBLE) {
  395. val = codec->codec_read(codec , mh->offset) & ~0x000f;
  396. left = ((100 - left) * mh->scale) / 100;
  397. if (left >= mh->scale)
  398. left = mh->scale-1;
  399. val |= left & 0x000e;
  400. } else if(left == 0) {
  401. val = AC97_MUTE;
  402. } else if (oss_channel == SOUND_MIXER_SPEAKER) {
  403. left = ((100 - left) * mh->scale) / 100;
  404. if (left >= mh->scale)
  405. left = mh->scale-1;
  406. val = left << 1;
  407. } else if (oss_channel == SOUND_MIXER_PHONEIN) {
  408. left = ((100 - left) * mh->scale) / 100;
  409. if (left >= mh->scale)
  410. left = mh->scale-1;
  411. val = left;
  412. } else if (oss_channel == SOUND_MIXER_PHONEOUT) {
  413. scale = (1 << codec->bit_resolution);
  414. left = ((100 - left) * scale) / 100;
  415. if (left >= mh->scale)
  416. left = mh->scale-1;
  417. val = left;
  418. } else if (oss_channel == SOUND_MIXER_MIC) {
  419. val = codec->codec_read(codec , mh->offset) & ~0x801f;
  420. left = ((100 - left) * mh->scale) / 100;
  421. if (left >= mh->scale)
  422. left = mh->scale-1;
  423. val |= left;
  424. /* the low bit is optional in the tone sliders and masking
  425. it lets us avoid the 0xf 'bypass'.. */
  426. }
  427. #ifdef DEBUG
  428. printk(" 0x%04x", val);
  429. #endif
  430. codec->codec_write(codec, mh->offset, val);
  431. #ifdef DEBUG
  432. val = codec->codec_read(codec, mh->offset);
  433. printk(" -> 0x%04x\n", val);
  434. #endif
  435. }
  436. /* a thin wrapper for write_mixer */
  437. static void ac97_set_mixer(struct ac97_codec *codec, unsigned int oss_mixer, unsigned int val )
  438. {
  439. unsigned int left,right;
  440. /* cleanse input a little */
  441. right = ((val >> 8) & 0xff) ;
  442. left = (val & 0xff) ;
  443. if (right > 100) right = 100;
  444. if (left > 100) left = 100;
  445. codec->mixer_state[oss_mixer] = (right << 8) | left;
  446. codec->write_mixer(codec, oss_mixer, left, right);
  447. }
  448. /* read or write the recmask, the ac97 can really have left and right recording
  449. inputs independantly set, but OSS doesn't seem to want us to express that to
  450. the user. the caller guarantees that we have a supported bit set, and they
  451. must be holding the card's spinlock */
  452. static int ac97_recmask_io(struct ac97_codec *codec, int rw, int mask)
  453. {
  454. unsigned int val;
  455. if (rw) {
  456. /* read it from the card */
  457. val = codec->codec_read(codec, AC97_RECORD_SELECT);
  458. #ifdef DEBUG
  459. printk("ac97_codec: ac97 recmask to set to 0x%04x\n", val);
  460. #endif
  461. return (1 << ac97_rm2oss[val & 0x07]);
  462. }
  463. /* else, write the first set in the mask as the
  464. output */
  465. /* clear out current set value first (AC97 supports only 1 input!) */
  466. val = (1 << ac97_rm2oss[codec->codec_read(codec, AC97_RECORD_SELECT) & 0x07]);
  467. if (mask != val)
  468. mask &= ~val;
  469. val = ffs(mask);
  470. val = ac97_oss_rm[val-1];
  471. val |= val << 8; /* set both channels */
  472. #ifdef DEBUG
  473. printk("ac97_codec: setting ac97 recmask to 0x%04x\n", val);
  474. #endif
  475. codec->codec_write(codec, AC97_RECORD_SELECT, val);
  476. return 0;
  477. };
  478. static int ac97_mixer_ioctl(struct ac97_codec *codec, unsigned int cmd, unsigned long arg)
  479. {
  480. int i, val = 0;
  481. if (cmd == SOUND_MIXER_INFO) {
  482. mixer_info info;
  483. memset(&info, 0, sizeof(info));
  484. strlcpy(info.id, codec->name, sizeof(info.id));
  485. strlcpy(info.name, codec->name, sizeof(info.name));
  486. info.modify_counter = codec->modcnt;
  487. if (copy_to_user((void __user *)arg, &info, sizeof(info)))
  488. return -EFAULT;
  489. return 0;
  490. }
  491. if (cmd == SOUND_OLD_MIXER_INFO) {
  492. _old_mixer_info info;
  493. memset(&info, 0, sizeof(info));
  494. strlcpy(info.id, codec->name, sizeof(info.id));
  495. strlcpy(info.name, codec->name, sizeof(info.name));
  496. if (copy_to_user((void __user *)arg, &info, sizeof(info)))
  497. return -EFAULT;
  498. return 0;
  499. }
  500. if (_IOC_TYPE(cmd) != 'M' || _SIOC_SIZE(cmd) != sizeof(int))
  501. return -EINVAL;
  502. if (cmd == OSS_GETVERSION)
  503. return put_user(SOUND_VERSION, (int __user *)arg);
  504. if (_SIOC_DIR(cmd) == _SIOC_READ) {
  505. switch (_IOC_NR(cmd)) {
  506. case SOUND_MIXER_RECSRC: /* give them the current record source */
  507. if (!codec->recmask_io) {
  508. val = 0;
  509. } else {
  510. val = codec->recmask_io(codec, 1, 0);
  511. }
  512. break;
  513. case SOUND_MIXER_DEVMASK: /* give them the supported mixers */
  514. val = codec->supported_mixers;
  515. break;
  516. case SOUND_MIXER_RECMASK: /* Arg contains a bit for each supported recording source */
  517. val = codec->record_sources;
  518. break;
  519. case SOUND_MIXER_STEREODEVS: /* Mixer channels supporting stereo */
  520. val = codec->stereo_mixers;
  521. break;
  522. case SOUND_MIXER_CAPS:
  523. val = SOUND_CAP_EXCL_INPUT;
  524. break;
  525. default: /* read a specific mixer */
  526. i = _IOC_NR(cmd);
  527. if (!supported_mixer(codec, i))
  528. return -EINVAL;
  529. /* do we ever want to touch the hardware? */
  530. /* val = codec->read_mixer(codec, i); */
  531. val = codec->mixer_state[i];
  532. break;
  533. }
  534. return put_user(val, (int __user *)arg);
  535. }
  536. if (_SIOC_DIR(cmd) == (_SIOC_WRITE|_SIOC_READ)) {
  537. codec->modcnt++;
  538. if (get_user(val, (int __user *)arg))
  539. return -EFAULT;
  540. switch (_IOC_NR(cmd)) {
  541. case SOUND_MIXER_RECSRC: /* Arg contains a bit for each recording source */
  542. if (!codec->recmask_io) return -EINVAL;
  543. if (!val) return 0;
  544. if (!(val &= codec->record_sources)) return -EINVAL;
  545. codec->recmask_io(codec, 0, val);
  546. return 0;
  547. default: /* write a specific mixer */
  548. i = _IOC_NR(cmd);
  549. if (!supported_mixer(codec, i))
  550. return -EINVAL;
  551. ac97_set_mixer(codec, i, val);
  552. return 0;
  553. }
  554. }
  555. return -EINVAL;
  556. }
  557. /* entry point for /proc/driver/controller_vendor/ac97/%d */
  558. int ac97_read_proc (char *page, char **start, off_t off,
  559. int count, int *eof, void *data)
  560. {
  561. int len = 0, cap, extid, val, id1, id2;
  562. struct ac97_codec *codec;
  563. int is_ac97_20 = 0;
  564. if ((codec = data) == NULL)
  565. return -ENODEV;
  566. id1 = codec->codec_read(codec, AC97_VENDOR_ID1);
  567. id2 = codec->codec_read(codec, AC97_VENDOR_ID2);
  568. len += sprintf (page+len, "Vendor name : %s\n", codec->name);
  569. len += sprintf (page+len, "Vendor id : %04X %04X\n", id1, id2);
  570. extid = codec->codec_read(codec, AC97_EXTENDED_ID);
  571. extid &= ~((1<<2)|(1<<4)|(1<<5)|(1<<10)|(1<<11)|(1<<12)|(1<<13));
  572. len += sprintf (page+len, "AC97 Version : %s\n",
  573. extid ? "2.0 or later" : "1.0");
  574. if (extid) is_ac97_20 = 1;
  575. cap = codec->codec_read(codec, AC97_RESET);
  576. len += sprintf (page+len, "Capabilities :%s%s%s%s%s%s\n",
  577. cap & 0x0001 ? " -dedicated MIC PCM IN channel-" : "",
  578. cap & 0x0002 ? " -reserved1-" : "",
  579. cap & 0x0004 ? " -bass & treble-" : "",
  580. cap & 0x0008 ? " -simulated stereo-" : "",
  581. cap & 0x0010 ? " -headphone out-" : "",
  582. cap & 0x0020 ? " -loudness-" : "");
  583. val = cap & 0x00c0;
  584. len += sprintf (page+len, "DAC resolutions :%s%s%s\n",
  585. " -16-bit-",
  586. val & 0x0040 ? " -18-bit-" : "",
  587. val & 0x0080 ? " -20-bit-" : "");
  588. val = cap & 0x0300;
  589. len += sprintf (page+len, "ADC resolutions :%s%s%s\n",
  590. " -16-bit-",
  591. val & 0x0100 ? " -18-bit-" : "",
  592. val & 0x0200 ? " -20-bit-" : "");
  593. len += sprintf (page+len, "3D enhancement : %s\n",
  594. ac97_stereo_enhancements[(cap >> 10) & 0x1f]);
  595. val = codec->codec_read(codec, AC97_GENERAL_PURPOSE);
  596. len += sprintf (page+len, "POP path : %s 3D\n"
  597. "Sim. stereo : %s\n"
  598. "3D enhancement : %s\n"
  599. "Loudness : %s\n"
  600. "Mono output : %s\n"
  601. "MIC select : %s\n"
  602. "ADC/DAC loopback : %s\n",
  603. val & 0x8000 ? "post" : "pre",
  604. val & 0x4000 ? "on" : "off",
  605. val & 0x2000 ? "on" : "off",
  606. val & 0x1000 ? "on" : "off",
  607. val & 0x0200 ? "MIC" : "MIX",
  608. val & 0x0100 ? "MIC2" : "MIC1",
  609. val & 0x0080 ? "on" : "off");
  610. extid = codec->codec_read(codec, AC97_EXTENDED_ID);
  611. cap = extid;
  612. len += sprintf (page+len, "Ext Capabilities :%s%s%s%s%s%s%s\n",
  613. cap & 0x0001 ? " -var rate PCM audio-" : "",
  614. cap & 0x0002 ? " -2x PCM audio out-" : "",
  615. cap & 0x0008 ? " -var rate MIC in-" : "",
  616. cap & 0x0040 ? " -PCM center DAC-" : "",
  617. cap & 0x0080 ? " -PCM surround DAC-" : "",
  618. cap & 0x0100 ? " -PCM LFE DAC-" : "",
  619. cap & 0x0200 ? " -slot/DAC mappings-" : "");
  620. if (is_ac97_20) {
  621. len += sprintf (page+len, "Front DAC rate : %d\n",
  622. codec->codec_read(codec, AC97_PCM_FRONT_DAC_RATE));
  623. }
  624. return len;
  625. }
  626. /**
  627. * codec_id - Turn id1/id2 into a PnP string
  628. * @id1: Vendor ID1
  629. * @id2: Vendor ID2
  630. * @buf: CODEC_ID_BUFSZ byte buffer
  631. *
  632. * Fills buf with a zero terminated PnP ident string for the id1/id2
  633. * pair. For convenience the return is the passed in buffer pointer.
  634. */
  635. static char *codec_id(u16 id1, u16 id2, char *buf)
  636. {
  637. if(id1&0x8080) {
  638. snprintf(buf, CODEC_ID_BUFSZ, "0x%04x:0x%04x", id1, id2);
  639. } else {
  640. buf[0] = (id1 >> 8);
  641. buf[1] = (id1 & 0xFF);
  642. buf[2] = (id2 >> 8);
  643. snprintf(buf+3, CODEC_ID_BUFSZ - 3, "%d", id2&0xFF);
  644. }
  645. return buf;
  646. }
  647. /**
  648. * ac97_check_modem - Check if the Codec is a modem
  649. * @codec: codec to check
  650. *
  651. * Return true if the device is an AC97 1.0 or AC97 2.0 modem
  652. */
  653. static int ac97_check_modem(struct ac97_codec *codec)
  654. {
  655. /* Check for an AC97 1.0 soft modem (ID1) */
  656. if(codec->codec_read(codec, AC97_RESET) & 2)
  657. return 1;
  658. /* Check for an AC97 2.x soft modem */
  659. codec->codec_write(codec, AC97_EXTENDED_MODEM_ID, 0L);
  660. if(codec->codec_read(codec, AC97_EXTENDED_MODEM_ID) & 1)
  661. return 1;
  662. return 0;
  663. }
  664. /**
  665. * ac97_alloc_codec - Allocate an AC97 codec
  666. *
  667. * Returns a new AC97 codec structure. AC97 codecs may become
  668. * refcounted soon so this interface is needed. Returns with
  669. * one reference taken.
  670. */
  671. struct ac97_codec *ac97_alloc_codec(void)
  672. {
  673. struct ac97_codec *codec = kmalloc(sizeof(struct ac97_codec), GFP_KERNEL);
  674. if(!codec)
  675. return NULL;
  676. memset(codec, 0, sizeof(*codec));
  677. spin_lock_init(&codec->lock);
  678. INIT_LIST_HEAD(&codec->list);
  679. return codec;
  680. }
  681. EXPORT_SYMBOL(ac97_alloc_codec);
  682. /**
  683. * ac97_release_codec - Release an AC97 codec
  684. * @codec: codec to release
  685. *
  686. * Release an allocated AC97 codec. This will be refcounted in
  687. * time but for the moment is trivial. Calls the unregister
  688. * handler if the codec is now defunct.
  689. */
  690. void ac97_release_codec(struct ac97_codec *codec)
  691. {
  692. /* Remove from the list first, we don't want to be
  693. "rediscovered" */
  694. mutex_lock(&codec_mutex);
  695. list_del(&codec->list);
  696. mutex_unlock(&codec_mutex);
  697. /*
  698. * The driver needs to deal with internal
  699. * locking to avoid accidents here.
  700. */
  701. if(codec->driver)
  702. codec->driver->remove(codec, codec->driver);
  703. kfree(codec);
  704. }
  705. EXPORT_SYMBOL(ac97_release_codec);
  706. /**
  707. * ac97_probe_codec - Initialize and setup AC97-compatible codec
  708. * @codec: (in/out) Kernel info for a single AC97 codec
  709. *
  710. * Reset the AC97 codec, then initialize the mixer and
  711. * the rest of the @codec structure.
  712. *
  713. * The codec_read and codec_write fields of @codec are
  714. * required to be setup and working when this function
  715. * is called. All other fields are set by this function.
  716. *
  717. * codec_wait field of @codec can optionally be provided
  718. * when calling this function. If codec_wait is not %NULL,
  719. * this function will call codec_wait any time it is
  720. * necessary to wait for the audio chip to reach the
  721. * codec-ready state. If codec_wait is %NULL, then
  722. * the default behavior is to call schedule_timeout.
  723. * Currently codec_wait is used to wait for AC97 codec
  724. * reset to complete.
  725. *
  726. * Some codecs will power down when a register reset is
  727. * performed. We now check for such codecs.
  728. *
  729. * Returns 1 (true) on success, or 0 (false) on failure.
  730. */
  731. int ac97_probe_codec(struct ac97_codec *codec)
  732. {
  733. u16 id1, id2;
  734. u16 audio;
  735. int i;
  736. char cidbuf[CODEC_ID_BUFSZ];
  737. u16 f;
  738. struct list_head *l;
  739. struct ac97_driver *d;
  740. /* wait for codec-ready state */
  741. if (codec->codec_wait)
  742. codec->codec_wait(codec);
  743. else
  744. udelay(10);
  745. /* will the codec power down if register reset ? */
  746. id1 = codec->codec_read(codec, AC97_VENDOR_ID1);
  747. id2 = codec->codec_read(codec, AC97_VENDOR_ID2);
  748. codec->name = NULL;
  749. codec->codec_ops = &null_ops;
  750. for (i = 0; i < ARRAY_SIZE(ac97_codec_ids); i++) {
  751. if (ac97_codec_ids[i].id == ((id1 << 16) | id2)) {
  752. codec->type = ac97_codec_ids[i].id;
  753. codec->name = ac97_codec_ids[i].name;
  754. codec->codec_ops = ac97_codec_ids[i].ops;
  755. codec->flags = ac97_codec_ids[i].flags;
  756. break;
  757. }
  758. }
  759. codec->model = (id1 << 16) | id2;
  760. if ((codec->flags & AC97_DEFAULT_POWER_OFF) == 0) {
  761. /* reset codec and wait for the ready bit before we continue */
  762. codec->codec_write(codec, AC97_RESET, 0L);
  763. if (codec->codec_wait)
  764. codec->codec_wait(codec);
  765. else
  766. udelay(10);
  767. }
  768. /* probing AC97 codec, AC97 2.0 says that bit 15 of register 0x00 (reset) should
  769. * be read zero.
  770. *
  771. * FIXME: is the following comment outdated? -jgarzik
  772. * Probing of AC97 in this way is not reliable, it is not even SAFE !!
  773. */
  774. if ((audio = codec->codec_read(codec, AC97_RESET)) & 0x8000) {
  775. printk(KERN_ERR "ac97_codec: %s ac97 codec not present\n",
  776. (codec->id & 0x2) ? (codec->id&1 ? "4th" : "Tertiary")
  777. : (codec->id&1 ? "Secondary": "Primary"));
  778. return 0;
  779. }
  780. /* probe for Modem Codec */
  781. codec->modem = ac97_check_modem(codec);
  782. /* enable SPDIF */
  783. f = codec->codec_read(codec, AC97_EXTENDED_STATUS);
  784. if((codec->codec_ops == &null_ops) && (f & 4))
  785. codec->codec_ops = &default_digital_ops;
  786. /* A device which thinks its a modem but isnt */
  787. if(codec->flags & AC97_DELUDED_MODEM)
  788. codec->modem = 0;
  789. if (codec->name == NULL)
  790. codec->name = "Unknown";
  791. printk(KERN_INFO "ac97_codec: AC97 %s codec, id: %s (%s)\n",
  792. codec->modem ? "Modem" : (audio ? "Audio" : ""),
  793. codec_id(id1, id2, cidbuf), codec->name);
  794. if(!ac97_init_mixer(codec))
  795. return 0;
  796. /*
  797. * Attach last so the caller can override the mixer
  798. * callbacks.
  799. */
  800. mutex_lock(&codec_mutex);
  801. list_add(&codec->list, &codecs);
  802. list_for_each(l, &codec_drivers) {
  803. d = list_entry(l, struct ac97_driver, list);
  804. if ((codec->model ^ d->codec_id) & d->codec_mask)
  805. continue;
  806. if(d->probe(codec, d) == 0)
  807. {
  808. codec->driver = d;
  809. break;
  810. }
  811. }
  812. mutex_unlock(&codec_mutex);
  813. return 1;
  814. }
  815. static int ac97_init_mixer(struct ac97_codec *codec)
  816. {
  817. u16 cap;
  818. int i;
  819. cap = codec->codec_read(codec, AC97_RESET);
  820. /* mixer masks */
  821. codec->supported_mixers = AC97_SUPPORTED_MASK;
  822. codec->stereo_mixers = AC97_STEREO_MASK;
  823. codec->record_sources = AC97_RECORD_MASK;
  824. if (!(cap & 0x04))
  825. codec->supported_mixers &= ~(SOUND_MASK_BASS|SOUND_MASK_TREBLE);
  826. if (!(cap & 0x10))
  827. codec->supported_mixers &= ~SOUND_MASK_ALTPCM;
  828. /* detect bit resolution */
  829. codec->codec_write(codec, AC97_MASTER_VOL_STEREO, 0x2020);
  830. if(codec->codec_read(codec, AC97_MASTER_VOL_STEREO) == 0x2020)
  831. codec->bit_resolution = 6;
  832. else
  833. codec->bit_resolution = 5;
  834. /* generic OSS to AC97 wrapper */
  835. codec->read_mixer = ac97_read_mixer;
  836. codec->write_mixer = ac97_write_mixer;
  837. codec->recmask_io = ac97_recmask_io;
  838. codec->mixer_ioctl = ac97_mixer_ioctl;
  839. /* initialize mixer channel volumes */
  840. for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
  841. struct mixer_defaults *md = &mixer_defaults[i];
  842. if (md->mixer == -1)
  843. break;
  844. if (!supported_mixer(codec, md->mixer))
  845. continue;
  846. ac97_set_mixer(codec, md->mixer, md->value);
  847. }
  848. /* codec specific initialization for 4-6 channel output or secondary codec stuff */
  849. if (codec->codec_ops->init != NULL) {
  850. codec->codec_ops->init(codec);
  851. }
  852. /*
  853. * Volume is MUTE only on this device. We have to initialise
  854. * it but its useless beyond that.
  855. */
  856. if(codec->flags & AC97_NO_PCM_VOLUME)
  857. {
  858. codec->supported_mixers &= ~SOUND_MASK_PCM;
  859. printk(KERN_WARNING "AC97 codec does not have proper volume support.\n");
  860. }
  861. return 1;
  862. }
  863. #define AC97_SIGMATEL_ANALOG 0x6c /* Analog Special */
  864. #define AC97_SIGMATEL_DAC2INVERT 0x6e
  865. #define AC97_SIGMATEL_BIAS1 0x70
  866. #define AC97_SIGMATEL_BIAS2 0x72
  867. #define AC97_SIGMATEL_MULTICHN 0x74 /* Multi-Channel programming */
  868. #define AC97_SIGMATEL_CIC1 0x76
  869. #define AC97_SIGMATEL_CIC2 0x78
  870. static int sigmatel_9708_init(struct ac97_codec * codec)
  871. {
  872. u16 codec72, codec6c;
  873. codec72 = codec->codec_read(codec, AC97_SIGMATEL_BIAS2) & 0x8000;
  874. codec6c = codec->codec_read(codec, AC97_SIGMATEL_ANALOG);
  875. if ((codec72==0) && (codec6c==0)) {
  876. codec->codec_write(codec, AC97_SIGMATEL_CIC1, 0xabba);
  877. codec->codec_write(codec, AC97_SIGMATEL_CIC2, 0x1000);
  878. codec->codec_write(codec, AC97_SIGMATEL_BIAS1, 0xabba);
  879. codec->codec_write(codec, AC97_SIGMATEL_BIAS2, 0x0007);
  880. } else if ((codec72==0x8000) && (codec6c==0)) {
  881. codec->codec_write(codec, AC97_SIGMATEL_CIC1, 0xabba);
  882. codec->codec_write(codec, AC97_SIGMATEL_CIC2, 0x1001);
  883. codec->codec_write(codec, AC97_SIGMATEL_DAC2INVERT, 0x0008);
  884. } else if ((codec72==0x8000) && (codec6c==0x0080)) {
  885. /* nothing */
  886. }
  887. codec->codec_write(codec, AC97_SIGMATEL_MULTICHN, 0x0000);
  888. return 0;
  889. }
  890. static int sigmatel_9721_init(struct ac97_codec * codec)
  891. {
  892. /* Only set up secondary codec */
  893. if (codec->id == 0)
  894. return 0;
  895. codec->codec_write(codec, AC97_SURROUND_MASTER, 0L);
  896. /* initialize SigmaTel STAC9721/23 as secondary codec, decoding AC link
  897. sloc 3,4 = 0x01, slot 7,8 = 0x00, */
  898. codec->codec_write(codec, AC97_SIGMATEL_MULTICHN, 0x00);
  899. /* we don't have the crystal when we are on an AMR card, so use
  900. BIT_CLK as our clock source. Write the magic word ABBA and read
  901. back to enable register 0x78 */
  902. codec->codec_write(codec, AC97_SIGMATEL_CIC1, 0xabba);
  903. codec->codec_read(codec, AC97_SIGMATEL_CIC1);
  904. /* sync all the clocks*/
  905. codec->codec_write(codec, AC97_SIGMATEL_CIC2, 0x3802);
  906. return 0;
  907. }
  908. static int sigmatel_9744_init(struct ac97_codec * codec)
  909. {
  910. // patch for SigmaTel
  911. codec->codec_write(codec, AC97_SIGMATEL_CIC1, 0xabba);
  912. codec->codec_write(codec, AC97_SIGMATEL_CIC2, 0x0000); // is this correct? --jk
  913. codec->codec_write(codec, AC97_SIGMATEL_BIAS1, 0xabba);
  914. codec->codec_write(codec, AC97_SIGMATEL_BIAS2, 0x0002);
  915. codec->codec_write(codec, AC97_SIGMATEL_MULTICHN, 0x0000);
  916. return 0;
  917. }
  918. static int cmedia_init(struct ac97_codec *codec)
  919. {
  920. /* Initialise the CMedia 9739 */
  921. /*
  922. We could set various options here
  923. Register 0x20 bit 0x100 sets mic as center bass
  924. Also do multi_channel_ctrl &=~0x3000 |=0x1000
  925. For now we set up the GPIO and PC beep
  926. */
  927. u16 v;
  928. /* MIC */
  929. codec->codec_write(codec, 0x64, 0x3000);
  930. v = codec->codec_read(codec, 0x64);
  931. v &= ~0x8000;
  932. codec->codec_write(codec, 0x64, v);
  933. codec->codec_write(codec, 0x70, 0x0100);
  934. codec->codec_write(codec, 0x72, 0x0020);
  935. return 0;
  936. }
  937. #define AC97_WM97XX_FMIXER_VOL 0x72
  938. #define AC97_WM97XX_RMIXER_VOL 0x74
  939. #define AC97_WM97XX_TEST 0x5a
  940. #define AC97_WM9704_RPCM_VOL 0x70
  941. #define AC97_WM9711_OUT3VOL 0x16
  942. static int wolfson_init03(struct ac97_codec * codec)
  943. {
  944. /* this is known to work for the ViewSonic ViewPad 1000 */
  945. codec->codec_write(codec, AC97_WM97XX_FMIXER_VOL, 0x0808);
  946. codec->codec_write(codec, AC97_GENERAL_PURPOSE, 0x8000);
  947. return 0;
  948. }
  949. static int wolfson_init04(struct ac97_codec * codec)
  950. {
  951. codec->codec_write(codec, AC97_WM97XX_FMIXER_VOL, 0x0808);
  952. codec->codec_write(codec, AC97_WM97XX_RMIXER_VOL, 0x0808);
  953. // patch for DVD noise
  954. codec->codec_write(codec, AC97_WM97XX_TEST, 0x0200);
  955. // init vol as PCM vol
  956. codec->codec_write(codec, AC97_WM9704_RPCM_VOL,
  957. codec->codec_read(codec, AC97_PCMOUT_VOL));
  958. /* set rear surround volume */
  959. codec->codec_write(codec, AC97_SURROUND_MASTER, 0x0000);
  960. return 0;
  961. }
  962. /* WM9705, WM9710 */
  963. static int wolfson_init05(struct ac97_codec * codec)
  964. {
  965. /* set front mixer volume */
  966. codec->codec_write(codec, AC97_WM97XX_FMIXER_VOL, 0x0808);
  967. return 0;
  968. }
  969. /* WM9711, WM9712 */
  970. static int wolfson_init11(struct ac97_codec * codec)
  971. {
  972. /* stop pop's during suspend/resume */
  973. codec->codec_write(codec, AC97_WM97XX_TEST,
  974. codec->codec_read(codec, AC97_WM97XX_TEST) & 0xffbf);
  975. /* set out3 volume */
  976. codec->codec_write(codec, AC97_WM9711_OUT3VOL, 0x0808);
  977. return 0;
  978. }
  979. /* WM9713 */
  980. static int wolfson_init13(struct ac97_codec * codec)
  981. {
  982. codec->codec_write(codec, AC97_RECORD_GAIN, 0x00a0);
  983. codec->codec_write(codec, AC97_POWER_CONTROL, 0x0000);
  984. codec->codec_write(codec, AC97_EXTENDED_MODEM_ID, 0xDA00);
  985. codec->codec_write(codec, AC97_EXTEND_MODEM_STAT, 0x3810);
  986. codec->codec_write(codec, AC97_PHONE_VOL, 0x0808);
  987. codec->codec_write(codec, AC97_PCBEEP_VOL, 0x0808);
  988. return 0;
  989. }
  990. static int tritech_init(struct ac97_codec * codec)
  991. {
  992. codec->codec_write(codec, 0x26, 0x0300);
  993. codec->codec_write(codec, 0x26, 0x0000);
  994. codec->codec_write(codec, AC97_SURROUND_MASTER, 0x0000);
  995. codec->codec_write(codec, AC97_RESERVED_3A, 0x0000);
  996. return 0;
  997. }
  998. /* copied from drivers/sound/maestro.c */
  999. static int tritech_maestro_init(struct ac97_codec * codec)
  1000. {
  1001. /* no idea what this does */
  1002. codec->codec_write(codec, 0x2A, 0x0001);
  1003. codec->codec_write(codec, 0x2C, 0x0000);
  1004. codec->codec_write(codec, 0x2C, 0XFFFF);
  1005. return 0;
  1006. }
  1007. /*
  1008. * Presario700 workaround
  1009. * for Jack Sense/SPDIF Register mis-setting causing
  1010. * no audible output
  1011. * by Santiago Nullo 04/05/2002
  1012. */
  1013. #define AC97_AD1886_JACK_SENSE 0x72
  1014. static int ad1886_init(struct ac97_codec * codec)
  1015. {
  1016. /* from AD1886 Specs */
  1017. codec->codec_write(codec, AC97_AD1886_JACK_SENSE, 0x0010);
  1018. return 0;
  1019. }
  1020. /*
  1021. * This is basically standard AC97. It should work as a default for
  1022. * almost all modern codecs. Note that some cards wire EAPD *backwards*
  1023. * That side of it is up to the card driver not us to cope with.
  1024. *
  1025. */
  1026. static int eapd_control(struct ac97_codec * codec, int on)
  1027. {
  1028. if(on)
  1029. codec->codec_write(codec, AC97_POWER_CONTROL,
  1030. codec->codec_read(codec, AC97_POWER_CONTROL)|0x8000);
  1031. else
  1032. codec->codec_write(codec, AC97_POWER_CONTROL,
  1033. codec->codec_read(codec, AC97_POWER_CONTROL)&~0x8000);
  1034. return 0;
  1035. }
  1036. static int generic_digital_control(struct ac97_codec *codec, int slots, int rate, int mode)
  1037. {
  1038. u16 reg;
  1039. reg = codec->codec_read(codec, AC97_SPDIF_CONTROL);
  1040. switch(rate)
  1041. {
  1042. /* Off by default */
  1043. default:
  1044. case 0:
  1045. reg = codec->codec_read(codec, AC97_EXTENDED_STATUS);
  1046. codec->codec_write(codec, AC97_EXTENDED_STATUS, (reg & ~AC97_EA_SPDIF));
  1047. if(rate == 0)
  1048. return 0;
  1049. return -EINVAL;
  1050. case 1:
  1051. reg = (reg & AC97_SC_SPSR_MASK) | AC97_SC_SPSR_48K;
  1052. break;
  1053. case 2:
  1054. reg = (reg & AC97_SC_SPSR_MASK) | AC97_SC_SPSR_44K;
  1055. break;
  1056. case 3:
  1057. reg = (reg & AC97_SC_SPSR_MASK) | AC97_SC_SPSR_32K;
  1058. break;
  1059. }
  1060. reg &= ~AC97_SC_CC_MASK;
  1061. reg |= (mode & AUDIO_CCMASK) << 6;
  1062. if(mode & AUDIO_DIGITAL)
  1063. reg |= 2;
  1064. if(mode & AUDIO_PRO)
  1065. reg |= 1;
  1066. if(mode & AUDIO_DRS)
  1067. reg |= 0x4000;
  1068. codec->codec_write(codec, AC97_SPDIF_CONTROL, reg);
  1069. reg = codec->codec_read(codec, AC97_EXTENDED_STATUS);
  1070. reg &= (AC97_EA_SLOT_MASK);
  1071. reg |= AC97_EA_VRA | AC97_EA_SPDIF | slots;
  1072. codec->codec_write(codec, AC97_EXTENDED_STATUS, reg);
  1073. reg = codec->codec_read(codec, AC97_EXTENDED_STATUS);
  1074. if(!(reg & 0x0400))
  1075. {
  1076. codec->codec_write(codec, AC97_EXTENDED_STATUS, reg & ~ AC97_EA_SPDIF);
  1077. return -EINVAL;
  1078. }
  1079. return 0;
  1080. }
  1081. /*
  1082. * Crystal digital audio control (CS4299)
  1083. */
  1084. static int crystal_digital_control(struct ac97_codec *codec, int slots, int rate, int mode)
  1085. {
  1086. u16 cv;
  1087. if(mode & AUDIO_DIGITAL)
  1088. return -EINVAL;
  1089. switch(rate)
  1090. {
  1091. case 0: cv = 0x0; break; /* SPEN off */
  1092. case 48000: cv = 0x8004; break; /* 48KHz digital */
  1093. case 44100: cv = 0x8104; break; /* 44.1KHz digital */
  1094. case 32768: /* 32Khz */
  1095. default:
  1096. return -EINVAL;
  1097. }
  1098. codec->codec_write(codec, 0x68, cv);
  1099. return 0;
  1100. }
  1101. /*
  1102. * CMedia digital audio control
  1103. * Needs more work.
  1104. */
  1105. static int cmedia_digital_control(struct ac97_codec *codec, int slots, int rate, int mode)
  1106. {
  1107. u16 cv;
  1108. if(mode & AUDIO_DIGITAL)
  1109. return -EINVAL;
  1110. switch(rate)
  1111. {
  1112. case 0: cv = 0x0001; break; /* SPEN off */
  1113. case 48000: cv = 0x0009; break; /* 48KHz digital */
  1114. default:
  1115. return -EINVAL;
  1116. }
  1117. codec->codec_write(codec, 0x2A, 0x05c4);
  1118. codec->codec_write(codec, 0x6C, cv);
  1119. /* Switch on mix to surround */
  1120. cv = codec->codec_read(codec, 0x64);
  1121. cv &= ~0x0200;
  1122. if(mode)
  1123. cv |= 0x0200;
  1124. codec->codec_write(codec, 0x64, cv);
  1125. return 0;
  1126. }
  1127. /* copied from drivers/sound/maestro.c */
  1128. #if 0 /* there has been 1 person on the planet with a pt101 that we
  1129. know of. If they care, they can put this back in :) */
  1130. static int pt101_init(struct ac97_codec * codec)
  1131. {
  1132. printk(KERN_INFO "ac97_codec: PT101 Codec detected, initializing but _not_ installing mixer device.\n");
  1133. /* who knows.. */
  1134. codec->codec_write(codec, 0x2A, 0x0001);
  1135. codec->codec_write(codec, 0x2C, 0x0000);
  1136. codec->codec_write(codec, 0x2C, 0xFFFF);
  1137. codec->codec_write(codec, 0x10, 0x9F1F);
  1138. codec->codec_write(codec, 0x12, 0x0808);
  1139. codec->codec_write(codec, 0x14, 0x9F1F);
  1140. codec->codec_write(codec, 0x16, 0x9F1F);
  1141. codec->codec_write(codec, 0x18, 0x0404);
  1142. codec->codec_write(codec, 0x1A, 0x0000);
  1143. codec->codec_write(codec, 0x1C, 0x0000);
  1144. codec->codec_write(codec, 0x02, 0x0404);
  1145. codec->codec_write(codec, 0x04, 0x0808);
  1146. codec->codec_write(codec, 0x0C, 0x801F);
  1147. codec->codec_write(codec, 0x0E, 0x801F);
  1148. return 0;
  1149. }
  1150. #endif
  1151. EXPORT_SYMBOL(ac97_read_proc);
  1152. EXPORT_SYMBOL(ac97_probe_codec);
  1153. /*
  1154. * AC97 library support routines
  1155. */
  1156. /**
  1157. * ac97_set_dac_rate - set codec rate adaption
  1158. * @codec: ac97 code
  1159. * @rate: rate in hertz
  1160. *
  1161. * Set the DAC rate. Assumes the codec supports VRA. The caller is
  1162. * expected to have checked this little detail.
  1163. */
  1164. unsigned int ac97_set_dac_rate(struct ac97_codec *codec, unsigned int rate)
  1165. {
  1166. unsigned int new_rate = rate;
  1167. u32 dacp;
  1168. u32 mast_vol, phone_vol, mono_vol, pcm_vol;
  1169. u32 mute_vol = 0x8000; /* The mute volume? */
  1170. if(rate != codec->codec_read(codec, AC97_PCM_FRONT_DAC_RATE))
  1171. {
  1172. /* Mute several registers */
  1173. mast_vol = codec->codec_read(codec, AC97_MASTER_VOL_STEREO);
  1174. mono_vol = codec->codec_read(codec, AC97_MASTER_VOL_MONO);
  1175. phone_vol = codec->codec_read(codec, AC97_HEADPHONE_VOL);
  1176. pcm_vol = codec->codec_read(codec, AC97_PCMOUT_VOL);
  1177. codec->codec_write(codec, AC97_MASTER_VOL_STEREO, mute_vol);
  1178. codec->codec_write(codec, AC97_MASTER_VOL_MONO, mute_vol);
  1179. codec->codec_write(codec, AC97_HEADPHONE_VOL, mute_vol);
  1180. codec->codec_write(codec, AC97_PCMOUT_VOL, mute_vol);
  1181. /* Power down the DAC */
  1182. dacp=codec->codec_read(codec, AC97_POWER_CONTROL);
  1183. codec->codec_write(codec, AC97_POWER_CONTROL, dacp|0x0200);
  1184. /* Load the rate and read the effective rate */
  1185. codec->codec_write(codec, AC97_PCM_FRONT_DAC_RATE, rate);
  1186. new_rate=codec->codec_read(codec, AC97_PCM_FRONT_DAC_RATE);
  1187. /* Power it back up */
  1188. codec->codec_write(codec, AC97_POWER_CONTROL, dacp);
  1189. /* Restore volumes */
  1190. codec->codec_write(codec, AC97_MASTER_VOL_STEREO, mast_vol);
  1191. codec->codec_write(codec, AC97_MASTER_VOL_MONO, mono_vol);
  1192. codec->codec_write(codec, AC97_HEADPHONE_VOL, phone_vol);
  1193. codec->codec_write(codec, AC97_PCMOUT_VOL, pcm_vol);
  1194. }
  1195. return new_rate;
  1196. }
  1197. EXPORT_SYMBOL(ac97_set_dac_rate);
  1198. /**
  1199. * ac97_set_adc_rate - set codec rate adaption
  1200. * @codec: ac97 code
  1201. * @rate: rate in hertz
  1202. *
  1203. * Set the ADC rate. Assumes the codec supports VRA. The caller is
  1204. * expected to have checked this little detail.
  1205. */
  1206. unsigned int ac97_set_adc_rate(struct ac97_codec *codec, unsigned int rate)
  1207. {
  1208. unsigned int new_rate = rate;
  1209. u32 dacp;
  1210. if(rate != codec->codec_read(codec, AC97_PCM_LR_ADC_RATE))
  1211. {
  1212. /* Power down the ADC */
  1213. dacp=codec->codec_read(codec, AC97_POWER_CONTROL);
  1214. codec->codec_write(codec, AC97_POWER_CONTROL, dacp|0x0100);
  1215. /* Load the rate and read the effective rate */
  1216. codec->codec_write(codec, AC97_PCM_LR_ADC_RATE, rate);
  1217. new_rate=codec->codec_read(codec, AC97_PCM_LR_ADC_RATE);
  1218. /* Power it back up */
  1219. codec->codec_write(codec, AC97_POWER_CONTROL, dacp);
  1220. }
  1221. return new_rate;
  1222. }
  1223. EXPORT_SYMBOL(ac97_set_adc_rate);
  1224. int ac97_save_state(struct ac97_codec *codec)
  1225. {
  1226. return 0;
  1227. }
  1228. EXPORT_SYMBOL(ac97_save_state);
  1229. int ac97_restore_state(struct ac97_codec *codec)
  1230. {
  1231. int i;
  1232. unsigned int left, right, val;
  1233. for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
  1234. if (!supported_mixer(codec, i))
  1235. continue;
  1236. val = codec->mixer_state[i];
  1237. right = val >> 8;
  1238. left = val & 0xff;
  1239. codec->write_mixer(codec, i, left, right);
  1240. }
  1241. return 0;
  1242. }
  1243. EXPORT_SYMBOL(ac97_restore_state);
  1244. /**
  1245. * ac97_register_driver - register a codec helper
  1246. * @driver: Driver handler
  1247. *
  1248. * Register a handler for codecs matching the codec id. The handler
  1249. * attach function is called for all present codecs and will be
  1250. * called when new codecs are discovered.
  1251. */
  1252. int ac97_register_driver(struct ac97_driver *driver)
  1253. {
  1254. struct list_head *l;
  1255. struct ac97_codec *c;
  1256. mutex_lock(&codec_mutex);
  1257. INIT_LIST_HEAD(&driver->list);
  1258. list_add(&driver->list, &codec_drivers);
  1259. list_for_each(l, &codecs)
  1260. {
  1261. c = list_entry(l, struct ac97_codec, list);
  1262. if(c->driver != NULL || ((c->model ^ driver->codec_id) & driver->codec_mask))
  1263. continue;
  1264. if(driver->probe(c, driver))
  1265. continue;
  1266. c->driver = driver;
  1267. }
  1268. mutex_unlock(&codec_mutex);
  1269. return 0;
  1270. }
  1271. EXPORT_SYMBOL_GPL(ac97_register_driver);
  1272. /**
  1273. * ac97_unregister_driver - unregister a codec helper
  1274. * @driver: Driver handler
  1275. *
  1276. * Unregister a handler for codecs matching the codec id. The handler
  1277. * remove function is called for all matching codecs.
  1278. */
  1279. void ac97_unregister_driver(struct ac97_driver *driver)
  1280. {
  1281. struct list_head *l;
  1282. struct ac97_codec *c;
  1283. mutex_lock(&codec_mutex);
  1284. list_del_init(&driver->list);
  1285. list_for_each(l, &codecs)
  1286. {
  1287. c = list_entry(l, struct ac97_codec, list);
  1288. if (c->driver == driver) {
  1289. driver->remove(c, driver);
  1290. c->driver = NULL;
  1291. }
  1292. }
  1293. mutex_unlock(&codec_mutex);
  1294. }
  1295. EXPORT_SYMBOL_GPL(ac97_unregister_driver);
  1296. static int swap_headphone(int remove_master)
  1297. {
  1298. struct list_head *l;
  1299. struct ac97_codec *c;
  1300. if (remove_master) {
  1301. mutex_lock(&codec_mutex);
  1302. list_for_each(l, &codecs)
  1303. {
  1304. c = list_entry(l, struct ac97_codec, list);
  1305. if (supported_mixer(c, SOUND_MIXER_PHONEOUT))
  1306. c->supported_mixers &= ~SOUND_MASK_PHONEOUT;
  1307. }
  1308. mutex_unlock(&codec_mutex);
  1309. } else
  1310. ac97_hw[SOUND_MIXER_PHONEOUT].offset = AC97_MASTER_VOL_STEREO;
  1311. /* Scale values already match */
  1312. ac97_hw[SOUND_MIXER_VOLUME].offset = AC97_MASTER_VOL_MONO;
  1313. return 0;
  1314. }
  1315. static int apply_quirk(int quirk)
  1316. {
  1317. switch (quirk) {
  1318. case AC97_TUNE_NONE:
  1319. return 0;
  1320. case AC97_TUNE_HP_ONLY:
  1321. return swap_headphone(1);
  1322. case AC97_TUNE_SWAP_HP:
  1323. return swap_headphone(0);
  1324. case AC97_TUNE_SWAP_SURROUND:
  1325. return -ENOSYS; /* not yet implemented */
  1326. case AC97_TUNE_AD_SHARING:
  1327. return -ENOSYS; /* not yet implemented */
  1328. case AC97_TUNE_ALC_JACK:
  1329. return -ENOSYS; /* not yet implemented */
  1330. }
  1331. return -EINVAL;
  1332. }
  1333. /**
  1334. * ac97_tune_hardware - tune up the hardware
  1335. * @pdev: pci_dev pointer
  1336. * @quirk: quirk list
  1337. * @override: explicit quirk value (overrides if not AC97_TUNE_DEFAULT)
  1338. *
  1339. * Do some workaround for each pci device, such as renaming of the
  1340. * headphone (true line-out) control as "Master".
  1341. * The quirk-list must be terminated with a zero-filled entry.
  1342. *
  1343. * Returns zero if successful, or a negative error code on failure.
  1344. */
  1345. int ac97_tune_hardware(struct pci_dev *pdev, struct ac97_quirk *quirk, int override)
  1346. {
  1347. int result;
  1348. if (!quirk)
  1349. return -EINVAL;
  1350. if (override != AC97_TUNE_DEFAULT) {
  1351. result = apply_quirk(override);
  1352. if (result < 0)
  1353. printk(KERN_ERR "applying quirk type %d failed (%d)\n", override, result);
  1354. return result;
  1355. }
  1356. for (; quirk->vendor; quirk++) {
  1357. if (quirk->vendor != pdev->subsystem_vendor)
  1358. continue;
  1359. if ((! quirk->mask && quirk->device == pdev->subsystem_device) ||
  1360. quirk->device == (quirk->mask & pdev->subsystem_device)) {
  1361. #ifdef DEBUG
  1362. printk("ac97 quirk for %s (%04x:%04x)\n", quirk->name, ac97->subsystem_vendor, pdev->subsystem_device);
  1363. #endif
  1364. result = apply_quirk(quirk->type);
  1365. if (result < 0)
  1366. printk(KERN_ERR "applying quirk type %d for %s failed (%d)\n", quirk->type, quirk->name, result);
  1367. return result;
  1368. }
  1369. }
  1370. return 0;
  1371. }
  1372. EXPORT_SYMBOL_GPL(ac97_tune_hardware);
  1373. MODULE_LICENSE("GPL");