pcxhr_mix22.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. /*
  2. * Driver for Digigram pcxhr compatible soundcards
  3. *
  4. * mixer interface for stereo cards
  5. *
  6. * Copyright (c) 2004 by Digigram <alsa@digigram.com>
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/delay.h>
  23. #include <linux/io.h>
  24. #include <linux/pci.h>
  25. #include <sound/core.h>
  26. #include <sound/control.h>
  27. #include <sound/tlv.h>
  28. #include <sound/asoundef.h>
  29. #include "pcxhr.h"
  30. #include "pcxhr_core.h"
  31. #include "pcxhr_mix22.h"
  32. /* registers used on the DSP and Xilinx (port 2) : HR stereo cards only */
  33. #define PCXHR_DSP_RESET 0x20
  34. #define PCXHR_XLX_CFG 0x24
  35. #define PCXHR_XLX_RUER 0x28
  36. #define PCXHR_XLX_DATA 0x2C
  37. #define PCXHR_XLX_STATUS 0x30
  38. #define PCXHR_XLX_LOFREQ 0x34
  39. #define PCXHR_XLX_HIFREQ 0x38
  40. #define PCXHR_XLX_CSUER 0x3C
  41. #define PCXHR_XLX_SELMIC 0x40
  42. #define PCXHR_DSP 2
  43. /* byte access only ! */
  44. #define PCXHR_INPB(mgr, x) inb((mgr)->port[PCXHR_DSP] + (x))
  45. #define PCXHR_OUTPB(mgr, x, data) outb((data), (mgr)->port[PCXHR_DSP] + (x))
  46. /* values for PCHR_DSP_RESET register */
  47. #define PCXHR_DSP_RESET_DSP 0x01
  48. #define PCXHR_DSP_RESET_MUTE 0x02
  49. #define PCXHR_DSP_RESET_CODEC 0x08
  50. #define PCXHR_DSP_RESET_SMPTE 0x10
  51. #define PCXHR_DSP_RESET_GPO_OFFSET 5
  52. #define PCXHR_DSP_RESET_GPO_MASK 0x60
  53. /* values for PCHR_XLX_CFG register */
  54. #define PCXHR_CFG_SYNCDSP_MASK 0x80
  55. #define PCXHR_CFG_DEPENDENCY_MASK 0x60
  56. #define PCXHR_CFG_INDEPENDANT_SEL 0x00
  57. #define PCXHR_CFG_MASTER_SEL 0x40
  58. #define PCXHR_CFG_SLAVE_SEL 0x20
  59. #define PCXHR_CFG_DATA_UER1_SEL_MASK 0x10 /* 0 (UER0), 1(UER1) */
  60. #define PCXHR_CFG_DATAIN_SEL_MASK 0x08 /* 0 (ana), 1 (UER) */
  61. #define PCXHR_CFG_SRC_MASK 0x04 /* 0 (Bypass), 1 (SRC Actif) */
  62. #define PCXHR_CFG_CLOCK_UER1_SEL_MASK 0x02 /* 0 (UER0), 1(UER1) */
  63. #define PCXHR_CFG_CLOCKIN_SEL_MASK 0x01 /* 0 (internal), 1 (AES/EBU) */
  64. /* values for PCHR_XLX_DATA register */
  65. #define PCXHR_DATA_CODEC 0x80
  66. #define AKM_POWER_CONTROL_CMD 0xA007
  67. #define AKM_RESET_ON_CMD 0xA100
  68. #define AKM_RESET_OFF_CMD 0xA103
  69. #define AKM_CLOCK_INF_55K_CMD 0xA240
  70. #define AKM_CLOCK_SUP_55K_CMD 0xA24D
  71. #define AKM_MUTE_CMD 0xA38D
  72. #define AKM_UNMUTE_CMD 0xA30D
  73. #define AKM_LEFT_LEVEL_CMD 0xA600
  74. #define AKM_RIGHT_LEVEL_CMD 0xA700
  75. /* values for PCHR_XLX_STATUS register - READ */
  76. #define PCXHR_STAT_SRC_LOCK 0x01
  77. #define PCXHR_STAT_LEVEL_IN 0x02
  78. #define PCXHR_STAT_GPI_OFFSET 2
  79. #define PCXHR_STAT_GPI_MASK 0x0C
  80. #define PCXHR_STAT_MIC_CAPS 0x10
  81. /* values for PCHR_XLX_STATUS register - WRITE */
  82. #define PCXHR_STAT_FREQ_SYNC_MASK 0x01
  83. #define PCXHR_STAT_FREQ_UER1_MASK 0x02
  84. #define PCXHR_STAT_FREQ_SAVE_MASK 0x80
  85. /* values for PCHR_XLX_CSUER register */
  86. #define PCXHR_SUER1_BIT_U_READ_MASK 0x80
  87. #define PCXHR_SUER1_BIT_C_READ_MASK 0x40
  88. #define PCXHR_SUER1_DATA_PRESENT_MASK 0x20
  89. #define PCXHR_SUER1_CLOCK_PRESENT_MASK 0x10
  90. #define PCXHR_SUER_BIT_U_READ_MASK 0x08
  91. #define PCXHR_SUER_BIT_C_READ_MASK 0x04
  92. #define PCXHR_SUER_DATA_PRESENT_MASK 0x02
  93. #define PCXHR_SUER_CLOCK_PRESENT_MASK 0x01
  94. #define PCXHR_SUER_BIT_U_WRITE_MASK 0x02
  95. #define PCXHR_SUER_BIT_C_WRITE_MASK 0x01
  96. /* values for PCXHR_XLX_SELMIC register - WRITE */
  97. #define PCXHR_SELMIC_PREAMPLI_OFFSET 2
  98. #define PCXHR_SELMIC_PREAMPLI_MASK 0x0C
  99. #define PCXHR_SELMIC_PHANTOM_ALIM 0x80
  100. static const unsigned char g_hr222_p_level[] = {
  101. 0x00, /* [000] -49.5 dB: AKM[000] = -1.#INF dB (mute) */
  102. 0x01, /* [001] -49.0 dB: AKM[001] = -48.131 dB (diff=0.86920 dB) */
  103. 0x01, /* [002] -48.5 dB: AKM[001] = -48.131 dB (diff=0.36920 dB) */
  104. 0x01, /* [003] -48.0 dB: AKM[001] = -48.131 dB (diff=0.13080 dB) */
  105. 0x01, /* [004] -47.5 dB: AKM[001] = -48.131 dB (diff=0.63080 dB) */
  106. 0x01, /* [005] -46.5 dB: AKM[001] = -48.131 dB (diff=1.63080 dB) */
  107. 0x01, /* [006] -47.0 dB: AKM[001] = -48.131 dB (diff=1.13080 dB) */
  108. 0x01, /* [007] -46.0 dB: AKM[001] = -48.131 dB (diff=2.13080 dB) */
  109. 0x01, /* [008] -45.5 dB: AKM[001] = -48.131 dB (diff=2.63080 dB) */
  110. 0x02, /* [009] -45.0 dB: AKM[002] = -42.110 dB (diff=2.88980 dB) */
  111. 0x02, /* [010] -44.5 dB: AKM[002] = -42.110 dB (diff=2.38980 dB) */
  112. 0x02, /* [011] -44.0 dB: AKM[002] = -42.110 dB (diff=1.88980 dB) */
  113. 0x02, /* [012] -43.5 dB: AKM[002] = -42.110 dB (diff=1.38980 dB) */
  114. 0x02, /* [013] -43.0 dB: AKM[002] = -42.110 dB (diff=0.88980 dB) */
  115. 0x02, /* [014] -42.5 dB: AKM[002] = -42.110 dB (diff=0.38980 dB) */
  116. 0x02, /* [015] -42.0 dB: AKM[002] = -42.110 dB (diff=0.11020 dB) */
  117. 0x02, /* [016] -41.5 dB: AKM[002] = -42.110 dB (diff=0.61020 dB) */
  118. 0x02, /* [017] -41.0 dB: AKM[002] = -42.110 dB (diff=1.11020 dB) */
  119. 0x02, /* [018] -40.5 dB: AKM[002] = -42.110 dB (diff=1.61020 dB) */
  120. 0x03, /* [019] -40.0 dB: AKM[003] = -38.588 dB (diff=1.41162 dB) */
  121. 0x03, /* [020] -39.5 dB: AKM[003] = -38.588 dB (diff=0.91162 dB) */
  122. 0x03, /* [021] -39.0 dB: AKM[003] = -38.588 dB (diff=0.41162 dB) */
  123. 0x03, /* [022] -38.5 dB: AKM[003] = -38.588 dB (diff=0.08838 dB) */
  124. 0x03, /* [023] -38.0 dB: AKM[003] = -38.588 dB (diff=0.58838 dB) */
  125. 0x03, /* [024] -37.5 dB: AKM[003] = -38.588 dB (diff=1.08838 dB) */
  126. 0x04, /* [025] -37.0 dB: AKM[004] = -36.090 dB (diff=0.91040 dB) */
  127. 0x04, /* [026] -36.5 dB: AKM[004] = -36.090 dB (diff=0.41040 dB) */
  128. 0x04, /* [027] -36.0 dB: AKM[004] = -36.090 dB (diff=0.08960 dB) */
  129. 0x04, /* [028] -35.5 dB: AKM[004] = -36.090 dB (diff=0.58960 dB) */
  130. 0x05, /* [029] -35.0 dB: AKM[005] = -34.151 dB (diff=0.84860 dB) */
  131. 0x05, /* [030] -34.5 dB: AKM[005] = -34.151 dB (diff=0.34860 dB) */
  132. 0x05, /* [031] -34.0 dB: AKM[005] = -34.151 dB (diff=0.15140 dB) */
  133. 0x05, /* [032] -33.5 dB: AKM[005] = -34.151 dB (diff=0.65140 dB) */
  134. 0x06, /* [033] -33.0 dB: AKM[006] = -32.568 dB (diff=0.43222 dB) */
  135. 0x06, /* [034] -32.5 dB: AKM[006] = -32.568 dB (diff=0.06778 dB) */
  136. 0x06, /* [035] -32.0 dB: AKM[006] = -32.568 dB (diff=0.56778 dB) */
  137. 0x07, /* [036] -31.5 dB: AKM[007] = -31.229 dB (diff=0.27116 dB) */
  138. 0x07, /* [037] -31.0 dB: AKM[007] = -31.229 dB (diff=0.22884 dB) */
  139. 0x08, /* [038] -30.5 dB: AKM[008] = -30.069 dB (diff=0.43100 dB) */
  140. 0x08, /* [039] -30.0 dB: AKM[008] = -30.069 dB (diff=0.06900 dB) */
  141. 0x09, /* [040] -29.5 dB: AKM[009] = -29.046 dB (diff=0.45405 dB) */
  142. 0x09, /* [041] -29.0 dB: AKM[009] = -29.046 dB (diff=0.04595 dB) */
  143. 0x0a, /* [042] -28.5 dB: AKM[010] = -28.131 dB (diff=0.36920 dB) */
  144. 0x0a, /* [043] -28.0 dB: AKM[010] = -28.131 dB (diff=0.13080 dB) */
  145. 0x0b, /* [044] -27.5 dB: AKM[011] = -27.303 dB (diff=0.19705 dB) */
  146. 0x0b, /* [045] -27.0 dB: AKM[011] = -27.303 dB (diff=0.30295 dB) */
  147. 0x0c, /* [046] -26.5 dB: AKM[012] = -26.547 dB (diff=0.04718 dB) */
  148. 0x0d, /* [047] -26.0 dB: AKM[013] = -25.852 dB (diff=0.14806 dB) */
  149. 0x0e, /* [048] -25.5 dB: AKM[014] = -25.208 dB (diff=0.29176 dB) */
  150. 0x0e, /* [049] -25.0 dB: AKM[014] = -25.208 dB (diff=0.20824 dB) */
  151. 0x0f, /* [050] -24.5 dB: AKM[015] = -24.609 dB (diff=0.10898 dB) */
  152. 0x10, /* [051] -24.0 dB: AKM[016] = -24.048 dB (diff=0.04840 dB) */
  153. 0x11, /* [052] -23.5 dB: AKM[017] = -23.522 dB (diff=0.02183 dB) */
  154. 0x12, /* [053] -23.0 dB: AKM[018] = -23.025 dB (diff=0.02535 dB) */
  155. 0x13, /* [054] -22.5 dB: AKM[019] = -22.556 dB (diff=0.05573 dB) */
  156. 0x14, /* [055] -22.0 dB: AKM[020] = -22.110 dB (diff=0.11020 dB) */
  157. 0x15, /* [056] -21.5 dB: AKM[021] = -21.686 dB (diff=0.18642 dB) */
  158. 0x17, /* [057] -21.0 dB: AKM[023] = -20.896 dB (diff=0.10375 dB) */
  159. 0x18, /* [058] -20.5 dB: AKM[024] = -20.527 dB (diff=0.02658 dB) */
  160. 0x1a, /* [059] -20.0 dB: AKM[026] = -19.831 dB (diff=0.16866 dB) */
  161. 0x1b, /* [060] -19.5 dB: AKM[027] = -19.504 dB (diff=0.00353 dB) */
  162. 0x1d, /* [061] -19.0 dB: AKM[029] = -18.883 dB (diff=0.11716 dB) */
  163. 0x1e, /* [062] -18.5 dB: AKM[030] = -18.588 dB (diff=0.08838 dB) */
  164. 0x20, /* [063] -18.0 dB: AKM[032] = -18.028 dB (diff=0.02780 dB) */
  165. 0x22, /* [064] -17.5 dB: AKM[034] = -17.501 dB (diff=0.00123 dB) */
  166. 0x24, /* [065] -17.0 dB: AKM[036] = -17.005 dB (diff=0.00475 dB) */
  167. 0x26, /* [066] -16.5 dB: AKM[038] = -16.535 dB (diff=0.03513 dB) */
  168. 0x28, /* [067] -16.0 dB: AKM[040] = -16.090 dB (diff=0.08960 dB) */
  169. 0x2b, /* [068] -15.5 dB: AKM[043] = -15.461 dB (diff=0.03857 dB) */
  170. 0x2d, /* [069] -15.0 dB: AKM[045] = -15.067 dB (diff=0.06655 dB) */
  171. 0x30, /* [070] -14.5 dB: AKM[048] = -14.506 dB (diff=0.00598 dB) */
  172. 0x33, /* [071] -14.0 dB: AKM[051] = -13.979 dB (diff=0.02060 dB) */
  173. 0x36, /* [072] -13.5 dB: AKM[054] = -13.483 dB (diff=0.01707 dB) */
  174. 0x39, /* [073] -13.0 dB: AKM[057] = -13.013 dB (diff=0.01331 dB) */
  175. 0x3c, /* [074] -12.5 dB: AKM[060] = -12.568 dB (diff=0.06778 dB) */
  176. 0x40, /* [075] -12.0 dB: AKM[064] = -12.007 dB (diff=0.00720 dB) */
  177. 0x44, /* [076] -11.5 dB: AKM[068] = -11.481 dB (diff=0.01937 dB) */
  178. 0x48, /* [077] -11.0 dB: AKM[072] = -10.984 dB (diff=0.01585 dB) */
  179. 0x4c, /* [078] -10.5 dB: AKM[076] = -10.515 dB (diff=0.01453 dB) */
  180. 0x51, /* [079] -10.0 dB: AKM[081] = -9.961 dB (diff=0.03890 dB) */
  181. 0x55, /* [080] -9.5 dB: AKM[085] = -9.542 dB (diff=0.04243 dB) */
  182. 0x5a, /* [081] -9.0 dB: AKM[090] = -9.046 dB (diff=0.04595 dB) */
  183. 0x60, /* [082] -8.5 dB: AKM[096] = -8.485 dB (diff=0.01462 dB) */
  184. 0x66, /* [083] -8.0 dB: AKM[102] = -7.959 dB (diff=0.04120 dB) */
  185. 0x6c, /* [084] -7.5 dB: AKM[108] = -7.462 dB (diff=0.03767 dB) */
  186. 0x72, /* [085] -7.0 dB: AKM[114] = -6.993 dB (diff=0.00729 dB) */
  187. 0x79, /* [086] -6.5 dB: AKM[121] = -6.475 dB (diff=0.02490 dB) */
  188. 0x80, /* [087] -6.0 dB: AKM[128] = -5.987 dB (diff=0.01340 dB) */
  189. 0x87, /* [088] -5.5 dB: AKM[135] = -5.524 dB (diff=0.02413 dB) */
  190. 0x8f, /* [089] -5.0 dB: AKM[143] = -5.024 dB (diff=0.02408 dB) */
  191. 0x98, /* [090] -4.5 dB: AKM[152] = -4.494 dB (diff=0.00607 dB) */
  192. 0xa1, /* [091] -4.0 dB: AKM[161] = -3.994 dB (diff=0.00571 dB) */
  193. 0xaa, /* [092] -3.5 dB: AKM[170] = -3.522 dB (diff=0.02183 dB) */
  194. 0xb5, /* [093] -3.0 dB: AKM[181] = -2.977 dB (diff=0.02277 dB) */
  195. 0xbf, /* [094] -2.5 dB: AKM[191] = -2.510 dB (diff=0.01014 dB) */
  196. 0xcb, /* [095] -2.0 dB: AKM[203] = -1.981 dB (diff=0.01912 dB) */
  197. 0xd7, /* [096] -1.5 dB: AKM[215] = -1.482 dB (diff=0.01797 dB) */
  198. 0xe3, /* [097] -1.0 dB: AKM[227] = -1.010 dB (diff=0.01029 dB) */
  199. 0xf1, /* [098] -0.5 dB: AKM[241] = -0.490 dB (diff=0.00954 dB) */
  200. 0xff, /* [099] +0.0 dB: AKM[255] = +0.000 dB (diff=0.00000 dB) */
  201. };
  202. static void hr222_config_akm(struct pcxhr_mgr *mgr, unsigned short data)
  203. {
  204. unsigned short mask = 0x8000;
  205. /* activate access to codec registers */
  206. PCXHR_INPB(mgr, PCXHR_XLX_HIFREQ);
  207. while (mask) {
  208. PCXHR_OUTPB(mgr, PCXHR_XLX_DATA,
  209. data & mask ? PCXHR_DATA_CODEC : 0);
  210. mask >>= 1;
  211. }
  212. /* termiate access to codec registers */
  213. PCXHR_INPB(mgr, PCXHR_XLX_RUER);
  214. }
  215. static int hr222_set_hw_playback_level(struct pcxhr_mgr *mgr,
  216. int idx, int level)
  217. {
  218. unsigned short cmd;
  219. if (idx > 1 ||
  220. level < 0 ||
  221. level >= ARRAY_SIZE(g_hr222_p_level))
  222. return -EINVAL;
  223. if (idx == 0)
  224. cmd = AKM_LEFT_LEVEL_CMD;
  225. else
  226. cmd = AKM_RIGHT_LEVEL_CMD;
  227. /* conversion from PmBoardCodedLevel to AKM nonlinear programming */
  228. cmd += g_hr222_p_level[level];
  229. hr222_config_akm(mgr, cmd);
  230. return 0;
  231. }
  232. static int hr222_set_hw_capture_level(struct pcxhr_mgr *mgr,
  233. int level_l, int level_r, int level_mic)
  234. {
  235. /* program all input levels at the same time */
  236. unsigned int data;
  237. int i;
  238. if (!mgr->capture_chips)
  239. return -EINVAL; /* no PCX22 */
  240. data = ((level_mic & 0xff) << 24); /* micro is mono, but apply */
  241. data |= ((level_mic & 0xff) << 16); /* level on both channels */
  242. data |= ((level_r & 0xff) << 8); /* line input right channel */
  243. data |= (level_l & 0xff); /* line input left channel */
  244. PCXHR_INPB(mgr, PCXHR_XLX_DATA); /* activate input codec */
  245. /* send 32 bits (4 x 8 bits) */
  246. for (i = 0; i < 32; i++, data <<= 1) {
  247. PCXHR_OUTPB(mgr, PCXHR_XLX_DATA,
  248. (data & 0x80000000) ? PCXHR_DATA_CODEC : 0);
  249. }
  250. PCXHR_INPB(mgr, PCXHR_XLX_RUER); /* close input level codec */
  251. return 0;
  252. }
  253. static void hr222_micro_boost(struct pcxhr_mgr *mgr, int level);
  254. int hr222_sub_init(struct pcxhr_mgr *mgr)
  255. {
  256. unsigned char reg;
  257. mgr->board_has_analog = 1; /* analog always available */
  258. mgr->xlx_cfg = PCXHR_CFG_SYNCDSP_MASK;
  259. reg = PCXHR_INPB(mgr, PCXHR_XLX_STATUS);
  260. if (reg & PCXHR_STAT_MIC_CAPS)
  261. mgr->board_has_mic = 1; /* microphone available */
  262. dev_dbg(&mgr->pci->dev,
  263. "MIC input available = %d\n", mgr->board_has_mic);
  264. /* reset codec */
  265. PCXHR_OUTPB(mgr, PCXHR_DSP_RESET,
  266. PCXHR_DSP_RESET_DSP);
  267. msleep(5);
  268. mgr->dsp_reset = PCXHR_DSP_RESET_DSP |
  269. PCXHR_DSP_RESET_MUTE |
  270. PCXHR_DSP_RESET_CODEC;
  271. PCXHR_OUTPB(mgr, PCXHR_DSP_RESET, mgr->dsp_reset);
  272. /* hr222_write_gpo(mgr, 0); does the same */
  273. msleep(5);
  274. /* config AKM */
  275. hr222_config_akm(mgr, AKM_POWER_CONTROL_CMD);
  276. hr222_config_akm(mgr, AKM_CLOCK_INF_55K_CMD);
  277. hr222_config_akm(mgr, AKM_UNMUTE_CMD);
  278. hr222_config_akm(mgr, AKM_RESET_OFF_CMD);
  279. /* init micro boost */
  280. hr222_micro_boost(mgr, 0);
  281. return 0;
  282. }
  283. /* calc PLL register */
  284. /* TODO : there is a very similar fct in pcxhr.c */
  285. static int hr222_pll_freq_register(unsigned int freq,
  286. unsigned int *pllreg,
  287. unsigned int *realfreq)
  288. {
  289. unsigned int reg;
  290. if (freq < 6900 || freq > 219000)
  291. return -EINVAL;
  292. reg = (28224000 * 2) / freq;
  293. reg = (reg - 1) / 2;
  294. if (reg < 0x100)
  295. *pllreg = reg + 0xC00;
  296. else if (reg < 0x200)
  297. *pllreg = reg + 0x800;
  298. else if (reg < 0x400)
  299. *pllreg = reg & 0x1ff;
  300. else if (reg < 0x800) {
  301. *pllreg = ((reg >> 1) & 0x1ff) + 0x200;
  302. reg &= ~1;
  303. } else {
  304. *pllreg = ((reg >> 2) & 0x1ff) + 0x400;
  305. reg &= ~3;
  306. }
  307. if (realfreq)
  308. *realfreq = (28224000 / (reg + 1));
  309. return 0;
  310. }
  311. int hr222_sub_set_clock(struct pcxhr_mgr *mgr,
  312. unsigned int rate,
  313. int *changed)
  314. {
  315. unsigned int speed, pllreg = 0;
  316. int err;
  317. unsigned realfreq = rate;
  318. switch (mgr->use_clock_type) {
  319. case HR22_CLOCK_TYPE_INTERNAL:
  320. err = hr222_pll_freq_register(rate, &pllreg, &realfreq);
  321. if (err)
  322. return err;
  323. mgr->xlx_cfg &= ~(PCXHR_CFG_CLOCKIN_SEL_MASK |
  324. PCXHR_CFG_CLOCK_UER1_SEL_MASK);
  325. break;
  326. case HR22_CLOCK_TYPE_AES_SYNC:
  327. mgr->xlx_cfg |= PCXHR_CFG_CLOCKIN_SEL_MASK;
  328. mgr->xlx_cfg &= ~PCXHR_CFG_CLOCK_UER1_SEL_MASK;
  329. break;
  330. case HR22_CLOCK_TYPE_AES_1:
  331. if (!mgr->board_has_aes1)
  332. return -EINVAL;
  333. mgr->xlx_cfg |= (PCXHR_CFG_CLOCKIN_SEL_MASK |
  334. PCXHR_CFG_CLOCK_UER1_SEL_MASK);
  335. break;
  336. default:
  337. return -EINVAL;
  338. }
  339. hr222_config_akm(mgr, AKM_MUTE_CMD);
  340. if (mgr->use_clock_type == HR22_CLOCK_TYPE_INTERNAL) {
  341. PCXHR_OUTPB(mgr, PCXHR_XLX_HIFREQ, pllreg >> 8);
  342. PCXHR_OUTPB(mgr, PCXHR_XLX_LOFREQ, pllreg & 0xff);
  343. }
  344. /* set clock source */
  345. PCXHR_OUTPB(mgr, PCXHR_XLX_CFG, mgr->xlx_cfg);
  346. /* codec speed modes */
  347. speed = rate < 55000 ? 0 : 1;
  348. if (mgr->codec_speed != speed) {
  349. mgr->codec_speed = speed;
  350. if (speed == 0)
  351. hr222_config_akm(mgr, AKM_CLOCK_INF_55K_CMD);
  352. else
  353. hr222_config_akm(mgr, AKM_CLOCK_SUP_55K_CMD);
  354. }
  355. mgr->sample_rate_real = realfreq;
  356. mgr->cur_clock_type = mgr->use_clock_type;
  357. if (changed)
  358. *changed = 1;
  359. hr222_config_akm(mgr, AKM_UNMUTE_CMD);
  360. dev_dbg(&mgr->pci->dev, "set_clock to %dHz (realfreq=%d pllreg=%x)\n",
  361. rate, realfreq, pllreg);
  362. return 0;
  363. }
  364. int hr222_get_external_clock(struct pcxhr_mgr *mgr,
  365. enum pcxhr_clock_type clock_type,
  366. int *sample_rate)
  367. {
  368. int rate, calc_rate = 0;
  369. unsigned int ticks;
  370. unsigned char mask, reg;
  371. if (clock_type == HR22_CLOCK_TYPE_AES_SYNC) {
  372. mask = (PCXHR_SUER_CLOCK_PRESENT_MASK |
  373. PCXHR_SUER_DATA_PRESENT_MASK);
  374. reg = PCXHR_STAT_FREQ_SYNC_MASK;
  375. } else if (clock_type == HR22_CLOCK_TYPE_AES_1 && mgr->board_has_aes1) {
  376. mask = (PCXHR_SUER1_CLOCK_PRESENT_MASK |
  377. PCXHR_SUER1_DATA_PRESENT_MASK);
  378. reg = PCXHR_STAT_FREQ_UER1_MASK;
  379. } else {
  380. dev_dbg(&mgr->pci->dev,
  381. "get_external_clock : type %d not supported\n",
  382. clock_type);
  383. return -EINVAL; /* other clocks not supported */
  384. }
  385. if ((PCXHR_INPB(mgr, PCXHR_XLX_CSUER) & mask) != mask) {
  386. dev_dbg(&mgr->pci->dev,
  387. "get_external_clock(%d) = 0 Hz\n", clock_type);
  388. *sample_rate = 0;
  389. return 0; /* no external clock locked */
  390. }
  391. PCXHR_OUTPB(mgr, PCXHR_XLX_STATUS, reg); /* calculate freq */
  392. /* save the measured clock frequency */
  393. reg |= PCXHR_STAT_FREQ_SAVE_MASK;
  394. if (mgr->last_reg_stat != reg) {
  395. udelay(500); /* wait min 2 cycles of lowest freq (8000) */
  396. mgr->last_reg_stat = reg;
  397. }
  398. PCXHR_OUTPB(mgr, PCXHR_XLX_STATUS, reg); /* save */
  399. /* get the frequency */
  400. ticks = (unsigned int)PCXHR_INPB(mgr, PCXHR_XLX_CFG);
  401. ticks = (ticks & 0x03) << 8;
  402. ticks |= (unsigned int)PCXHR_INPB(mgr, PCXHR_DSP_RESET);
  403. if (ticks != 0)
  404. calc_rate = 28224000 / ticks;
  405. /* rounding */
  406. if (calc_rate > 184200)
  407. rate = 192000;
  408. else if (calc_rate > 152200)
  409. rate = 176400;
  410. else if (calc_rate > 112000)
  411. rate = 128000;
  412. else if (calc_rate > 92100)
  413. rate = 96000;
  414. else if (calc_rate > 76100)
  415. rate = 88200;
  416. else if (calc_rate > 56000)
  417. rate = 64000;
  418. else if (calc_rate > 46050)
  419. rate = 48000;
  420. else if (calc_rate > 38050)
  421. rate = 44100;
  422. else if (calc_rate > 28000)
  423. rate = 32000;
  424. else if (calc_rate > 23025)
  425. rate = 24000;
  426. else if (calc_rate > 19025)
  427. rate = 22050;
  428. else if (calc_rate > 14000)
  429. rate = 16000;
  430. else if (calc_rate > 11512)
  431. rate = 12000;
  432. else if (calc_rate > 9512)
  433. rate = 11025;
  434. else if (calc_rate > 7000)
  435. rate = 8000;
  436. else
  437. rate = 0;
  438. dev_dbg(&mgr->pci->dev, "External clock is at %d Hz (measured %d Hz)\n",
  439. rate, calc_rate);
  440. *sample_rate = rate;
  441. return 0;
  442. }
  443. int hr222_read_gpio(struct pcxhr_mgr *mgr, int is_gpi, int *value)
  444. {
  445. if (is_gpi) {
  446. unsigned char reg = PCXHR_INPB(mgr, PCXHR_XLX_STATUS);
  447. *value = (int)(reg & PCXHR_STAT_GPI_MASK) >>
  448. PCXHR_STAT_GPI_OFFSET;
  449. } else {
  450. *value = (int)(mgr->dsp_reset & PCXHR_DSP_RESET_GPO_MASK) >>
  451. PCXHR_DSP_RESET_GPO_OFFSET;
  452. }
  453. return 0;
  454. }
  455. int hr222_write_gpo(struct pcxhr_mgr *mgr, int value)
  456. {
  457. unsigned char reg = mgr->dsp_reset & ~PCXHR_DSP_RESET_GPO_MASK;
  458. reg |= (unsigned char)(value << PCXHR_DSP_RESET_GPO_OFFSET) &
  459. PCXHR_DSP_RESET_GPO_MASK;
  460. PCXHR_OUTPB(mgr, PCXHR_DSP_RESET, reg);
  461. mgr->dsp_reset = reg;
  462. return 0;
  463. }
  464. int hr222_manage_timecode(struct pcxhr_mgr *mgr, int enable)
  465. {
  466. if (enable)
  467. mgr->dsp_reset |= PCXHR_DSP_RESET_SMPTE;
  468. else
  469. mgr->dsp_reset &= ~PCXHR_DSP_RESET_SMPTE;
  470. PCXHR_OUTPB(mgr, PCXHR_DSP_RESET, mgr->dsp_reset);
  471. return 0;
  472. }
  473. int hr222_update_analog_audio_level(struct snd_pcxhr *chip,
  474. int is_capture, int channel)
  475. {
  476. dev_dbg(chip->card->dev,
  477. "hr222_update_analog_audio_level(%s chan=%d)\n",
  478. is_capture ? "capture" : "playback", channel);
  479. if (is_capture) {
  480. int level_l, level_r, level_mic;
  481. /* we have to update all levels */
  482. if (chip->analog_capture_active) {
  483. level_l = chip->analog_capture_volume[0];
  484. level_r = chip->analog_capture_volume[1];
  485. } else {
  486. level_l = HR222_LINE_CAPTURE_LEVEL_MIN;
  487. level_r = HR222_LINE_CAPTURE_LEVEL_MIN;
  488. }
  489. if (chip->mic_active)
  490. level_mic = chip->mic_volume;
  491. else
  492. level_mic = HR222_MICRO_CAPTURE_LEVEL_MIN;
  493. return hr222_set_hw_capture_level(chip->mgr,
  494. level_l, level_r, level_mic);
  495. } else {
  496. int vol;
  497. if (chip->analog_playback_active[channel])
  498. vol = chip->analog_playback_volume[channel];
  499. else
  500. vol = HR222_LINE_PLAYBACK_LEVEL_MIN;
  501. return hr222_set_hw_playback_level(chip->mgr, channel, vol);
  502. }
  503. }
  504. /*texts[5] = {"Line", "Digital", "Digi+SRC", "Mic", "Line+Mic"}*/
  505. #define SOURCE_LINE 0
  506. #define SOURCE_DIGITAL 1
  507. #define SOURCE_DIGISRC 2
  508. #define SOURCE_MIC 3
  509. #define SOURCE_LINEMIC 4
  510. int hr222_set_audio_source(struct snd_pcxhr *chip)
  511. {
  512. int digital = 0;
  513. /* default analog source */
  514. chip->mgr->xlx_cfg &= ~(PCXHR_CFG_SRC_MASK |
  515. PCXHR_CFG_DATAIN_SEL_MASK |
  516. PCXHR_CFG_DATA_UER1_SEL_MASK);
  517. if (chip->audio_capture_source == SOURCE_DIGISRC) {
  518. chip->mgr->xlx_cfg |= PCXHR_CFG_SRC_MASK;
  519. digital = 1;
  520. } else {
  521. if (chip->audio_capture_source == SOURCE_DIGITAL)
  522. digital = 1;
  523. }
  524. if (digital) {
  525. chip->mgr->xlx_cfg |= PCXHR_CFG_DATAIN_SEL_MASK;
  526. if (chip->mgr->board_has_aes1) {
  527. /* get data from the AES1 plug */
  528. chip->mgr->xlx_cfg |= PCXHR_CFG_DATA_UER1_SEL_MASK;
  529. }
  530. /* chip->mic_active = 0; */
  531. /* chip->analog_capture_active = 0; */
  532. } else {
  533. int update_lvl = 0;
  534. chip->analog_capture_active = 0;
  535. chip->mic_active = 0;
  536. if (chip->audio_capture_source == SOURCE_LINE ||
  537. chip->audio_capture_source == SOURCE_LINEMIC) {
  538. if (chip->analog_capture_active == 0)
  539. update_lvl = 1;
  540. chip->analog_capture_active = 1;
  541. }
  542. if (chip->audio_capture_source == SOURCE_MIC ||
  543. chip->audio_capture_source == SOURCE_LINEMIC) {
  544. if (chip->mic_active == 0)
  545. update_lvl = 1;
  546. chip->mic_active = 1;
  547. }
  548. if (update_lvl) {
  549. /* capture: update all 3 mutes/unmutes with one call */
  550. hr222_update_analog_audio_level(chip, 1, 0);
  551. }
  552. }
  553. /* set the source infos (max 3 bits modified) */
  554. PCXHR_OUTPB(chip->mgr, PCXHR_XLX_CFG, chip->mgr->xlx_cfg);
  555. return 0;
  556. }
  557. int hr222_iec958_capture_byte(struct snd_pcxhr *chip,
  558. int aes_idx, unsigned char *aes_bits)
  559. {
  560. unsigned char idx = (unsigned char)(aes_idx * 8);
  561. unsigned char temp = 0;
  562. unsigned char mask = chip->mgr->board_has_aes1 ?
  563. PCXHR_SUER1_BIT_C_READ_MASK : PCXHR_SUER_BIT_C_READ_MASK;
  564. int i;
  565. for (i = 0; i < 8; i++) {
  566. PCXHR_OUTPB(chip->mgr, PCXHR_XLX_RUER, idx++); /* idx < 192 */
  567. temp <<= 1;
  568. if (PCXHR_INPB(chip->mgr, PCXHR_XLX_CSUER) & mask)
  569. temp |= 1;
  570. }
  571. dev_dbg(chip->card->dev, "read iec958 AES %d byte %d = 0x%x\n",
  572. chip->chip_idx, aes_idx, temp);
  573. *aes_bits = temp;
  574. return 0;
  575. }
  576. int hr222_iec958_update_byte(struct snd_pcxhr *chip,
  577. int aes_idx, unsigned char aes_bits)
  578. {
  579. int i;
  580. unsigned char new_bits = aes_bits;
  581. unsigned char old_bits = chip->aes_bits[aes_idx];
  582. unsigned char idx = (unsigned char)(aes_idx * 8);
  583. for (i = 0; i < 8; i++) {
  584. if ((old_bits & 0x01) != (new_bits & 0x01)) {
  585. /* idx < 192 */
  586. PCXHR_OUTPB(chip->mgr, PCXHR_XLX_RUER, idx);
  587. /* write C and U bit */
  588. PCXHR_OUTPB(chip->mgr, PCXHR_XLX_CSUER, new_bits&0x01 ?
  589. PCXHR_SUER_BIT_C_WRITE_MASK : 0);
  590. }
  591. idx++;
  592. old_bits >>= 1;
  593. new_bits >>= 1;
  594. }
  595. chip->aes_bits[aes_idx] = aes_bits;
  596. return 0;
  597. }
  598. static void hr222_micro_boost(struct pcxhr_mgr *mgr, int level)
  599. {
  600. unsigned char boost_mask;
  601. boost_mask = (unsigned char) (level << PCXHR_SELMIC_PREAMPLI_OFFSET);
  602. if (boost_mask & (~PCXHR_SELMIC_PREAMPLI_MASK))
  603. return; /* only values form 0 to 3 accepted */
  604. mgr->xlx_selmic &= ~PCXHR_SELMIC_PREAMPLI_MASK;
  605. mgr->xlx_selmic |= boost_mask;
  606. PCXHR_OUTPB(mgr, PCXHR_XLX_SELMIC, mgr->xlx_selmic);
  607. dev_dbg(&mgr->pci->dev, "hr222_micro_boost : set %x\n", boost_mask);
  608. }
  609. static void hr222_phantom_power(struct pcxhr_mgr *mgr, int power)
  610. {
  611. if (power)
  612. mgr->xlx_selmic |= PCXHR_SELMIC_PHANTOM_ALIM;
  613. else
  614. mgr->xlx_selmic &= ~PCXHR_SELMIC_PHANTOM_ALIM;
  615. PCXHR_OUTPB(mgr, PCXHR_XLX_SELMIC, mgr->xlx_selmic);
  616. dev_dbg(&mgr->pci->dev, "hr222_phantom_power : set %d\n", power);
  617. }
  618. /* mic level */
  619. static const DECLARE_TLV_DB_SCALE(db_scale_mic_hr222, -9850, 50, 650);
  620. static int hr222_mic_vol_info(struct snd_kcontrol *kcontrol,
  621. struct snd_ctl_elem_info *uinfo)
  622. {
  623. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  624. uinfo->count = 1;
  625. uinfo->value.integer.min = HR222_MICRO_CAPTURE_LEVEL_MIN; /* -98 dB */
  626. /* gains from 9 dB to 31.5 dB not recommended; use micboost instead */
  627. uinfo->value.integer.max = HR222_MICRO_CAPTURE_LEVEL_MAX; /* +7 dB */
  628. return 0;
  629. }
  630. static int hr222_mic_vol_get(struct snd_kcontrol *kcontrol,
  631. struct snd_ctl_elem_value *ucontrol)
  632. {
  633. struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
  634. mutex_lock(&chip->mgr->mixer_mutex);
  635. ucontrol->value.integer.value[0] = chip->mic_volume;
  636. mutex_unlock(&chip->mgr->mixer_mutex);
  637. return 0;
  638. }
  639. static int hr222_mic_vol_put(struct snd_kcontrol *kcontrol,
  640. struct snd_ctl_elem_value *ucontrol)
  641. {
  642. struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
  643. int changed = 0;
  644. mutex_lock(&chip->mgr->mixer_mutex);
  645. if (chip->mic_volume != ucontrol->value.integer.value[0]) {
  646. changed = 1;
  647. chip->mic_volume = ucontrol->value.integer.value[0];
  648. hr222_update_analog_audio_level(chip, 1, 0);
  649. }
  650. mutex_unlock(&chip->mgr->mixer_mutex);
  651. return changed;
  652. }
  653. static struct snd_kcontrol_new hr222_control_mic_level = {
  654. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  655. .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
  656. SNDRV_CTL_ELEM_ACCESS_TLV_READ),
  657. .name = "Mic Capture Volume",
  658. .info = hr222_mic_vol_info,
  659. .get = hr222_mic_vol_get,
  660. .put = hr222_mic_vol_put,
  661. .tlv = { .p = db_scale_mic_hr222 },
  662. };
  663. /* mic boost level */
  664. static const DECLARE_TLV_DB_SCALE(db_scale_micboost_hr222, 0, 1800, 5400);
  665. static int hr222_mic_boost_info(struct snd_kcontrol *kcontrol,
  666. struct snd_ctl_elem_info *uinfo)
  667. {
  668. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  669. uinfo->count = 1;
  670. uinfo->value.integer.min = 0; /* 0 dB */
  671. uinfo->value.integer.max = 3; /* 54 dB */
  672. return 0;
  673. }
  674. static int hr222_mic_boost_get(struct snd_kcontrol *kcontrol,
  675. struct snd_ctl_elem_value *ucontrol)
  676. {
  677. struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
  678. mutex_lock(&chip->mgr->mixer_mutex);
  679. ucontrol->value.integer.value[0] = chip->mic_boost;
  680. mutex_unlock(&chip->mgr->mixer_mutex);
  681. return 0;
  682. }
  683. static int hr222_mic_boost_put(struct snd_kcontrol *kcontrol,
  684. struct snd_ctl_elem_value *ucontrol)
  685. {
  686. struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
  687. int changed = 0;
  688. mutex_lock(&chip->mgr->mixer_mutex);
  689. if (chip->mic_boost != ucontrol->value.integer.value[0]) {
  690. changed = 1;
  691. chip->mic_boost = ucontrol->value.integer.value[0];
  692. hr222_micro_boost(chip->mgr, chip->mic_boost);
  693. }
  694. mutex_unlock(&chip->mgr->mixer_mutex);
  695. return changed;
  696. }
  697. static struct snd_kcontrol_new hr222_control_mic_boost = {
  698. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  699. .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
  700. SNDRV_CTL_ELEM_ACCESS_TLV_READ),
  701. .name = "MicBoost Capture Volume",
  702. .info = hr222_mic_boost_info,
  703. .get = hr222_mic_boost_get,
  704. .put = hr222_mic_boost_put,
  705. .tlv = { .p = db_scale_micboost_hr222 },
  706. };
  707. /******************* Phantom power switch *******************/
  708. #define hr222_phantom_power_info snd_ctl_boolean_mono_info
  709. static int hr222_phantom_power_get(struct snd_kcontrol *kcontrol,
  710. struct snd_ctl_elem_value *ucontrol)
  711. {
  712. struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
  713. mutex_lock(&chip->mgr->mixer_mutex);
  714. ucontrol->value.integer.value[0] = chip->phantom_power;
  715. mutex_unlock(&chip->mgr->mixer_mutex);
  716. return 0;
  717. }
  718. static int hr222_phantom_power_put(struct snd_kcontrol *kcontrol,
  719. struct snd_ctl_elem_value *ucontrol)
  720. {
  721. struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
  722. int power, changed = 0;
  723. mutex_lock(&chip->mgr->mixer_mutex);
  724. power = !!ucontrol->value.integer.value[0];
  725. if (chip->phantom_power != power) {
  726. hr222_phantom_power(chip->mgr, power);
  727. chip->phantom_power = power;
  728. changed = 1;
  729. }
  730. mutex_unlock(&chip->mgr->mixer_mutex);
  731. return changed;
  732. }
  733. static struct snd_kcontrol_new hr222_phantom_power_switch = {
  734. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  735. .name = "Phantom Power Switch",
  736. .info = hr222_phantom_power_info,
  737. .get = hr222_phantom_power_get,
  738. .put = hr222_phantom_power_put,
  739. };
  740. int hr222_add_mic_controls(struct snd_pcxhr *chip)
  741. {
  742. int err;
  743. if (!chip->mgr->board_has_mic)
  744. return 0;
  745. /* controls */
  746. err = snd_ctl_add(chip->card, snd_ctl_new1(&hr222_control_mic_level,
  747. chip));
  748. if (err < 0)
  749. return err;
  750. err = snd_ctl_add(chip->card, snd_ctl_new1(&hr222_control_mic_boost,
  751. chip));
  752. if (err < 0)
  753. return err;
  754. err = snd_ctl_add(chip->card, snd_ctl_new1(&hr222_phantom_power_switch,
  755. chip));
  756. return err;
  757. }