au8522_decoder.c 25 KB

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