au8522_decoder.c 24 KB

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