pcxhr_mix22.c 27 KB

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