au8522_decoder.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. /*
  2. * Auvitek AU8522 QAM/8VSB demodulator driver and video decoder
  3. *
  4. * Copyright (C) 2009 Devin Heitmueller <dheitmueller@linuxtv.org>
  5. * Copyright (C) 2005-2008 Auvitek International, Ltd.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * As published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  20. * 02110-1301, USA.
  21. */
  22. /* Developer notes:
  23. *
  24. * VBI support is not yet working
  25. * Enough is implemented here for CVBS and S-Video inputs, but the actual
  26. * analog demodulator code isn't implemented (not needed for xc5000 since it
  27. * has its own demodulator and outputs CVBS)
  28. *
  29. */
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/videodev2.h>
  33. #include <linux/i2c.h>
  34. #include <linux/delay.h>
  35. #include <media/v4l2-common.h>
  36. #include <media/v4l2-device.h>
  37. #include "au8522.h"
  38. #include "au8522_priv.h"
  39. MODULE_AUTHOR("Devin Heitmueller");
  40. MODULE_LICENSE("GPL");
  41. static int au8522_analog_debug;
  42. module_param_named(analog_debug, au8522_analog_debug, int, 0644);
  43. MODULE_PARM_DESC(analog_debug,
  44. "Analog debugging messages [0=Off (default) 1=On]");
  45. struct au8522_register_config {
  46. u16 reg_name;
  47. u8 reg_val[8];
  48. };
  49. /* Video Decoder Filter Coefficients
  50. The values are as follows from left to right
  51. 0="ATV RF" 1="ATV RF13" 2="CVBS" 3="S-Video" 4="PAL" 5=CVBS13" 6="SVideo13"
  52. */
  53. static const struct au8522_register_config filter_coef[] = {
  54. {AU8522_FILTER_COEF_R410, {0x25, 0x00, 0x25, 0x25, 0x00, 0x00, 0x00} },
  55. {AU8522_FILTER_COEF_R411, {0x20, 0x00, 0x20, 0x20, 0x00, 0x00, 0x00} },
  56. {AU8522_FILTER_COEF_R412, {0x03, 0x00, 0x03, 0x03, 0x00, 0x00, 0x00} },
  57. {AU8522_FILTER_COEF_R413, {0xe6, 0x00, 0xe6, 0xe6, 0x00, 0x00, 0x00} },
  58. {AU8522_FILTER_COEF_R414, {0x40, 0x00, 0x40, 0x40, 0x00, 0x00, 0x00} },
  59. {AU8522_FILTER_COEF_R415, {0x1b, 0x00, 0x1b, 0x1b, 0x00, 0x00, 0x00} },
  60. {AU8522_FILTER_COEF_R416, {0xc0, 0x00, 0xc0, 0x04, 0x00, 0x00, 0x00} },
  61. {AU8522_FILTER_COEF_R417, {0x04, 0x00, 0x04, 0x04, 0x00, 0x00, 0x00} },
  62. {AU8522_FILTER_COEF_R418, {0x8c, 0x00, 0x8c, 0x8c, 0x00, 0x00, 0x00} },
  63. {AU8522_FILTER_COEF_R419, {0xa0, 0x40, 0xa0, 0xa0, 0x40, 0x40, 0x40} },
  64. {AU8522_FILTER_COEF_R41A, {0x21, 0x09, 0x21, 0x21, 0x09, 0x09, 0x09} },
  65. {AU8522_FILTER_COEF_R41B, {0x6c, 0x38, 0x6c, 0x6c, 0x38, 0x38, 0x38} },
  66. {AU8522_FILTER_COEF_R41C, {0x03, 0xff, 0x03, 0x03, 0xff, 0xff, 0xff} },
  67. {AU8522_FILTER_COEF_R41D, {0xbf, 0xc7, 0xbf, 0xbf, 0xc7, 0xc7, 0xc7} },
  68. {AU8522_FILTER_COEF_R41E, {0xa0, 0xdf, 0xa0, 0xa0, 0xdf, 0xdf, 0xdf} },
  69. {AU8522_FILTER_COEF_R41F, {0x10, 0x06, 0x10, 0x10, 0x06, 0x06, 0x06} },
  70. {AU8522_FILTER_COEF_R420, {0xae, 0x30, 0xae, 0xae, 0x30, 0x30, 0x30} },
  71. {AU8522_FILTER_COEF_R421, {0xc4, 0x01, 0xc4, 0xc4, 0x01, 0x01, 0x01} },
  72. {AU8522_FILTER_COEF_R422, {0x54, 0xdd, 0x54, 0x54, 0xdd, 0xdd, 0xdd} },
  73. {AU8522_FILTER_COEF_R423, {0xd0, 0xaf, 0xd0, 0xd0, 0xaf, 0xaf, 0xaf} },
  74. {AU8522_FILTER_COEF_R424, {0x1c, 0xf7, 0x1c, 0x1c, 0xf7, 0xf7, 0xf7} },
  75. {AU8522_FILTER_COEF_R425, {0x76, 0xdb, 0x76, 0x76, 0xdb, 0xdb, 0xdb} },
  76. {AU8522_FILTER_COEF_R426, {0x61, 0xc0, 0x61, 0x61, 0xc0, 0xc0, 0xc0} },
  77. {AU8522_FILTER_COEF_R427, {0xd1, 0x2f, 0xd1, 0xd1, 0x2f, 0x2f, 0x2f} },
  78. {AU8522_FILTER_COEF_R428, {0x84, 0xd8, 0x84, 0x84, 0xd8, 0xd8, 0xd8} },
  79. {AU8522_FILTER_COEF_R429, {0x06, 0xfb, 0x06, 0x06, 0xfb, 0xfb, 0xfb} },
  80. {AU8522_FILTER_COEF_R42A, {0x21, 0xd5, 0x21, 0x21, 0xd5, 0xd5, 0xd5} },
  81. {AU8522_FILTER_COEF_R42B, {0x0a, 0x3e, 0x0a, 0x0a, 0x3e, 0x3e, 0x3e} },
  82. {AU8522_FILTER_COEF_R42C, {0xe6, 0x15, 0xe6, 0xe6, 0x15, 0x15, 0x15} },
  83. {AU8522_FILTER_COEF_R42D, {0x01, 0x34, 0x01, 0x01, 0x34, 0x34, 0x34} },
  84. };
  85. #define NUM_FILTER_COEF (sizeof(filter_coef)\
  86. / sizeof(struct au8522_register_config))
  87. /* Registers 0x060b through 0x0652 are the LP Filter coefficients
  88. The values are as follows from left to right
  89. 0="SIF" 1="ATVRF/ATVRF13"
  90. Note: the "ATVRF/ATVRF13" mode has never been tested
  91. */
  92. static const struct au8522_register_config lpfilter_coef[] = {
  93. {0x060b, {0x21, 0x0b} },
  94. {0x060c, {0xad, 0xad} },
  95. {0x060d, {0x70, 0xf0} },
  96. {0x060e, {0xea, 0xe9} },
  97. {0x060f, {0xdd, 0xdd} },
  98. {0x0610, {0x08, 0x64} },
  99. {0x0611, {0x60, 0x60} },
  100. {0x0612, {0xf8, 0xb2} },
  101. {0x0613, {0x01, 0x02} },
  102. {0x0614, {0xe4, 0xb4} },
  103. {0x0615, {0x19, 0x02} },
  104. {0x0616, {0xae, 0x2e} },
  105. {0x0617, {0xee, 0xc5} },
  106. {0x0618, {0x56, 0x56} },
  107. {0x0619, {0x30, 0x58} },
  108. {0x061a, {0xf9, 0xf8} },
  109. {0x061b, {0x24, 0x64} },
  110. {0x061c, {0x07, 0x07} },
  111. {0x061d, {0x30, 0x30} },
  112. {0x061e, {0xa9, 0xed} },
  113. {0x061f, {0x09, 0x0b} },
  114. {0x0620, {0x42, 0xc2} },
  115. {0x0621, {0x1d, 0x2a} },
  116. {0x0622, {0xd6, 0x56} },
  117. {0x0623, {0x95, 0x8b} },
  118. {0x0624, {0x2b, 0x2b} },
  119. {0x0625, {0x30, 0x24} },
  120. {0x0626, {0x3e, 0x3e} },
  121. {0x0627, {0x62, 0xe2} },
  122. {0x0628, {0xe9, 0xf5} },
  123. {0x0629, {0x99, 0x19} },
  124. {0x062a, {0xd4, 0x11} },
  125. {0x062b, {0x03, 0x04} },
  126. {0x062c, {0xb5, 0x85} },
  127. {0x062d, {0x1e, 0x20} },
  128. {0x062e, {0x2a, 0xea} },
  129. {0x062f, {0xd7, 0xd2} },
  130. {0x0630, {0x15, 0x15} },
  131. {0x0631, {0xa3, 0xa9} },
  132. {0x0632, {0x1f, 0x1f} },
  133. {0x0633, {0xf9, 0xd1} },
  134. {0x0634, {0xc0, 0xc3} },
  135. {0x0635, {0x4d, 0x8d} },
  136. {0x0636, {0x21, 0x31} },
  137. {0x0637, {0x83, 0x83} },
  138. {0x0638, {0x08, 0x8c} },
  139. {0x0639, {0x19, 0x19} },
  140. {0x063a, {0x45, 0xa5} },
  141. {0x063b, {0xef, 0xec} },
  142. {0x063c, {0x8a, 0x8a} },
  143. {0x063d, {0xf4, 0xf6} },
  144. {0x063e, {0x8f, 0x8f} },
  145. {0x063f, {0x44, 0x0c} },
  146. {0x0640, {0xef, 0xf0} },
  147. {0x0641, {0x66, 0x66} },
  148. {0x0642, {0xcc, 0xd2} },
  149. {0x0643, {0x41, 0x41} },
  150. {0x0644, {0x63, 0x93} },
  151. {0x0645, {0x8e, 0x8e} },
  152. {0x0646, {0xa2, 0x42} },
  153. {0x0647, {0x7b, 0x7b} },
  154. {0x0648, {0x04, 0x04} },
  155. {0x0649, {0x00, 0x00} },
  156. {0x064a, {0x40, 0x40} },
  157. {0x064b, {0x8c, 0x98} },
  158. {0x064c, {0x00, 0x00} },
  159. {0x064d, {0x63, 0xc3} },
  160. {0x064e, {0x04, 0x04} },
  161. {0x064f, {0x20, 0x20} },
  162. {0x0650, {0x00, 0x00} },
  163. {0x0651, {0x40, 0x40} },
  164. {0x0652, {0x01, 0x01} },
  165. };
  166. #define NUM_LPFILTER_COEF (sizeof(lpfilter_coef)\
  167. / sizeof(struct au8522_register_config))
  168. static inline struct au8522_state *to_state(struct v4l2_subdev *sd)
  169. {
  170. return container_of(sd, struct au8522_state, sd);
  171. }
  172. static void setup_vbi(struct au8522_state *state, int aud_input)
  173. {
  174. int i;
  175. /* These are set to zero regardless of what mode we're in */
  176. au8522_writereg(state, AU8522_TVDEC_VBI_CTRL_H_REG017H, 0x00);
  177. au8522_writereg(state, AU8522_TVDEC_VBI_CTRL_L_REG018H, 0x00);
  178. au8522_writereg(state, AU8522_TVDEC_VBI_USER_TOTAL_BITS_REG019H, 0x00);
  179. au8522_writereg(state, AU8522_TVDEC_VBI_USER_TUNIT_H_REG01AH, 0x00);
  180. au8522_writereg(state, AU8522_TVDEC_VBI_USER_TUNIT_L_REG01BH, 0x00);
  181. au8522_writereg(state, AU8522_TVDEC_VBI_USER_THRESH1_REG01CH, 0x00);
  182. au8522_writereg(state, AU8522_TVDEC_VBI_USER_FRAME_PAT2_REG01EH, 0x00);
  183. au8522_writereg(state, AU8522_TVDEC_VBI_USER_FRAME_PAT1_REG01FH, 0x00);
  184. au8522_writereg(state, AU8522_TVDEC_VBI_USER_FRAME_PAT0_REG020H, 0x00);
  185. au8522_writereg(state, AU8522_TVDEC_VBI_USER_FRAME_MASK2_REG021H,
  186. 0x00);
  187. au8522_writereg(state, AU8522_TVDEC_VBI_USER_FRAME_MASK1_REG022H,
  188. 0x00);
  189. au8522_writereg(state, AU8522_TVDEC_VBI_USER_FRAME_MASK0_REG023H,
  190. 0x00);
  191. /* Setup the VBI registers */
  192. for (i = 0x30; i < 0x60; i++)
  193. au8522_writereg(state, i, 0x40);
  194. /* For some reason, every register is 0x40 except register 0x44
  195. (confirmed via the HVR-950q USB capture) */
  196. au8522_writereg(state, 0x44, 0x60);
  197. /* Enable VBI (we always do this regardless of whether the user is
  198. viewing closed caption info) */
  199. au8522_writereg(state, AU8522_TVDEC_VBI_CTRL_H_REG017H,
  200. AU8522_TVDEC_VBI_CTRL_H_REG017H_CCON);
  201. }
  202. static void setup_decoder_defaults(struct au8522_state *state, u8 input_mode)
  203. {
  204. int i;
  205. int filter_coef_type;
  206. /* Provide reasonable defaults for picture tuning values */
  207. au8522_writereg(state, AU8522_TVDEC_SHARPNESSREG009H, 0x07);
  208. au8522_writereg(state, AU8522_TVDEC_BRIGHTNESS_REG00AH, 0xed);
  209. au8522_writereg(state, AU8522_TVDEC_CONTRAST_REG00BH, 0x79);
  210. au8522_writereg(state, AU8522_TVDEC_SATURATION_CB_REG00CH, 0x80);
  211. au8522_writereg(state, AU8522_TVDEC_SATURATION_CR_REG00DH, 0x80);
  212. au8522_writereg(state, AU8522_TVDEC_HUE_H_REG00EH, 0x00);
  213. au8522_writereg(state, AU8522_TVDEC_HUE_L_REG00FH, 0x00);
  214. /* Other decoder registers */
  215. au8522_writereg(state, AU8522_TVDEC_INT_MASK_REG010H, 0x00);
  216. if (input_mode == 0x23) {
  217. /* S-Video input mapping */
  218. au8522_writereg(state, AU8522_VIDEO_MODE_REG011H, 0x04);
  219. } else {
  220. /* All other modes (CVBS/ATVRF etc.) */
  221. au8522_writereg(state, AU8522_VIDEO_MODE_REG011H, 0x00);
  222. }
  223. au8522_writereg(state, AU8522_TVDEC_PGA_REG012H,
  224. AU8522_TVDEC_PGA_REG012H_CVBS);
  225. au8522_writereg(state, AU8522_TVDEC_COMB_MODE_REG015H,
  226. AU8522_TVDEC_COMB_MODE_REG015H_CVBS);
  227. au8522_writereg(state, AU8522_TVDED_DBG_MODE_REG060H,
  228. AU8522_TVDED_DBG_MODE_REG060H_CVBS);
  229. au8522_writereg(state, AU8522_TVDEC_FORMAT_CTRL1_REG061H,
  230. AU8522_TVDEC_FORMAT_CTRL1_REG061H_FIELD_LEN_525 |
  231. AU8522_TVDEC_FORMAT_CTRL1_REG061H_LINE_LEN_63_492 |
  232. AU8522_TVDEC_FORMAT_CTRL1_REG061H_SUBCARRIER_NTSC_MN);
  233. au8522_writereg(state, AU8522_TVDEC_FORMAT_CTRL2_REG062H,
  234. AU8522_TVDEC_FORMAT_CTRL2_REG062H_STD_NTSC);
  235. au8522_writereg(state, AU8522_TVDEC_VCR_DET_LLIM_REG063H,
  236. AU8522_TVDEC_VCR_DET_LLIM_REG063H_CVBS);
  237. au8522_writereg(state, AU8522_TVDEC_VCR_DET_HLIM_REG064H,
  238. AU8522_TVDEC_VCR_DET_HLIM_REG064H_CVBS);
  239. au8522_writereg(state, AU8522_TVDEC_COMB_VDIF_THR1_REG065H,
  240. AU8522_TVDEC_COMB_VDIF_THR1_REG065H_CVBS);
  241. au8522_writereg(state, AU8522_TVDEC_COMB_VDIF_THR2_REG066H,
  242. AU8522_TVDEC_COMB_VDIF_THR2_REG066H_CVBS);
  243. au8522_writereg(state, AU8522_TVDEC_COMB_VDIF_THR3_REG067H,
  244. AU8522_TVDEC_COMB_VDIF_THR3_REG067H_CVBS);
  245. au8522_writereg(state, AU8522_TVDEC_COMB_NOTCH_THR_REG068H,
  246. AU8522_TVDEC_COMB_NOTCH_THR_REG068H_CVBS);
  247. au8522_writereg(state, AU8522_TVDEC_COMB_HDIF_THR1_REG069H,
  248. AU8522_TVDEC_COMB_HDIF_THR1_REG069H_CVBS);
  249. au8522_writereg(state, AU8522_TVDEC_COMB_HDIF_THR2_REG06AH,
  250. AU8522_TVDEC_COMB_HDIF_THR2_REG06AH_CVBS);
  251. au8522_writereg(state, AU8522_TVDEC_COMB_HDIF_THR3_REG06BH,
  252. AU8522_TVDEC_COMB_HDIF_THR3_REG06BH_CVBS);
  253. if (input_mode == AU8522_INPUT_CONTROL_REG081H_SVIDEO_CH13 ||
  254. input_mode == AU8522_INPUT_CONTROL_REG081H_SVIDEO_CH24) {
  255. au8522_writereg(state, AU8522_TVDEC_COMB_DCDIF_THR1_REG06CH,
  256. AU8522_TVDEC_COMB_DCDIF_THR1_REG06CH_SVIDEO);
  257. au8522_writereg(state, AU8522_TVDEC_COMB_DCDIF_THR2_REG06DH,
  258. AU8522_TVDEC_COMB_DCDIF_THR2_REG06DH_SVIDEO);
  259. } else {
  260. au8522_writereg(state, AU8522_TVDEC_COMB_DCDIF_THR1_REG06CH,
  261. AU8522_TVDEC_COMB_DCDIF_THR1_REG06CH_CVBS);
  262. au8522_writereg(state, AU8522_TVDEC_COMB_DCDIF_THR2_REG06DH,
  263. AU8522_TVDEC_COMB_DCDIF_THR2_REG06DH_CVBS);
  264. }
  265. au8522_writereg(state, AU8522_TVDEC_COMB_DCDIF_THR3_REG06EH,
  266. AU8522_TVDEC_COMB_DCDIF_THR3_REG06EH_CVBS);
  267. au8522_writereg(state, AU8522_TVDEC_UV_SEP_THR_REG06FH,
  268. AU8522_TVDEC_UV_SEP_THR_REG06FH_CVBS);
  269. au8522_writereg(state, AU8522_TVDEC_COMB_DC_THR1_NTSC_REG070H,
  270. AU8522_TVDEC_COMB_DC_THR1_NTSC_REG070H_CVBS);
  271. au8522_writereg(state, AU8522_REG071H, AU8522_REG071H_CVBS);
  272. au8522_writereg(state, AU8522_REG072H, AU8522_REG072H_CVBS);
  273. au8522_writereg(state, AU8522_TVDEC_COMB_DC_THR2_NTSC_REG073H,
  274. AU8522_TVDEC_COMB_DC_THR2_NTSC_REG073H_CVBS);
  275. au8522_writereg(state, AU8522_REG074H, AU8522_REG074H_CVBS);
  276. au8522_writereg(state, AU8522_REG075H, AU8522_REG075H_CVBS);
  277. au8522_writereg(state, AU8522_TVDEC_DCAGC_CTRL_REG077H,
  278. AU8522_TVDEC_DCAGC_CTRL_REG077H_CVBS);
  279. au8522_writereg(state, AU8522_TVDEC_PIC_START_ADJ_REG078H,
  280. AU8522_TVDEC_PIC_START_ADJ_REG078H_CVBS);
  281. au8522_writereg(state, AU8522_TVDEC_AGC_HIGH_LIMIT_REG079H,
  282. AU8522_TVDEC_AGC_HIGH_LIMIT_REG079H_CVBS);
  283. au8522_writereg(state, AU8522_TVDEC_MACROVISION_SYNC_THR_REG07AH,
  284. AU8522_TVDEC_MACROVISION_SYNC_THR_REG07AH_CVBS);
  285. au8522_writereg(state, AU8522_TVDEC_INTRP_CTRL_REG07BH,
  286. AU8522_TVDEC_INTRP_CTRL_REG07BH_CVBS);
  287. au8522_writereg(state, AU8522_TVDEC_AGC_LOW_LIMIT_REG0E4H,
  288. AU8522_TVDEC_AGC_LOW_LIMIT_REG0E4H_CVBS);
  289. au8522_writereg(state, AU8522_TOREGAAGC_REG0E5H,
  290. AU8522_TOREGAAGC_REG0E5H_CVBS);
  291. au8522_writereg(state, AU8522_REG016H, AU8522_REG016H_CVBS);
  292. setup_vbi(state, 0);
  293. if (input_mode == AU8522_INPUT_CONTROL_REG081H_SVIDEO_CH13 ||
  294. input_mode == AU8522_INPUT_CONTROL_REG081H_SVIDEO_CH24) {
  295. /* Despite what the table says, for the HVR-950q we still need
  296. to be in CVBS mode for the S-Video input (reason unknown). */
  297. /* filter_coef_type = 3; */
  298. filter_coef_type = 5;
  299. } else {
  300. filter_coef_type = 5;
  301. }
  302. /* Load the Video Decoder Filter Coefficients */
  303. for (i = 0; i < NUM_FILTER_COEF; i++) {
  304. au8522_writereg(state, filter_coef[i].reg_name,
  305. filter_coef[i].reg_val[filter_coef_type]);
  306. }
  307. /* It's not clear what these registers are for, but they are always
  308. set to the same value regardless of what mode we're in */
  309. au8522_writereg(state, AU8522_REG42EH, 0x87);
  310. au8522_writereg(state, AU8522_REG42FH, 0xa2);
  311. au8522_writereg(state, AU8522_REG430H, 0xbf);
  312. au8522_writereg(state, AU8522_REG431H, 0xcb);
  313. au8522_writereg(state, AU8522_REG432H, 0xa1);
  314. au8522_writereg(state, AU8522_REG433H, 0x41);
  315. au8522_writereg(state, AU8522_REG434H, 0x88);
  316. au8522_writereg(state, AU8522_REG435H, 0xc2);
  317. au8522_writereg(state, AU8522_REG436H, 0x3c);
  318. }
  319. static void au8522_setup_cvbs_mode(struct au8522_state *state)
  320. {
  321. /* here we're going to try the pre-programmed route */
  322. au8522_writereg(state, AU8522_MODULE_CLOCK_CONTROL_REG0A3H,
  323. AU8522_MODULE_CLOCK_CONTROL_REG0A3H_CVBS);
  324. /* PGA in automatic mode */
  325. au8522_writereg(state, AU8522_PGA_CONTROL_REG082H, 0x00);
  326. /* Enable clamping control */
  327. au8522_writereg(state, AU8522_CLAMPING_CONTROL_REG083H, 0x00);
  328. au8522_writereg(state, AU8522_INPUT_CONTROL_REG081H,
  329. AU8522_INPUT_CONTROL_REG081H_CVBS_CH1);
  330. setup_decoder_defaults(state, AU8522_INPUT_CONTROL_REG081H_CVBS_CH1);
  331. au8522_writereg(state, AU8522_SYSTEM_MODULE_CONTROL_0_REG0A4H,
  332. AU8522_SYSTEM_MODULE_CONTROL_0_REG0A4H_CVBS);
  333. }
  334. static void au8522_setup_cvbs_tuner_mode(struct au8522_state *state)
  335. {
  336. /* here we're going to try the pre-programmed route */
  337. au8522_writereg(state, AU8522_MODULE_CLOCK_CONTROL_REG0A3H,
  338. AU8522_MODULE_CLOCK_CONTROL_REG0A3H_CVBS);
  339. /* It's not clear why we have to have the PGA in automatic mode while
  340. enabling clamp control, but it's what Windows does */
  341. au8522_writereg(state, AU8522_PGA_CONTROL_REG082H, 0x00);
  342. /* Enable clamping control */
  343. au8522_writereg(state, AU8522_CLAMPING_CONTROL_REG083H, 0x0e);
  344. /* Disable automatic PGA (since the CVBS is coming from the tuner) */
  345. au8522_writereg(state, AU8522_PGA_CONTROL_REG082H, 0x10);
  346. /* Set input mode to CVBS on channel 4 with SIF audio input enabled */
  347. au8522_writereg(state, AU8522_INPUT_CONTROL_REG081H,
  348. AU8522_INPUT_CONTROL_REG081H_CVBS_CH4_SIF);
  349. setup_decoder_defaults(state,
  350. AU8522_INPUT_CONTROL_REG081H_CVBS_CH4_SIF);
  351. au8522_writereg(state, AU8522_SYSTEM_MODULE_CONTROL_0_REG0A4H,
  352. AU8522_SYSTEM_MODULE_CONTROL_0_REG0A4H_CVBS);
  353. }
  354. static void au8522_setup_svideo_mode(struct au8522_state *state)
  355. {
  356. au8522_writereg(state, AU8522_MODULE_CLOCK_CONTROL_REG0A3H,
  357. AU8522_MODULE_CLOCK_CONTROL_REG0A3H_SVIDEO);
  358. /* Set input to Y on Channe1, C on Channel 3 */
  359. au8522_writereg(state, AU8522_INPUT_CONTROL_REG081H,
  360. AU8522_INPUT_CONTROL_REG081H_SVIDEO_CH13);
  361. /* PGA in automatic mode */
  362. au8522_writereg(state, AU8522_PGA_CONTROL_REG082H, 0x00);
  363. /* Enable clamping control */
  364. au8522_writereg(state, AU8522_CLAMPING_CONTROL_REG083H, 0x00);
  365. setup_decoder_defaults(state,
  366. AU8522_INPUT_CONTROL_REG081H_SVIDEO_CH13);
  367. au8522_writereg(state, AU8522_SYSTEM_MODULE_CONTROL_0_REG0A4H,
  368. AU8522_SYSTEM_MODULE_CONTROL_0_REG0A4H_CVBS);
  369. }
  370. /* ----------------------------------------------------------------------- */
  371. static void disable_audio_input(struct au8522_state *state)
  372. {
  373. au8522_writereg(state, AU8522_AUDIO_VOLUME_L_REG0F2H, 0x00);
  374. au8522_writereg(state, AU8522_AUDIO_VOLUME_R_REG0F3H, 0x00);
  375. au8522_writereg(state, AU8522_AUDIO_VOLUME_REG0F4H, 0x00);
  376. au8522_writereg(state, AU8522_SYSTEM_MODULE_CONTROL_1_REG0A5H, 0x04);
  377. au8522_writereg(state, AU8522_I2S_CTRL_2_REG112H, 0x02);
  378. au8522_writereg(state, AU8522_SYSTEM_MODULE_CONTROL_0_REG0A4H,
  379. AU8522_SYSTEM_MODULE_CONTROL_0_REG0A4H_SVIDEO);
  380. }
  381. /* 0=disable, 1=SIF */
  382. static void set_audio_input(struct au8522_state *state, int aud_input)
  383. {
  384. int i;
  385. /* Note that this function needs to be used in conjunction with setting
  386. the input routing via register 0x81 */
  387. if (aud_input == AU8522_AUDIO_NONE) {
  388. disable_audio_input(state);
  389. return;
  390. }
  391. if (aud_input != AU8522_AUDIO_SIF) {
  392. /* The caller asked for a mode we don't currently support */
  393. printk(KERN_ERR "Unsupported audio mode requested! mode=%d\n",
  394. aud_input);
  395. return;
  396. }
  397. /* Load the Audio Decoder Filter Coefficients */
  398. for (i = 0; i < NUM_LPFILTER_COEF; i++) {
  399. au8522_writereg(state, lpfilter_coef[i].reg_name,
  400. lpfilter_coef[i].reg_val[0]);
  401. }
  402. /* Setup audio */
  403. au8522_writereg(state, AU8522_AUDIO_VOLUME_L_REG0F2H, 0x00);
  404. au8522_writereg(state, AU8522_AUDIO_VOLUME_R_REG0F3H, 0x00);
  405. au8522_writereg(state, AU8522_AUDIO_VOLUME_REG0F4H, 0x00);
  406. au8522_writereg(state, AU8522_I2C_CONTROL_REG1_REG091H, 0x80);
  407. au8522_writereg(state, AU8522_I2C_CONTROL_REG0_REG090H, 0x84);
  408. msleep(150);
  409. au8522_writereg(state, AU8522_SYSTEM_MODULE_CONTROL_0_REG0A4H, 0x00);
  410. msleep(1);
  411. au8522_writereg(state, AU8522_SYSTEM_MODULE_CONTROL_0_REG0A4H, 0x9d);
  412. msleep(50);
  413. au8522_writereg(state, AU8522_AUDIO_VOLUME_L_REG0F2H, 0x7F);
  414. au8522_writereg(state, AU8522_AUDIO_VOLUME_R_REG0F3H, 0x7F);
  415. au8522_writereg(state, AU8522_AUDIO_VOLUME_REG0F4H, 0xff);
  416. msleep(80);
  417. au8522_writereg(state, AU8522_AUDIO_VOLUME_L_REG0F2H, 0x7F);
  418. au8522_writereg(state, AU8522_AUDIO_VOLUME_R_REG0F3H, 0x7F);
  419. au8522_writereg(state, AU8522_REG0F9H, AU8522_REG0F9H_AUDIO);
  420. au8522_writereg(state, AU8522_AUDIO_MODE_REG0F1H, 0x82);
  421. msleep(70);
  422. au8522_writereg(state, AU8522_SYSTEM_MODULE_CONTROL_1_REG0A5H, 0x09);
  423. au8522_writereg(state, AU8522_AUDIOFREQ_REG606H, 0x03);
  424. au8522_writereg(state, AU8522_I2S_CTRL_2_REG112H, 0xc2);
  425. }
  426. /* ----------------------------------------------------------------------- */
  427. static int au8522_s_ctrl(struct v4l2_ctrl *ctrl)
  428. {
  429. struct au8522_state *state =
  430. container_of(ctrl->handler, struct au8522_state, hdl);
  431. switch (ctrl->id) {
  432. case V4L2_CID_BRIGHTNESS:
  433. au8522_writereg(state, AU8522_TVDEC_BRIGHTNESS_REG00AH,
  434. ctrl->val - 128);
  435. break;
  436. case V4L2_CID_CONTRAST:
  437. au8522_writereg(state, AU8522_TVDEC_CONTRAST_REG00BH,
  438. ctrl->val);
  439. break;
  440. case V4L2_CID_SATURATION:
  441. au8522_writereg(state, AU8522_TVDEC_SATURATION_CB_REG00CH,
  442. ctrl->val);
  443. au8522_writereg(state, AU8522_TVDEC_SATURATION_CR_REG00DH,
  444. ctrl->val);
  445. break;
  446. case V4L2_CID_HUE:
  447. au8522_writereg(state, AU8522_TVDEC_HUE_H_REG00EH,
  448. ctrl->val >> 8);
  449. au8522_writereg(state, AU8522_TVDEC_HUE_L_REG00FH,
  450. ctrl->val & 0xFF);
  451. break;
  452. default:
  453. return -EINVAL;
  454. }
  455. return 0;
  456. }
  457. /* ----------------------------------------------------------------------- */
  458. #ifdef CONFIG_VIDEO_ADV_DEBUG
  459. static int au8522_g_register(struct v4l2_subdev *sd,
  460. struct v4l2_dbg_register *reg)
  461. {
  462. struct au8522_state *state = to_state(sd);
  463. reg->val = au8522_readreg(state, reg->reg & 0xffff);
  464. return 0;
  465. }
  466. static int au8522_s_register(struct v4l2_subdev *sd,
  467. const struct v4l2_dbg_register *reg)
  468. {
  469. struct au8522_state *state = to_state(sd);
  470. au8522_writereg(state, reg->reg, reg->val & 0xff);
  471. return 0;
  472. }
  473. #endif
  474. static int au8522_s_stream(struct v4l2_subdev *sd, int enable)
  475. {
  476. struct au8522_state *state = to_state(sd);
  477. if (enable) {
  478. au8522_writereg(state, AU8522_SYSTEM_MODULE_CONTROL_0_REG0A4H,
  479. 0x01);
  480. msleep(1);
  481. au8522_writereg(state, AU8522_SYSTEM_MODULE_CONTROL_0_REG0A4H,
  482. AU8522_SYSTEM_MODULE_CONTROL_0_REG0A4H_CVBS);
  483. } else {
  484. /* This does not completely power down the device
  485. (it only reduces it from around 140ma to 80ma) */
  486. au8522_writereg(state, AU8522_SYSTEM_MODULE_CONTROL_0_REG0A4H,
  487. 1 << 5);
  488. }
  489. return 0;
  490. }
  491. static int au8522_reset(struct v4l2_subdev *sd, u32 val)
  492. {
  493. struct au8522_state *state = to_state(sd);
  494. state->operational_mode = AU8522_ANALOG_MODE;
  495. /* Clear out any state associated with the digital side of the
  496. chip, so that when it gets powered back up it won't think
  497. that it is already tuned */
  498. state->current_frequency = 0;
  499. au8522_writereg(state, 0xa4, 1 << 5);
  500. return 0;
  501. }
  502. static int au8522_s_video_routing(struct v4l2_subdev *sd,
  503. u32 input, u32 output, u32 config)
  504. {
  505. struct au8522_state *state = to_state(sd);
  506. au8522_reset(sd, 0);
  507. if (input == AU8522_COMPOSITE_CH1) {
  508. au8522_setup_cvbs_mode(state);
  509. } else if (input == AU8522_SVIDEO_CH13) {
  510. au8522_setup_svideo_mode(state);
  511. } else if (input == AU8522_COMPOSITE_CH4_SIF) {
  512. au8522_setup_cvbs_tuner_mode(state);
  513. } else {
  514. printk(KERN_ERR "au8522 mode not currently supported\n");
  515. return -EINVAL;
  516. }
  517. return 0;
  518. }
  519. static int au8522_s_audio_routing(struct v4l2_subdev *sd,
  520. u32 input, u32 output, u32 config)
  521. {
  522. struct au8522_state *state = to_state(sd);
  523. set_audio_input(state, input);
  524. return 0;
  525. }
  526. static int au8522_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
  527. {
  528. int val = 0;
  529. struct au8522_state *state = to_state(sd);
  530. u8 lock_status;
  531. /* Interrogate the decoder to see if we are getting a real signal */
  532. lock_status = au8522_readreg(state, 0x00);
  533. if (lock_status == 0xa2)
  534. vt->signal = 0xffff;
  535. else
  536. vt->signal = 0x00;
  537. vt->capability |=
  538. V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LANG1 |
  539. V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP;
  540. val = V4L2_TUNER_SUB_MONO;
  541. vt->rxsubchans = val;
  542. vt->audmode = V4L2_TUNER_MODE_STEREO;
  543. return 0;
  544. }
  545. /* ----------------------------------------------------------------------- */
  546. static const struct v4l2_subdev_core_ops au8522_core_ops = {
  547. .log_status = v4l2_ctrl_subdev_log_status,
  548. .reset = au8522_reset,
  549. #ifdef CONFIG_VIDEO_ADV_DEBUG
  550. .g_register = au8522_g_register,
  551. .s_register = au8522_s_register,
  552. #endif
  553. };
  554. static const struct v4l2_subdev_tuner_ops au8522_tuner_ops = {
  555. .g_tuner = au8522_g_tuner,
  556. };
  557. static const struct v4l2_subdev_audio_ops au8522_audio_ops = {
  558. .s_routing = au8522_s_audio_routing,
  559. };
  560. static const struct v4l2_subdev_video_ops au8522_video_ops = {
  561. .s_routing = au8522_s_video_routing,
  562. .s_stream = au8522_s_stream,
  563. };
  564. static const struct v4l2_subdev_ops au8522_ops = {
  565. .core = &au8522_core_ops,
  566. .tuner = &au8522_tuner_ops,
  567. .audio = &au8522_audio_ops,
  568. .video = &au8522_video_ops,
  569. };
  570. static const struct v4l2_ctrl_ops au8522_ctrl_ops = {
  571. .s_ctrl = au8522_s_ctrl,
  572. };
  573. /* ----------------------------------------------------------------------- */
  574. static int au8522_probe(struct i2c_client *client,
  575. const struct i2c_device_id *did)
  576. {
  577. struct au8522_state *state;
  578. struct v4l2_ctrl_handler *hdl;
  579. struct v4l2_subdev *sd;
  580. int instance;
  581. struct au8522_config *demod_config;
  582. /* Check if the adapter supports the needed features */
  583. if (!i2c_check_functionality(client->adapter,
  584. I2C_FUNC_SMBUS_BYTE_DATA)) {
  585. return -EIO;
  586. }
  587. /* allocate memory for the internal state */
  588. instance = au8522_get_state(&state, client->adapter, client->addr);
  589. switch (instance) {
  590. case 0:
  591. printk(KERN_ERR "au8522_decoder allocation failed\n");
  592. return -EIO;
  593. case 1:
  594. /* new demod instance */
  595. printk(KERN_INFO "au8522_decoder creating new instance...\n");
  596. break;
  597. default:
  598. /* existing demod instance */
  599. printk(KERN_INFO "au8522_decoder attach existing instance.\n");
  600. break;
  601. }
  602. demod_config = kzalloc(sizeof(struct au8522_config), GFP_KERNEL);
  603. if (demod_config == NULL) {
  604. if (instance == 1)
  605. kfree(state);
  606. return -ENOMEM;
  607. }
  608. demod_config->demod_address = 0x8e >> 1;
  609. state->config = demod_config;
  610. state->i2c = client->adapter;
  611. sd = &state->sd;
  612. v4l2_i2c_subdev_init(sd, client, &au8522_ops);
  613. hdl = &state->hdl;
  614. v4l2_ctrl_handler_init(hdl, 4);
  615. v4l2_ctrl_new_std(hdl, &au8522_ctrl_ops,
  616. V4L2_CID_BRIGHTNESS, 0, 255, 1, 109);
  617. v4l2_ctrl_new_std(hdl, &au8522_ctrl_ops,
  618. V4L2_CID_CONTRAST, 0, 255, 1,
  619. AU8522_TVDEC_CONTRAST_REG00BH_CVBS);
  620. v4l2_ctrl_new_std(hdl, &au8522_ctrl_ops,
  621. V4L2_CID_SATURATION, 0, 255, 1, 128);
  622. v4l2_ctrl_new_std(hdl, &au8522_ctrl_ops,
  623. V4L2_CID_HUE, -32768, 32767, 1, 0);
  624. sd->ctrl_handler = hdl;
  625. if (hdl->error) {
  626. int err = hdl->error;
  627. v4l2_ctrl_handler_free(hdl);
  628. kfree(demod_config);
  629. kfree(state);
  630. return err;
  631. }
  632. state->c = client;
  633. state->vid_input = AU8522_COMPOSITE_CH1;
  634. state->aud_input = AU8522_AUDIO_NONE;
  635. state->id = 8522;
  636. state->rev = 0;
  637. /* Jam open the i2c gate to the tuner */
  638. au8522_writereg(state, 0x106, 1);
  639. return 0;
  640. }
  641. static int au8522_remove(struct i2c_client *client)
  642. {
  643. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  644. v4l2_device_unregister_subdev(sd);
  645. v4l2_ctrl_handler_free(sd->ctrl_handler);
  646. au8522_release_state(to_state(sd));
  647. return 0;
  648. }
  649. static const struct i2c_device_id au8522_id[] = {
  650. {"au8522", 0},
  651. {}
  652. };
  653. MODULE_DEVICE_TABLE(i2c, au8522_id);
  654. static struct i2c_driver au8522_driver = {
  655. .driver = {
  656. .owner = THIS_MODULE,
  657. .name = "au8522",
  658. },
  659. .probe = au8522_probe,
  660. .remove = au8522_remove,
  661. .id_table = au8522_id,
  662. };
  663. module_i2c_driver(au8522_driver);