ac97_codec.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  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 <lrg@slimlogic.co.uk>
  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. /* this table has default mixer values for all OSS mixers. */
  184. static struct mixer_defaults {
  185. int mixer;
  186. unsigned int value;
  187. } mixer_defaults[SOUND_MIXER_NRDEVICES] = {
  188. /* all values 0 -> 100 in bytes */
  189. {SOUND_MIXER_VOLUME, 0x4343},
  190. {SOUND_MIXER_BASS, 0x4343},
  191. {SOUND_MIXER_TREBLE, 0x4343},
  192. {SOUND_MIXER_PCM, 0x4343},
  193. {SOUND_MIXER_SPEAKER, 0x4343},
  194. {SOUND_MIXER_LINE, 0x4343},
  195. {SOUND_MIXER_MIC, 0x0000},
  196. {SOUND_MIXER_CD, 0x4343},
  197. {SOUND_MIXER_ALTPCM, 0x4343},
  198. {SOUND_MIXER_IGAIN, 0x4343},
  199. {SOUND_MIXER_LINE1, 0x4343},
  200. {SOUND_MIXER_PHONEIN, 0x4343},
  201. {SOUND_MIXER_PHONEOUT, 0x4343},
  202. {SOUND_MIXER_VIDEO, 0x4343},
  203. {-1,0}
  204. };
  205. /* table to scale scale from OSS mixer value to AC97 mixer register value */
  206. static struct ac97_mixer_hw {
  207. unsigned char offset;
  208. int scale;
  209. } ac97_hw[SOUND_MIXER_NRDEVICES]= {
  210. [SOUND_MIXER_VOLUME] = {AC97_MASTER_VOL_STEREO,64},
  211. [SOUND_MIXER_BASS] = {AC97_MASTER_TONE, 16},
  212. [SOUND_MIXER_TREBLE] = {AC97_MASTER_TONE, 16},
  213. [SOUND_MIXER_PCM] = {AC97_PCMOUT_VOL, 32},
  214. [SOUND_MIXER_SPEAKER] = {AC97_PCBEEP_VOL, 16},
  215. [SOUND_MIXER_LINE] = {AC97_LINEIN_VOL, 32},
  216. [SOUND_MIXER_MIC] = {AC97_MIC_VOL, 32},
  217. [SOUND_MIXER_CD] = {AC97_CD_VOL, 32},
  218. [SOUND_MIXER_ALTPCM] = {AC97_HEADPHONE_VOL, 64},
  219. [SOUND_MIXER_IGAIN] = {AC97_RECORD_GAIN, 16},
  220. [SOUND_MIXER_LINE1] = {AC97_AUX_VOL, 32},
  221. [SOUND_MIXER_PHONEIN] = {AC97_PHONE_VOL, 32},
  222. [SOUND_MIXER_PHONEOUT] = {AC97_MASTER_VOL_MONO, 64},
  223. [SOUND_MIXER_VIDEO] = {AC97_VIDEO_VOL, 32},
  224. };
  225. /* the following tables allow us to go from OSS <-> ac97 quickly. */
  226. enum ac97_recsettings {
  227. AC97_REC_MIC=0,
  228. AC97_REC_CD,
  229. AC97_REC_VIDEO,
  230. AC97_REC_AUX,
  231. AC97_REC_LINE,
  232. AC97_REC_STEREO, /* combination of all enabled outputs.. */
  233. AC97_REC_MONO, /*.. or the mono equivalent */
  234. AC97_REC_PHONE
  235. };
  236. static const unsigned int ac97_rm2oss[] = {
  237. [AC97_REC_MIC] = SOUND_MIXER_MIC,
  238. [AC97_REC_CD] = SOUND_MIXER_CD,
  239. [AC97_REC_VIDEO] = SOUND_MIXER_VIDEO,
  240. [AC97_REC_AUX] = SOUND_MIXER_LINE1,
  241. [AC97_REC_LINE] = SOUND_MIXER_LINE,
  242. [AC97_REC_STEREO]= SOUND_MIXER_IGAIN,
  243. [AC97_REC_PHONE] = SOUND_MIXER_PHONEIN
  244. };
  245. /* indexed by bit position */
  246. static const unsigned int ac97_oss_rm[] = {
  247. [SOUND_MIXER_MIC] = AC97_REC_MIC,
  248. [SOUND_MIXER_CD] = AC97_REC_CD,
  249. [SOUND_MIXER_VIDEO] = AC97_REC_VIDEO,
  250. [SOUND_MIXER_LINE1] = AC97_REC_AUX,
  251. [SOUND_MIXER_LINE] = AC97_REC_LINE,
  252. [SOUND_MIXER_IGAIN] = AC97_REC_STEREO,
  253. [SOUND_MIXER_PHONEIN] = AC97_REC_PHONE
  254. };
  255. static LIST_HEAD(codecs);
  256. static LIST_HEAD(codec_drivers);
  257. static DEFINE_MUTEX(codec_mutex);
  258. /* reads the given OSS mixer from the ac97 the caller must have insured that the ac97 knows
  259. about that given mixer, and should be holding a spinlock for the card */
  260. static int ac97_read_mixer(struct ac97_codec *codec, int oss_channel)
  261. {
  262. u16 val;
  263. int ret = 0;
  264. int scale;
  265. struct ac97_mixer_hw *mh = &ac97_hw[oss_channel];
  266. val = codec->codec_read(codec , mh->offset);
  267. if (val & AC97_MUTE) {
  268. ret = 0;
  269. } else if (AC97_STEREO_MASK & (1 << oss_channel)) {
  270. /* nice stereo mixers .. */
  271. int left,right;
  272. left = (val >> 8) & 0x7f;
  273. right = val & 0x7f;
  274. if (oss_channel == SOUND_MIXER_IGAIN) {
  275. right = (right * 100) / mh->scale;
  276. left = (left * 100) / mh->scale;
  277. } else {
  278. /* these may have 5 or 6 bit resolution */
  279. if(oss_channel == SOUND_MIXER_VOLUME || oss_channel == SOUND_MIXER_ALTPCM)
  280. scale = (1 << codec->bit_resolution);
  281. else
  282. scale = mh->scale;
  283. right = 100 - ((right * 100) / scale);
  284. left = 100 - ((left * 100) / scale);
  285. }
  286. ret = left | (right << 8);
  287. } else if (oss_channel == SOUND_MIXER_SPEAKER) {
  288. ret = 100 - ((((val & 0x1e)>>1) * 100) / mh->scale);
  289. } else if (oss_channel == SOUND_MIXER_PHONEIN) {
  290. ret = 100 - (((val & 0x1f) * 100) / mh->scale);
  291. } else if (oss_channel == SOUND_MIXER_PHONEOUT) {
  292. scale = (1 << codec->bit_resolution);
  293. ret = 100 - (((val & 0x1f) * 100) / scale);
  294. } else if (oss_channel == SOUND_MIXER_MIC) {
  295. ret = 100 - (((val & 0x1f) * 100) / mh->scale);
  296. /* the low bit is optional in the tone sliders and masking
  297. it lets us avoid the 0xf 'bypass'.. */
  298. } else if (oss_channel == SOUND_MIXER_BASS) {
  299. ret = 100 - ((((val >> 8) & 0xe) * 100) / mh->scale);
  300. } else if (oss_channel == SOUND_MIXER_TREBLE) {
  301. ret = 100 - (((val & 0xe) * 100) / mh->scale);
  302. }
  303. #ifdef DEBUG
  304. printk("ac97_codec: read OSS mixer %2d (%s ac97 register 0x%02x), "
  305. "0x%04x -> 0x%04x\n",
  306. oss_channel, codec->id ? "Secondary" : "Primary",
  307. mh->offset, val, ret);
  308. #endif
  309. return ret;
  310. }
  311. /* write the OSS encoded volume to the given OSS encoded mixer, again caller's job to
  312. make sure all is well in arg land, call with spinlock held */
  313. static void ac97_write_mixer(struct ac97_codec *codec, int oss_channel,
  314. unsigned int left, unsigned int right)
  315. {
  316. u16 val = 0;
  317. int scale;
  318. struct ac97_mixer_hw *mh = &ac97_hw[oss_channel];
  319. #ifdef DEBUG
  320. printk("ac97_codec: wrote OSS mixer %2d (%s ac97 register 0x%02x), "
  321. "left vol:%2d, right vol:%2d:",
  322. oss_channel, codec->id ? "Secondary" : "Primary",
  323. mh->offset, left, right);
  324. #endif
  325. if (AC97_STEREO_MASK & (1 << oss_channel)) {
  326. /* stereo mixers */
  327. if (left == 0 && right == 0) {
  328. val = AC97_MUTE;
  329. } else {
  330. if (oss_channel == SOUND_MIXER_IGAIN) {
  331. right = (right * mh->scale) / 100;
  332. left = (left * mh->scale) / 100;
  333. if (right >= mh->scale)
  334. right = mh->scale-1;
  335. if (left >= mh->scale)
  336. left = mh->scale-1;
  337. } else {
  338. /* these may have 5 or 6 bit resolution */
  339. if (oss_channel == SOUND_MIXER_VOLUME ||
  340. oss_channel == SOUND_MIXER_ALTPCM)
  341. scale = (1 << codec->bit_resolution);
  342. else
  343. scale = mh->scale;
  344. right = ((100 - right) * scale) / 100;
  345. left = ((100 - left) * scale) / 100;
  346. if (right >= scale)
  347. right = scale-1;
  348. if (left >= scale)
  349. left = scale-1;
  350. }
  351. val = (left << 8) | right;
  352. }
  353. } else if (oss_channel == SOUND_MIXER_BASS) {
  354. val = codec->codec_read(codec , mh->offset) & ~0x0f00;
  355. left = ((100 - left) * mh->scale) / 100;
  356. if (left >= mh->scale)
  357. left = mh->scale-1;
  358. val |= (left << 8) & 0x0e00;
  359. } else if (oss_channel == SOUND_MIXER_TREBLE) {
  360. val = codec->codec_read(codec , mh->offset) & ~0x000f;
  361. left = ((100 - left) * mh->scale) / 100;
  362. if (left >= mh->scale)
  363. left = mh->scale-1;
  364. val |= left & 0x000e;
  365. } else if(left == 0) {
  366. val = AC97_MUTE;
  367. } else if (oss_channel == SOUND_MIXER_SPEAKER) {
  368. left = ((100 - left) * mh->scale) / 100;
  369. if (left >= mh->scale)
  370. left = mh->scale-1;
  371. val = left << 1;
  372. } else if (oss_channel == SOUND_MIXER_PHONEIN) {
  373. left = ((100 - left) * mh->scale) / 100;
  374. if (left >= mh->scale)
  375. left = mh->scale-1;
  376. val = left;
  377. } else if (oss_channel == SOUND_MIXER_PHONEOUT) {
  378. scale = (1 << codec->bit_resolution);
  379. left = ((100 - left) * scale) / 100;
  380. if (left >= mh->scale)
  381. left = mh->scale-1;
  382. val = left;
  383. } else if (oss_channel == SOUND_MIXER_MIC) {
  384. val = codec->codec_read(codec , mh->offset) & ~0x801f;
  385. left = ((100 - left) * mh->scale) / 100;
  386. if (left >= mh->scale)
  387. left = mh->scale-1;
  388. val |= left;
  389. /* the low bit is optional in the tone sliders and masking
  390. it lets us avoid the 0xf 'bypass'.. */
  391. }
  392. #ifdef DEBUG
  393. printk(" 0x%04x", val);
  394. #endif
  395. codec->codec_write(codec, mh->offset, val);
  396. #ifdef DEBUG
  397. val = codec->codec_read(codec, mh->offset);
  398. printk(" -> 0x%04x\n", val);
  399. #endif
  400. }
  401. /* a thin wrapper for write_mixer */
  402. static void ac97_set_mixer(struct ac97_codec *codec, unsigned int oss_mixer, unsigned int val )
  403. {
  404. unsigned int left,right;
  405. /* cleanse input a little */
  406. right = ((val >> 8) & 0xff) ;
  407. left = (val & 0xff) ;
  408. if (right > 100) right = 100;
  409. if (left > 100) left = 100;
  410. codec->mixer_state[oss_mixer] = (right << 8) | left;
  411. codec->write_mixer(codec, oss_mixer, left, right);
  412. }
  413. /* read or write the recmask, the ac97 can really have left and right recording
  414. inputs independantly set, but OSS doesn't seem to want us to express that to
  415. the user. the caller guarantees that we have a supported bit set, and they
  416. must be holding the card's spinlock */
  417. static int ac97_recmask_io(struct ac97_codec *codec, int rw, int mask)
  418. {
  419. unsigned int val;
  420. if (rw) {
  421. /* read it from the card */
  422. val = codec->codec_read(codec, AC97_RECORD_SELECT);
  423. #ifdef DEBUG
  424. printk("ac97_codec: ac97 recmask to set to 0x%04x\n", val);
  425. #endif
  426. return (1 << ac97_rm2oss[val & 0x07]);
  427. }
  428. /* else, write the first set in the mask as the
  429. output */
  430. /* clear out current set value first (AC97 supports only 1 input!) */
  431. val = (1 << ac97_rm2oss[codec->codec_read(codec, AC97_RECORD_SELECT) & 0x07]);
  432. if (mask != val)
  433. mask &= ~val;
  434. val = ffs(mask);
  435. val = ac97_oss_rm[val-1];
  436. val |= val << 8; /* set both channels */
  437. #ifdef DEBUG
  438. printk("ac97_codec: setting ac97 recmask to 0x%04x\n", val);
  439. #endif
  440. codec->codec_write(codec, AC97_RECORD_SELECT, val);
  441. return 0;
  442. };
  443. static int ac97_mixer_ioctl(struct ac97_codec *codec, unsigned int cmd, unsigned long arg)
  444. {
  445. int i, val = 0;
  446. if (cmd == SOUND_MIXER_INFO) {
  447. mixer_info info;
  448. memset(&info, 0, sizeof(info));
  449. strlcpy(info.id, codec->name, sizeof(info.id));
  450. strlcpy(info.name, codec->name, sizeof(info.name));
  451. info.modify_counter = codec->modcnt;
  452. if (copy_to_user((void __user *)arg, &info, sizeof(info)))
  453. return -EFAULT;
  454. return 0;
  455. }
  456. if (cmd == SOUND_OLD_MIXER_INFO) {
  457. _old_mixer_info info;
  458. memset(&info, 0, sizeof(info));
  459. strlcpy(info.id, codec->name, sizeof(info.id));
  460. strlcpy(info.name, codec->name, sizeof(info.name));
  461. if (copy_to_user((void __user *)arg, &info, sizeof(info)))
  462. return -EFAULT;
  463. return 0;
  464. }
  465. if (_IOC_TYPE(cmd) != 'M' || _SIOC_SIZE(cmd) != sizeof(int))
  466. return -EINVAL;
  467. if (cmd == OSS_GETVERSION)
  468. return put_user(SOUND_VERSION, (int __user *)arg);
  469. if (_SIOC_DIR(cmd) == _SIOC_READ) {
  470. switch (_IOC_NR(cmd)) {
  471. case SOUND_MIXER_RECSRC: /* give them the current record source */
  472. if (!codec->recmask_io) {
  473. val = 0;
  474. } else {
  475. val = codec->recmask_io(codec, 1, 0);
  476. }
  477. break;
  478. case SOUND_MIXER_DEVMASK: /* give them the supported mixers */
  479. val = codec->supported_mixers;
  480. break;
  481. case SOUND_MIXER_RECMASK: /* Arg contains a bit for each supported recording source */
  482. val = codec->record_sources;
  483. break;
  484. case SOUND_MIXER_STEREODEVS: /* Mixer channels supporting stereo */
  485. val = codec->stereo_mixers;
  486. break;
  487. case SOUND_MIXER_CAPS:
  488. val = SOUND_CAP_EXCL_INPUT;
  489. break;
  490. default: /* read a specific mixer */
  491. i = _IOC_NR(cmd);
  492. if (!supported_mixer(codec, i))
  493. return -EINVAL;
  494. /* do we ever want to touch the hardware? */
  495. /* val = codec->read_mixer(codec, i); */
  496. val = codec->mixer_state[i];
  497. break;
  498. }
  499. return put_user(val, (int __user *)arg);
  500. }
  501. if (_SIOC_DIR(cmd) == (_SIOC_WRITE|_SIOC_READ)) {
  502. codec->modcnt++;
  503. if (get_user(val, (int __user *)arg))
  504. return -EFAULT;
  505. switch (_IOC_NR(cmd)) {
  506. case SOUND_MIXER_RECSRC: /* Arg contains a bit for each recording source */
  507. if (!codec->recmask_io) return -EINVAL;
  508. if (!val) return 0;
  509. if (!(val &= codec->record_sources)) return -EINVAL;
  510. codec->recmask_io(codec, 0, val);
  511. return 0;
  512. default: /* write a specific mixer */
  513. i = _IOC_NR(cmd);
  514. if (!supported_mixer(codec, i))
  515. return -EINVAL;
  516. ac97_set_mixer(codec, i, val);
  517. return 0;
  518. }
  519. }
  520. return -EINVAL;
  521. }
  522. /**
  523. * codec_id - Turn id1/id2 into a PnP string
  524. * @id1: Vendor ID1
  525. * @id2: Vendor ID2
  526. * @buf: CODEC_ID_BUFSZ byte buffer
  527. *
  528. * Fills buf with a zero terminated PnP ident string for the id1/id2
  529. * pair. For convenience the return is the passed in buffer pointer.
  530. */
  531. static char *codec_id(u16 id1, u16 id2, char *buf)
  532. {
  533. if(id1&0x8080) {
  534. snprintf(buf, CODEC_ID_BUFSZ, "0x%04x:0x%04x", id1, id2);
  535. } else {
  536. buf[0] = (id1 >> 8);
  537. buf[1] = (id1 & 0xFF);
  538. buf[2] = (id2 >> 8);
  539. snprintf(buf+3, CODEC_ID_BUFSZ - 3, "%d", id2&0xFF);
  540. }
  541. return buf;
  542. }
  543. /**
  544. * ac97_check_modem - Check if the Codec is a modem
  545. * @codec: codec to check
  546. *
  547. * Return true if the device is an AC97 1.0 or AC97 2.0 modem
  548. */
  549. static int ac97_check_modem(struct ac97_codec *codec)
  550. {
  551. /* Check for an AC97 1.0 soft modem (ID1) */
  552. if(codec->codec_read(codec, AC97_RESET) & 2)
  553. return 1;
  554. /* Check for an AC97 2.x soft modem */
  555. codec->codec_write(codec, AC97_EXTENDED_MODEM_ID, 0L);
  556. if(codec->codec_read(codec, AC97_EXTENDED_MODEM_ID) & 1)
  557. return 1;
  558. return 0;
  559. }
  560. /**
  561. * ac97_alloc_codec - Allocate an AC97 codec
  562. *
  563. * Returns a new AC97 codec structure. AC97 codecs may become
  564. * refcounted soon so this interface is needed. Returns with
  565. * one reference taken.
  566. */
  567. struct ac97_codec *ac97_alloc_codec(void)
  568. {
  569. struct ac97_codec *codec = kzalloc(sizeof(struct ac97_codec), GFP_KERNEL);
  570. if(!codec)
  571. return NULL;
  572. spin_lock_init(&codec->lock);
  573. INIT_LIST_HEAD(&codec->list);
  574. return codec;
  575. }
  576. EXPORT_SYMBOL(ac97_alloc_codec);
  577. /**
  578. * ac97_release_codec - Release an AC97 codec
  579. * @codec: codec to release
  580. *
  581. * Release an allocated AC97 codec. This will be refcounted in
  582. * time but for the moment is trivial. Calls the unregister
  583. * handler if the codec is now defunct.
  584. */
  585. void ac97_release_codec(struct ac97_codec *codec)
  586. {
  587. /* Remove from the list first, we don't want to be
  588. "rediscovered" */
  589. mutex_lock(&codec_mutex);
  590. list_del(&codec->list);
  591. mutex_unlock(&codec_mutex);
  592. /*
  593. * The driver needs to deal with internal
  594. * locking to avoid accidents here.
  595. */
  596. if(codec->driver)
  597. codec->driver->remove(codec, codec->driver);
  598. kfree(codec);
  599. }
  600. EXPORT_SYMBOL(ac97_release_codec);
  601. /**
  602. * ac97_probe_codec - Initialize and setup AC97-compatible codec
  603. * @codec: (in/out) Kernel info for a single AC97 codec
  604. *
  605. * Reset the AC97 codec, then initialize the mixer and
  606. * the rest of the @codec structure.
  607. *
  608. * The codec_read and codec_write fields of @codec are
  609. * required to be setup and working when this function
  610. * is called. All other fields are set by this function.
  611. *
  612. * codec_wait field of @codec can optionally be provided
  613. * when calling this function. If codec_wait is not %NULL,
  614. * this function will call codec_wait any time it is
  615. * necessary to wait for the audio chip to reach the
  616. * codec-ready state. If codec_wait is %NULL, then
  617. * the default behavior is to call schedule_timeout.
  618. * Currently codec_wait is used to wait for AC97 codec
  619. * reset to complete.
  620. *
  621. * Some codecs will power down when a register reset is
  622. * performed. We now check for such codecs.
  623. *
  624. * Returns 1 (true) on success, or 0 (false) on failure.
  625. */
  626. int ac97_probe_codec(struct ac97_codec *codec)
  627. {
  628. u16 id1, id2;
  629. u16 audio;
  630. int i;
  631. char cidbuf[CODEC_ID_BUFSZ];
  632. u16 f;
  633. struct list_head *l;
  634. struct ac97_driver *d;
  635. /* wait for codec-ready state */
  636. if (codec->codec_wait)
  637. codec->codec_wait(codec);
  638. else
  639. udelay(10);
  640. /* will the codec power down if register reset ? */
  641. id1 = codec->codec_read(codec, AC97_VENDOR_ID1);
  642. id2 = codec->codec_read(codec, AC97_VENDOR_ID2);
  643. codec->name = NULL;
  644. codec->codec_ops = &null_ops;
  645. for (i = 0; i < ARRAY_SIZE(ac97_codec_ids); i++) {
  646. if (ac97_codec_ids[i].id == ((id1 << 16) | id2)) {
  647. codec->type = ac97_codec_ids[i].id;
  648. codec->name = ac97_codec_ids[i].name;
  649. codec->codec_ops = ac97_codec_ids[i].ops;
  650. codec->flags = ac97_codec_ids[i].flags;
  651. break;
  652. }
  653. }
  654. codec->model = (id1 << 16) | id2;
  655. if ((codec->flags & AC97_DEFAULT_POWER_OFF) == 0) {
  656. /* reset codec and wait for the ready bit before we continue */
  657. codec->codec_write(codec, AC97_RESET, 0L);
  658. if (codec->codec_wait)
  659. codec->codec_wait(codec);
  660. else
  661. udelay(10);
  662. }
  663. /* probing AC97 codec, AC97 2.0 says that bit 15 of register 0x00 (reset) should
  664. * be read zero.
  665. *
  666. * FIXME: is the following comment outdated? -jgarzik
  667. * Probing of AC97 in this way is not reliable, it is not even SAFE !!
  668. */
  669. if ((audio = codec->codec_read(codec, AC97_RESET)) & 0x8000) {
  670. printk(KERN_ERR "ac97_codec: %s ac97 codec not present\n",
  671. (codec->id & 0x2) ? (codec->id&1 ? "4th" : "Tertiary")
  672. : (codec->id&1 ? "Secondary": "Primary"));
  673. return 0;
  674. }
  675. /* probe for Modem Codec */
  676. codec->modem = ac97_check_modem(codec);
  677. /* enable SPDIF */
  678. f = codec->codec_read(codec, AC97_EXTENDED_STATUS);
  679. if((codec->codec_ops == &null_ops) && (f & 4))
  680. codec->codec_ops = &default_digital_ops;
  681. /* A device which thinks its a modem but isnt */
  682. if(codec->flags & AC97_DELUDED_MODEM)
  683. codec->modem = 0;
  684. if (codec->name == NULL)
  685. codec->name = "Unknown";
  686. printk(KERN_INFO "ac97_codec: AC97 %s codec, id: %s (%s)\n",
  687. codec->modem ? "Modem" : (audio ? "Audio" : ""),
  688. codec_id(id1, id2, cidbuf), codec->name);
  689. if(!ac97_init_mixer(codec))
  690. return 0;
  691. /*
  692. * Attach last so the caller can override the mixer
  693. * callbacks.
  694. */
  695. mutex_lock(&codec_mutex);
  696. list_add(&codec->list, &codecs);
  697. list_for_each(l, &codec_drivers) {
  698. d = list_entry(l, struct ac97_driver, list);
  699. if ((codec->model ^ d->codec_id) & d->codec_mask)
  700. continue;
  701. if(d->probe(codec, d) == 0)
  702. {
  703. codec->driver = d;
  704. break;
  705. }
  706. }
  707. mutex_unlock(&codec_mutex);
  708. return 1;
  709. }
  710. static int ac97_init_mixer(struct ac97_codec *codec)
  711. {
  712. u16 cap;
  713. int i;
  714. cap = codec->codec_read(codec, AC97_RESET);
  715. /* mixer masks */
  716. codec->supported_mixers = AC97_SUPPORTED_MASK;
  717. codec->stereo_mixers = AC97_STEREO_MASK;
  718. codec->record_sources = AC97_RECORD_MASK;
  719. if (!(cap & 0x04))
  720. codec->supported_mixers &= ~(SOUND_MASK_BASS|SOUND_MASK_TREBLE);
  721. if (!(cap & 0x10))
  722. codec->supported_mixers &= ~SOUND_MASK_ALTPCM;
  723. /* detect bit resolution */
  724. codec->codec_write(codec, AC97_MASTER_VOL_STEREO, 0x2020);
  725. if(codec->codec_read(codec, AC97_MASTER_VOL_STEREO) == 0x2020)
  726. codec->bit_resolution = 6;
  727. else
  728. codec->bit_resolution = 5;
  729. /* generic OSS to AC97 wrapper */
  730. codec->read_mixer = ac97_read_mixer;
  731. codec->write_mixer = ac97_write_mixer;
  732. codec->recmask_io = ac97_recmask_io;
  733. codec->mixer_ioctl = ac97_mixer_ioctl;
  734. /* initialize mixer channel volumes */
  735. for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
  736. struct mixer_defaults *md = &mixer_defaults[i];
  737. if (md->mixer == -1)
  738. break;
  739. if (!supported_mixer(codec, md->mixer))
  740. continue;
  741. ac97_set_mixer(codec, md->mixer, md->value);
  742. }
  743. /* codec specific initialization for 4-6 channel output or secondary codec stuff */
  744. if (codec->codec_ops->init != NULL) {
  745. codec->codec_ops->init(codec);
  746. }
  747. /*
  748. * Volume is MUTE only on this device. We have to initialise
  749. * it but its useless beyond that.
  750. */
  751. if(codec->flags & AC97_NO_PCM_VOLUME)
  752. {
  753. codec->supported_mixers &= ~SOUND_MASK_PCM;
  754. printk(KERN_WARNING "AC97 codec does not have proper volume support.\n");
  755. }
  756. return 1;
  757. }
  758. #define AC97_SIGMATEL_ANALOG 0x6c /* Analog Special */
  759. #define AC97_SIGMATEL_DAC2INVERT 0x6e
  760. #define AC97_SIGMATEL_BIAS1 0x70
  761. #define AC97_SIGMATEL_BIAS2 0x72
  762. #define AC97_SIGMATEL_MULTICHN 0x74 /* Multi-Channel programming */
  763. #define AC97_SIGMATEL_CIC1 0x76
  764. #define AC97_SIGMATEL_CIC2 0x78
  765. static int sigmatel_9708_init(struct ac97_codec * codec)
  766. {
  767. u16 codec72, codec6c;
  768. codec72 = codec->codec_read(codec, AC97_SIGMATEL_BIAS2) & 0x8000;
  769. codec6c = codec->codec_read(codec, AC97_SIGMATEL_ANALOG);
  770. if ((codec72==0) && (codec6c==0)) {
  771. codec->codec_write(codec, AC97_SIGMATEL_CIC1, 0xabba);
  772. codec->codec_write(codec, AC97_SIGMATEL_CIC2, 0x1000);
  773. codec->codec_write(codec, AC97_SIGMATEL_BIAS1, 0xabba);
  774. codec->codec_write(codec, AC97_SIGMATEL_BIAS2, 0x0007);
  775. } else if ((codec72==0x8000) && (codec6c==0)) {
  776. codec->codec_write(codec, AC97_SIGMATEL_CIC1, 0xabba);
  777. codec->codec_write(codec, AC97_SIGMATEL_CIC2, 0x1001);
  778. codec->codec_write(codec, AC97_SIGMATEL_DAC2INVERT, 0x0008);
  779. } else if ((codec72==0x8000) && (codec6c==0x0080)) {
  780. /* nothing */
  781. }
  782. codec->codec_write(codec, AC97_SIGMATEL_MULTICHN, 0x0000);
  783. return 0;
  784. }
  785. static int sigmatel_9721_init(struct ac97_codec * codec)
  786. {
  787. /* Only set up secondary codec */
  788. if (codec->id == 0)
  789. return 0;
  790. codec->codec_write(codec, AC97_SURROUND_MASTER, 0L);
  791. /* initialize SigmaTel STAC9721/23 as secondary codec, decoding AC link
  792. sloc 3,4 = 0x01, slot 7,8 = 0x00, */
  793. codec->codec_write(codec, AC97_SIGMATEL_MULTICHN, 0x00);
  794. /* we don't have the crystal when we are on an AMR card, so use
  795. BIT_CLK as our clock source. Write the magic word ABBA and read
  796. back to enable register 0x78 */
  797. codec->codec_write(codec, AC97_SIGMATEL_CIC1, 0xabba);
  798. codec->codec_read(codec, AC97_SIGMATEL_CIC1);
  799. /* sync all the clocks*/
  800. codec->codec_write(codec, AC97_SIGMATEL_CIC2, 0x3802);
  801. return 0;
  802. }
  803. static int sigmatel_9744_init(struct ac97_codec * codec)
  804. {
  805. // patch for SigmaTel
  806. codec->codec_write(codec, AC97_SIGMATEL_CIC1, 0xabba);
  807. codec->codec_write(codec, AC97_SIGMATEL_CIC2, 0x0000); // is this correct? --jk
  808. codec->codec_write(codec, AC97_SIGMATEL_BIAS1, 0xabba);
  809. codec->codec_write(codec, AC97_SIGMATEL_BIAS2, 0x0002);
  810. codec->codec_write(codec, AC97_SIGMATEL_MULTICHN, 0x0000);
  811. return 0;
  812. }
  813. static int cmedia_init(struct ac97_codec *codec)
  814. {
  815. /* Initialise the CMedia 9739 */
  816. /*
  817. We could set various options here
  818. Register 0x20 bit 0x100 sets mic as center bass
  819. Also do multi_channel_ctrl &=~0x3000 |=0x1000
  820. For now we set up the GPIO and PC beep
  821. */
  822. u16 v;
  823. /* MIC */
  824. codec->codec_write(codec, 0x64, 0x3000);
  825. v = codec->codec_read(codec, 0x64);
  826. v &= ~0x8000;
  827. codec->codec_write(codec, 0x64, v);
  828. codec->codec_write(codec, 0x70, 0x0100);
  829. codec->codec_write(codec, 0x72, 0x0020);
  830. return 0;
  831. }
  832. #define AC97_WM97XX_FMIXER_VOL 0x72
  833. #define AC97_WM97XX_RMIXER_VOL 0x74
  834. #define AC97_WM97XX_TEST 0x5a
  835. #define AC97_WM9704_RPCM_VOL 0x70
  836. #define AC97_WM9711_OUT3VOL 0x16
  837. static int wolfson_init03(struct ac97_codec * codec)
  838. {
  839. /* this is known to work for the ViewSonic ViewPad 1000 */
  840. codec->codec_write(codec, AC97_WM97XX_FMIXER_VOL, 0x0808);
  841. codec->codec_write(codec, AC97_GENERAL_PURPOSE, 0x8000);
  842. return 0;
  843. }
  844. static int wolfson_init04(struct ac97_codec * codec)
  845. {
  846. codec->codec_write(codec, AC97_WM97XX_FMIXER_VOL, 0x0808);
  847. codec->codec_write(codec, AC97_WM97XX_RMIXER_VOL, 0x0808);
  848. // patch for DVD noise
  849. codec->codec_write(codec, AC97_WM97XX_TEST, 0x0200);
  850. // init vol as PCM vol
  851. codec->codec_write(codec, AC97_WM9704_RPCM_VOL,
  852. codec->codec_read(codec, AC97_PCMOUT_VOL));
  853. /* set rear surround volume */
  854. codec->codec_write(codec, AC97_SURROUND_MASTER, 0x0000);
  855. return 0;
  856. }
  857. /* WM9705, WM9710 */
  858. static int wolfson_init05(struct ac97_codec * codec)
  859. {
  860. /* set front mixer volume */
  861. codec->codec_write(codec, AC97_WM97XX_FMIXER_VOL, 0x0808);
  862. return 0;
  863. }
  864. /* WM9711, WM9712 */
  865. static int wolfson_init11(struct ac97_codec * codec)
  866. {
  867. /* stop pop's during suspend/resume */
  868. codec->codec_write(codec, AC97_WM97XX_TEST,
  869. codec->codec_read(codec, AC97_WM97XX_TEST) & 0xffbf);
  870. /* set out3 volume */
  871. codec->codec_write(codec, AC97_WM9711_OUT3VOL, 0x0808);
  872. return 0;
  873. }
  874. /* WM9713 */
  875. static int wolfson_init13(struct ac97_codec * codec)
  876. {
  877. codec->codec_write(codec, AC97_RECORD_GAIN, 0x00a0);
  878. codec->codec_write(codec, AC97_POWER_CONTROL, 0x0000);
  879. codec->codec_write(codec, AC97_EXTENDED_MODEM_ID, 0xDA00);
  880. codec->codec_write(codec, AC97_EXTEND_MODEM_STAT, 0x3810);
  881. codec->codec_write(codec, AC97_PHONE_VOL, 0x0808);
  882. codec->codec_write(codec, AC97_PCBEEP_VOL, 0x0808);
  883. return 0;
  884. }
  885. static int tritech_init(struct ac97_codec * codec)
  886. {
  887. codec->codec_write(codec, 0x26, 0x0300);
  888. codec->codec_write(codec, 0x26, 0x0000);
  889. codec->codec_write(codec, AC97_SURROUND_MASTER, 0x0000);
  890. codec->codec_write(codec, AC97_RESERVED_3A, 0x0000);
  891. return 0;
  892. }
  893. /* copied from drivers/sound/maestro.c */
  894. static int tritech_maestro_init(struct ac97_codec * codec)
  895. {
  896. /* no idea what this does */
  897. codec->codec_write(codec, 0x2A, 0x0001);
  898. codec->codec_write(codec, 0x2C, 0x0000);
  899. codec->codec_write(codec, 0x2C, 0XFFFF);
  900. return 0;
  901. }
  902. /*
  903. * Presario700 workaround
  904. * for Jack Sense/SPDIF Register mis-setting causing
  905. * no audible output
  906. * by Santiago Nullo 04/05/2002
  907. */
  908. #define AC97_AD1886_JACK_SENSE 0x72
  909. static int ad1886_init(struct ac97_codec * codec)
  910. {
  911. /* from AD1886 Specs */
  912. codec->codec_write(codec, AC97_AD1886_JACK_SENSE, 0x0010);
  913. return 0;
  914. }
  915. /*
  916. * This is basically standard AC97. It should work as a default for
  917. * almost all modern codecs. Note that some cards wire EAPD *backwards*
  918. * That side of it is up to the card driver not us to cope with.
  919. *
  920. */
  921. static int eapd_control(struct ac97_codec * codec, int on)
  922. {
  923. if(on)
  924. codec->codec_write(codec, AC97_POWER_CONTROL,
  925. codec->codec_read(codec, AC97_POWER_CONTROL)|0x8000);
  926. else
  927. codec->codec_write(codec, AC97_POWER_CONTROL,
  928. codec->codec_read(codec, AC97_POWER_CONTROL)&~0x8000);
  929. return 0;
  930. }
  931. static int generic_digital_control(struct ac97_codec *codec, int slots, int rate, int mode)
  932. {
  933. u16 reg;
  934. reg = codec->codec_read(codec, AC97_SPDIF_CONTROL);
  935. switch(rate)
  936. {
  937. /* Off by default */
  938. default:
  939. case 0:
  940. reg = codec->codec_read(codec, AC97_EXTENDED_STATUS);
  941. codec->codec_write(codec, AC97_EXTENDED_STATUS, (reg & ~AC97_EA_SPDIF));
  942. if(rate == 0)
  943. return 0;
  944. return -EINVAL;
  945. case 1:
  946. reg = (reg & AC97_SC_SPSR_MASK) | AC97_SC_SPSR_48K;
  947. break;
  948. case 2:
  949. reg = (reg & AC97_SC_SPSR_MASK) | AC97_SC_SPSR_44K;
  950. break;
  951. case 3:
  952. reg = (reg & AC97_SC_SPSR_MASK) | AC97_SC_SPSR_32K;
  953. break;
  954. }
  955. reg &= ~AC97_SC_CC_MASK;
  956. reg |= (mode & AUDIO_CCMASK) << 6;
  957. if(mode & AUDIO_DIGITAL)
  958. reg |= 2;
  959. if(mode & AUDIO_PRO)
  960. reg |= 1;
  961. if(mode & AUDIO_DRS)
  962. reg |= 0x4000;
  963. codec->codec_write(codec, AC97_SPDIF_CONTROL, reg);
  964. reg = codec->codec_read(codec, AC97_EXTENDED_STATUS);
  965. reg &= (AC97_EA_SLOT_MASK);
  966. reg |= AC97_EA_VRA | AC97_EA_SPDIF | slots;
  967. codec->codec_write(codec, AC97_EXTENDED_STATUS, reg);
  968. reg = codec->codec_read(codec, AC97_EXTENDED_STATUS);
  969. if(!(reg & 0x0400))
  970. {
  971. codec->codec_write(codec, AC97_EXTENDED_STATUS, reg & ~ AC97_EA_SPDIF);
  972. return -EINVAL;
  973. }
  974. return 0;
  975. }
  976. /*
  977. * Crystal digital audio control (CS4299)
  978. */
  979. static int crystal_digital_control(struct ac97_codec *codec, int slots, int rate, int mode)
  980. {
  981. u16 cv;
  982. if(mode & AUDIO_DIGITAL)
  983. return -EINVAL;
  984. switch(rate)
  985. {
  986. case 0: cv = 0x0; break; /* SPEN off */
  987. case 48000: cv = 0x8004; break; /* 48KHz digital */
  988. case 44100: cv = 0x8104; break; /* 44.1KHz digital */
  989. case 32768: /* 32Khz */
  990. default:
  991. return -EINVAL;
  992. }
  993. codec->codec_write(codec, 0x68, cv);
  994. return 0;
  995. }
  996. /*
  997. * CMedia digital audio control
  998. * Needs more work.
  999. */
  1000. static int cmedia_digital_control(struct ac97_codec *codec, int slots, int rate, int mode)
  1001. {
  1002. u16 cv;
  1003. if(mode & AUDIO_DIGITAL)
  1004. return -EINVAL;
  1005. switch(rate)
  1006. {
  1007. case 0: cv = 0x0001; break; /* SPEN off */
  1008. case 48000: cv = 0x0009; break; /* 48KHz digital */
  1009. default:
  1010. return -EINVAL;
  1011. }
  1012. codec->codec_write(codec, 0x2A, 0x05c4);
  1013. codec->codec_write(codec, 0x6C, cv);
  1014. /* Switch on mix to surround */
  1015. cv = codec->codec_read(codec, 0x64);
  1016. cv &= ~0x0200;
  1017. if(mode)
  1018. cv |= 0x0200;
  1019. codec->codec_write(codec, 0x64, cv);
  1020. return 0;
  1021. }
  1022. /* copied from drivers/sound/maestro.c */
  1023. #if 0 /* there has been 1 person on the planet with a pt101 that we
  1024. know of. If they care, they can put this back in :) */
  1025. static int pt101_init(struct ac97_codec * codec)
  1026. {
  1027. printk(KERN_INFO "ac97_codec: PT101 Codec detected, initializing but _not_ installing mixer device.\n");
  1028. /* who knows.. */
  1029. codec->codec_write(codec, 0x2A, 0x0001);
  1030. codec->codec_write(codec, 0x2C, 0x0000);
  1031. codec->codec_write(codec, 0x2C, 0xFFFF);
  1032. codec->codec_write(codec, 0x10, 0x9F1F);
  1033. codec->codec_write(codec, 0x12, 0x0808);
  1034. codec->codec_write(codec, 0x14, 0x9F1F);
  1035. codec->codec_write(codec, 0x16, 0x9F1F);
  1036. codec->codec_write(codec, 0x18, 0x0404);
  1037. codec->codec_write(codec, 0x1A, 0x0000);
  1038. codec->codec_write(codec, 0x1C, 0x0000);
  1039. codec->codec_write(codec, 0x02, 0x0404);
  1040. codec->codec_write(codec, 0x04, 0x0808);
  1041. codec->codec_write(codec, 0x0C, 0x801F);
  1042. codec->codec_write(codec, 0x0E, 0x801F);
  1043. return 0;
  1044. }
  1045. #endif
  1046. EXPORT_SYMBOL(ac97_probe_codec);
  1047. MODULE_LICENSE("GPL");