cx18-av-core.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368
  1. /*
  2. * cx18 ADEC audio functions
  3. *
  4. * Derived from cx25840-core.c
  5. *
  6. * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
  7. * Copyright (C) 2008 Andy Walls <awalls@md.metrocast.net>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #include "cx18-driver.h"
  20. #include "cx18-io.h"
  21. #include "cx18-cards.h"
  22. int cx18_av_write(struct cx18 *cx, u16 addr, u8 value)
  23. {
  24. u32 reg = 0xc40000 + (addr & ~3);
  25. u32 mask = 0xff;
  26. int shift = (addr & 3) * 8;
  27. u32 x = cx18_read_reg(cx, reg);
  28. x = (x & ~(mask << shift)) | ((u32)value << shift);
  29. cx18_write_reg(cx, x, reg);
  30. return 0;
  31. }
  32. int cx18_av_write_expect(struct cx18 *cx, u16 addr, u8 value, u8 eval, u8 mask)
  33. {
  34. u32 reg = 0xc40000 + (addr & ~3);
  35. int shift = (addr & 3) * 8;
  36. u32 x = cx18_read_reg(cx, reg);
  37. x = (x & ~((u32)0xff << shift)) | ((u32)value << shift);
  38. cx18_write_reg_expect(cx, x, reg,
  39. ((u32)eval << shift), ((u32)mask << shift));
  40. return 0;
  41. }
  42. int cx18_av_write4(struct cx18 *cx, u16 addr, u32 value)
  43. {
  44. cx18_write_reg(cx, value, 0xc40000 + addr);
  45. return 0;
  46. }
  47. int
  48. cx18_av_write4_expect(struct cx18 *cx, u16 addr, u32 value, u32 eval, u32 mask)
  49. {
  50. cx18_write_reg_expect(cx, value, 0xc40000 + addr, eval, mask);
  51. return 0;
  52. }
  53. int cx18_av_write4_noretry(struct cx18 *cx, u16 addr, u32 value)
  54. {
  55. cx18_write_reg_noretry(cx, value, 0xc40000 + addr);
  56. return 0;
  57. }
  58. u8 cx18_av_read(struct cx18 *cx, u16 addr)
  59. {
  60. u32 x = cx18_read_reg(cx, 0xc40000 + (addr & ~3));
  61. int shift = (addr & 3) * 8;
  62. return (x >> shift) & 0xff;
  63. }
  64. u32 cx18_av_read4(struct cx18 *cx, u16 addr)
  65. {
  66. return cx18_read_reg(cx, 0xc40000 + addr);
  67. }
  68. int cx18_av_and_or(struct cx18 *cx, u16 addr, unsigned and_mask,
  69. u8 or_value)
  70. {
  71. return cx18_av_write(cx, addr,
  72. (cx18_av_read(cx, addr) & and_mask) |
  73. or_value);
  74. }
  75. int cx18_av_and_or4(struct cx18 *cx, u16 addr, u32 and_mask,
  76. u32 or_value)
  77. {
  78. return cx18_av_write4(cx, addr,
  79. (cx18_av_read4(cx, addr) & and_mask) |
  80. or_value);
  81. }
  82. static void cx18_av_init(struct cx18 *cx)
  83. {
  84. /*
  85. * The crystal freq used in calculations in this driver will be
  86. * 28.636360 MHz.
  87. * Aim to run the PLLs' VCOs near 400 MHz to minimze errors.
  88. */
  89. /*
  90. * VDCLK Integer = 0x0f, Post Divider = 0x04
  91. * AIMCLK Integer = 0x0e, Post Divider = 0x16
  92. */
  93. cx18_av_write4(cx, CXADEC_PLL_CTRL1, 0x160e040f);
  94. /* VDCLK Fraction = 0x2be2fe */
  95. /* xtal * 0xf.15f17f0/4 = 108 MHz: 432 MHz before post divide */
  96. cx18_av_write4(cx, CXADEC_VID_PLL_FRAC, 0x002be2fe);
  97. /* AIMCLK Fraction = 0x05227ad */
  98. /* xtal * 0xe.2913d68/0x16 = 48000 * 384: 406 MHz pre post-div*/
  99. cx18_av_write4(cx, CXADEC_AUX_PLL_FRAC, 0x005227ad);
  100. /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x16 */
  101. cx18_av_write(cx, CXADEC_I2S_MCLK, 0x56);
  102. }
  103. static void cx18_av_initialize(struct v4l2_subdev *sd)
  104. {
  105. struct cx18_av_state *state = to_cx18_av_state(sd);
  106. struct cx18 *cx = v4l2_get_subdevdata(sd);
  107. int default_volume;
  108. u32 v;
  109. cx18_av_loadfw(cx);
  110. /* Stop 8051 code execution */
  111. cx18_av_write4_expect(cx, CXADEC_DL_CTL, 0x03000000,
  112. 0x03000000, 0x13000000);
  113. /* initallize the PLL by toggling sleep bit */
  114. v = cx18_av_read4(cx, CXADEC_HOST_REG1);
  115. /* enable sleep mode - register appears to be read only... */
  116. cx18_av_write4_expect(cx, CXADEC_HOST_REG1, v | 1, v, 0xfffe);
  117. /* disable sleep mode */
  118. cx18_av_write4_expect(cx, CXADEC_HOST_REG1, v & 0xfffe,
  119. v & 0xfffe, 0xffff);
  120. /* initialize DLLs */
  121. v = cx18_av_read4(cx, CXADEC_DLL1_DIAG_CTRL) & 0xE1FFFEFF;
  122. /* disable FLD */
  123. cx18_av_write4(cx, CXADEC_DLL1_DIAG_CTRL, v);
  124. /* enable FLD */
  125. cx18_av_write4(cx, CXADEC_DLL1_DIAG_CTRL, v | 0x10000100);
  126. v = cx18_av_read4(cx, CXADEC_DLL2_DIAG_CTRL) & 0xE1FFFEFF;
  127. /* disable FLD */
  128. cx18_av_write4(cx, CXADEC_DLL2_DIAG_CTRL, v);
  129. /* enable FLD */
  130. cx18_av_write4(cx, CXADEC_DLL2_DIAG_CTRL, v | 0x06000100);
  131. /* set analog bias currents. Set Vreg to 1.20V. */
  132. cx18_av_write4(cx, CXADEC_AFE_DIAG_CTRL1, 0x000A1802);
  133. v = cx18_av_read4(cx, CXADEC_AFE_DIAG_CTRL3) | 1;
  134. /* enable TUNE_FIL_RST */
  135. cx18_av_write4_expect(cx, CXADEC_AFE_DIAG_CTRL3, v, v, 0x03009F0F);
  136. /* disable TUNE_FIL_RST */
  137. cx18_av_write4_expect(cx, CXADEC_AFE_DIAG_CTRL3,
  138. v & 0xFFFFFFFE, v & 0xFFFFFFFE, 0x03009F0F);
  139. /* enable 656 output */
  140. cx18_av_and_or4(cx, CXADEC_PIN_CTRL1, ~0, 0x040C00);
  141. /* video output drive strength */
  142. cx18_av_and_or4(cx, CXADEC_PIN_CTRL2, ~0, 0x2);
  143. /* reset video */
  144. cx18_av_write4(cx, CXADEC_SOFT_RST_CTRL, 0x8000);
  145. cx18_av_write4(cx, CXADEC_SOFT_RST_CTRL, 0);
  146. /*
  147. * Disable Video Auto-config of the Analog Front End and Video PLL.
  148. *
  149. * Since we only use BT.656 pixel mode, which works for both 525 and 625
  150. * line systems, it's just easier for us to set registers
  151. * 0x102 (CXADEC_CHIP_CTRL), 0x104-0x106 (CXADEC_AFE_CTRL),
  152. * 0x108-0x109 (CXADEC_PLL_CTRL1), and 0x10c-0x10f (CXADEC_VID_PLL_FRAC)
  153. * ourselves, than to run around cleaning up after the auto-config.
  154. *
  155. * (Note: my CX23418 chip doesn't seem to let the ACFG_DIS bit
  156. * get set to 1, but OTOH, it doesn't seem to do AFE and VID PLL
  157. * autoconfig either.)
  158. *
  159. * As a default, also turn off Dual mode for ADC2 and set ADC2 to CH3.
  160. */
  161. cx18_av_and_or4(cx, CXADEC_CHIP_CTRL, 0xFFFBFFFF, 0x00120000);
  162. /* Setup the Video and and Aux/Audio PLLs */
  163. cx18_av_init(cx);
  164. /* set video to auto-detect */
  165. /* Clear bits 11-12 to enable slow locking mode. Set autodetect mode */
  166. /* set the comb notch = 1 */
  167. cx18_av_and_or4(cx, CXADEC_MODE_CTRL, 0xFFF7E7F0, 0x02040800);
  168. /* Enable wtw_en in CRUSH_CTRL (Set bit 22) */
  169. /* Enable maj_sel in CRUSH_CTRL (Set bit 20) */
  170. cx18_av_and_or4(cx, CXADEC_CRUSH_CTRL, ~0, 0x00500000);
  171. /* Set VGA_TRACK_RANGE to 0x20 */
  172. cx18_av_and_or4(cx, CXADEC_DFE_CTRL2, 0xFFFF00FF, 0x00002000);
  173. /*
  174. * Initial VBI setup
  175. * VIP-1.1, 10 bit mode, enable Raw, disable sliced,
  176. * don't clamp raw samples when codes are in use, 1 byte user D-words,
  177. * IDID0 has line #, RP code V bit transition on VBLANK, data during
  178. * blanking intervals
  179. */
  180. cx18_av_write4(cx, CXADEC_OUT_CTRL1, 0x4013252e);
  181. /* Set the video input.
  182. The setting in MODE_CTRL gets lost when we do the above setup */
  183. /* EncSetSignalStd(dwDevNum, pEnc->dwSigStd); */
  184. /* EncSetVideoInput(dwDevNum, pEnc->VidIndSelection); */
  185. /*
  186. * Analog Front End (AFE)
  187. * Default to luma on ch1/ADC1, chroma on ch2/ADC2, SIF on ch3/ADC2
  188. * bypass_ch[1-3] use filter
  189. * droop_comp_ch[1-3] disable
  190. * clamp_en_ch[1-3] disable
  191. * aud_in_sel ADC2
  192. * luma_in_sel ADC1
  193. * chroma_in_sel ADC2
  194. * clamp_sel_ch[2-3] midcode
  195. * clamp_sel_ch1 video decoder
  196. * vga_sel_ch3 audio decoder
  197. * vga_sel_ch[1-2] video decoder
  198. * half_bw_ch[1-3] disable
  199. * +12db_ch[1-3] disable
  200. */
  201. cx18_av_and_or4(cx, CXADEC_AFE_CTRL, 0xFF000000, 0x00005D00);
  202. /* if(dwEnable && dw3DCombAvailable) { */
  203. /* CxDevWrReg(CXADEC_SRC_COMB_CFG, 0x7728021F); */
  204. /* } else { */
  205. /* CxDevWrReg(CXADEC_SRC_COMB_CFG, 0x6628021F); */
  206. /* } */
  207. cx18_av_write4(cx, CXADEC_SRC_COMB_CFG, 0x6628021F);
  208. default_volume = cx18_av_read(cx, 0x8d4);
  209. /*
  210. * Enforce the legacy volume scale mapping limits to avoid
  211. * -ERANGE errors when initializing the volume control
  212. */
  213. if (default_volume > 228) {
  214. /* Bottom out at -96 dB, v4l2 vol range 0x2e00-0x2fff */
  215. default_volume = 228;
  216. cx18_av_write(cx, 0x8d4, 228);
  217. } else if (default_volume < 20) {
  218. /* Top out at + 8 dB, v4l2 vol range 0xfe00-0xffff */
  219. default_volume = 20;
  220. cx18_av_write(cx, 0x8d4, 20);
  221. }
  222. default_volume = (((228 - default_volume) >> 1) + 23) << 9;
  223. state->volume->cur.val = state->volume->default_value = default_volume;
  224. v4l2_ctrl_handler_setup(&state->hdl);
  225. }
  226. static int cx18_av_reset(struct v4l2_subdev *sd, u32 val)
  227. {
  228. cx18_av_initialize(sd);
  229. return 0;
  230. }
  231. static int cx18_av_load_fw(struct v4l2_subdev *sd)
  232. {
  233. struct cx18_av_state *state = to_cx18_av_state(sd);
  234. if (!state->is_initialized) {
  235. /* initialize on first use */
  236. state->is_initialized = 1;
  237. cx18_av_initialize(sd);
  238. }
  239. return 0;
  240. }
  241. void cx18_av_std_setup(struct cx18 *cx)
  242. {
  243. struct cx18_av_state *state = &cx->av_state;
  244. struct v4l2_subdev *sd = &state->sd;
  245. v4l2_std_id std = state->std;
  246. /*
  247. * Video ADC crystal clock to pixel clock SRC decimation ratio
  248. * 28.636360 MHz/13.5 Mpps * 256 = 0x21f.07b
  249. */
  250. const int src_decimation = 0x21f;
  251. int hblank, hactive, burst, vblank, vactive, sc;
  252. int vblank656;
  253. int luma_lpf, uv_lpf, comb;
  254. u32 pll_int, pll_frac, pll_post;
  255. /* datasheet startup, step 8d */
  256. if (std & ~V4L2_STD_NTSC)
  257. cx18_av_write(cx, 0x49f, 0x11);
  258. else
  259. cx18_av_write(cx, 0x49f, 0x14);
  260. /*
  261. * Note: At the end of a field, there are 3 sets of half line duration
  262. * (double horizontal rate) pulses:
  263. *
  264. * 5 (625) or 6 (525) half-lines to blank for the vertical retrace
  265. * 5 (625) or 6 (525) vertical sync pulses of half line duration
  266. * 5 (625) or 6 (525) half-lines of equalization pulses
  267. */
  268. if (std & V4L2_STD_625_50) {
  269. /*
  270. * The following relationships of half line counts should hold:
  271. * 625 = vblank656 + vactive
  272. * 10 = vblank656 - vblank = vsync pulses + equalization pulses
  273. *
  274. * vblank656: half lines after line 625/mid-313 of blanked video
  275. * vblank: half lines, after line 5/317, of blanked video
  276. * vactive: half lines of active video +
  277. * 5 half lines after the end of active video
  278. *
  279. * As far as I can tell:
  280. * vblank656 starts counting from the falling edge of the first
  281. * vsync pulse (start of line 1 or mid-313)
  282. * vblank starts counting from the after the 5 vsync pulses and
  283. * 5 or 4 equalization pulses (start of line 6 or 318)
  284. *
  285. * For 625 line systems the driver will extract VBI information
  286. * from lines 6-23 and lines 318-335 (but the slicer can only
  287. * handle 17 lines, not the 18 in the vblank region).
  288. * In addition, we need vblank656 and vblank to be one whole
  289. * line longer, to cover line 24 and 336, so the SAV/EAV RP
  290. * codes get generated such that the encoder can actually
  291. * extract line 23 & 335 (WSS). We'll lose 1 line in each field
  292. * at the top of the screen.
  293. *
  294. * It appears the 5 half lines that happen after active
  295. * video must be included in vactive (579 instead of 574),
  296. * otherwise the colors get badly displayed in various regions
  297. * of the screen. I guess the chroma comb filter gets confused
  298. * without them (at least when a PVR-350 is the PAL source).
  299. */
  300. vblank656 = 48; /* lines 1 - 24 & 313 - 336 */
  301. vblank = 38; /* lines 6 - 24 & 318 - 336 */
  302. vactive = 579; /* lines 24 - 313 & 337 - 626 */
  303. /*
  304. * For a 13.5 Mpps clock and 15,625 Hz line rate, a line is
  305. * is 864 pixels = 720 active + 144 blanking. ITU-R BT.601
  306. * specifies 12 luma clock periods or ~ 0.9 * 13.5 Mpps after
  307. * the end of active video to start a horizontal line, so that
  308. * leaves 132 pixels of hblank to ignore.
  309. */
  310. hblank = 132;
  311. hactive = 720;
  312. /*
  313. * Burst gate delay (for 625 line systems)
  314. * Hsync leading edge to color burst rise = 5.6 us
  315. * Color burst width = 2.25 us
  316. * Gate width = 4 pixel clocks
  317. * (5.6 us + 2.25/2 us) * 13.5 Mpps + 4/2 clocks = 92.79 clocks
  318. */
  319. burst = 93;
  320. luma_lpf = 2;
  321. if (std & V4L2_STD_PAL) {
  322. uv_lpf = 1;
  323. comb = 0x20;
  324. /* sc = 4433618.75 * src_decimation/28636360 * 2^13 */
  325. sc = 688700;
  326. } else if (std == V4L2_STD_PAL_Nc) {
  327. uv_lpf = 1;
  328. comb = 0x20;
  329. /* sc = 3582056.25 * src_decimation/28636360 * 2^13 */
  330. sc = 556422;
  331. } else { /* SECAM */
  332. uv_lpf = 0;
  333. comb = 0;
  334. /* (fr + fb)/2 = (4406260 + 4250000)/2 = 4328130 */
  335. /* sc = 4328130 * src_decimation/28636360 * 2^13 */
  336. sc = 672314;
  337. }
  338. } else {
  339. /*
  340. * The following relationships of half line counts should hold:
  341. * 525 = prevsync + vblank656 + vactive
  342. * 12 = vblank656 - vblank = vsync pulses + equalization pulses
  343. *
  344. * prevsync: 6 half-lines before the vsync pulses
  345. * vblank656: half lines, after line 3/mid-266, of blanked video
  346. * vblank: half lines, after line 9/272, of blanked video
  347. * vactive: half lines of active video
  348. *
  349. * As far as I can tell:
  350. * vblank656 starts counting from the falling edge of the first
  351. * vsync pulse (start of line 4 or mid-266)
  352. * vblank starts counting from the after the 6 vsync pulses and
  353. * 6 or 5 equalization pulses (start of line 10 or 272)
  354. *
  355. * For 525 line systems the driver will extract VBI information
  356. * from lines 10-21 and lines 273-284.
  357. */
  358. vblank656 = 38; /* lines 4 - 22 & 266 - 284 */
  359. vblank = 26; /* lines 10 - 22 & 272 - 284 */
  360. vactive = 481; /* lines 23 - 263 & 285 - 525 */
  361. /*
  362. * For a 13.5 Mpps clock and 15,734.26 Hz line rate, a line is
  363. * is 858 pixels = 720 active + 138 blanking. The Hsync leading
  364. * edge should happen 1.2 us * 13.5 Mpps ~= 16 pixels after the
  365. * end of active video, leaving 122 pixels of hblank to ignore
  366. * before active video starts.
  367. */
  368. hactive = 720;
  369. hblank = 122;
  370. luma_lpf = 1;
  371. uv_lpf = 1;
  372. /*
  373. * Burst gate delay (for 525 line systems)
  374. * Hsync leading edge to color burst rise = 5.3 us
  375. * Color burst width = 2.5 us
  376. * Gate width = 4 pixel clocks
  377. * (5.3 us + 2.5/2 us) * 13.5 Mpps + 4/2 clocks = 90.425 clocks
  378. */
  379. if (std == V4L2_STD_PAL_60) {
  380. burst = 90;
  381. luma_lpf = 2;
  382. comb = 0x20;
  383. /* sc = 4433618.75 * src_decimation/28636360 * 2^13 */
  384. sc = 688700;
  385. } else if (std == V4L2_STD_PAL_M) {
  386. /* The 97 needs to be verified against PAL-M timings */
  387. burst = 97;
  388. comb = 0x20;
  389. /* sc = 3575611.49 * src_decimation/28636360 * 2^13 */
  390. sc = 555421;
  391. } else {
  392. burst = 90;
  393. comb = 0x66;
  394. /* sc = 3579545.45.. * src_decimation/28636360 * 2^13 */
  395. sc = 556032;
  396. }
  397. }
  398. /* DEBUG: Displays configured PLL frequency */
  399. pll_int = cx18_av_read(cx, 0x108);
  400. pll_frac = cx18_av_read4(cx, 0x10c) & 0x1ffffff;
  401. pll_post = cx18_av_read(cx, 0x109);
  402. CX18_DEBUG_INFO_DEV(sd, "PLL regs = int: %u, frac: %u, post: %u\n",
  403. pll_int, pll_frac, pll_post);
  404. if (pll_post) {
  405. int fsc, pll;
  406. u64 tmp;
  407. pll = (28636360L * ((((u64)pll_int) << 25) + pll_frac)) >> 25;
  408. pll /= pll_post;
  409. CX18_DEBUG_INFO_DEV(sd, "Video PLL = %d.%06d MHz\n",
  410. pll / 1000000, pll % 1000000);
  411. CX18_DEBUG_INFO_DEV(sd, "Pixel rate = %d.%06d Mpixel/sec\n",
  412. pll / 8000000, (pll / 8) % 1000000);
  413. CX18_DEBUG_INFO_DEV(sd, "ADC XTAL/pixel clock decimation ratio = %d.%03d\n",
  414. src_decimation / 256,
  415. ((src_decimation % 256) * 1000) / 256);
  416. tmp = 28636360 * (u64) sc;
  417. do_div(tmp, src_decimation);
  418. fsc = tmp >> 13;
  419. CX18_DEBUG_INFO_DEV(sd,
  420. "Chroma sub-carrier initial freq = %d.%06d MHz\n",
  421. fsc / 1000000, fsc % 1000000);
  422. CX18_DEBUG_INFO_DEV(sd,
  423. "hblank %i, hactive %i, vblank %i, vactive %i, vblank656 %i, src_dec %i, burst 0x%02x, luma_lpf %i, uv_lpf %i, comb 0x%02x, sc 0x%06x\n",
  424. hblank, hactive, vblank, vactive, vblank656,
  425. src_decimation, burst, luma_lpf, uv_lpf,
  426. comb, sc);
  427. }
  428. /* Sets horizontal blanking delay and active lines */
  429. cx18_av_write(cx, 0x470, hblank);
  430. cx18_av_write(cx, 0x471,
  431. (((hblank >> 8) & 0x3) | (hactive << 4)) & 0xff);
  432. cx18_av_write(cx, 0x472, hactive >> 4);
  433. /* Sets burst gate delay */
  434. cx18_av_write(cx, 0x473, burst);
  435. /* Sets vertical blanking delay and active duration */
  436. cx18_av_write(cx, 0x474, vblank);
  437. cx18_av_write(cx, 0x475,
  438. (((vblank >> 8) & 0x3) | (vactive << 4)) & 0xff);
  439. cx18_av_write(cx, 0x476, vactive >> 4);
  440. cx18_av_write(cx, 0x477, vblank656);
  441. /* Sets src decimation rate */
  442. cx18_av_write(cx, 0x478, src_decimation & 0xff);
  443. cx18_av_write(cx, 0x479, (src_decimation >> 8) & 0xff);
  444. /* Sets Luma and UV Low pass filters */
  445. cx18_av_write(cx, 0x47a, luma_lpf << 6 | ((uv_lpf << 4) & 0x30));
  446. /* Enables comb filters */
  447. cx18_av_write(cx, 0x47b, comb);
  448. /* Sets SC Step*/
  449. cx18_av_write(cx, 0x47c, sc);
  450. cx18_av_write(cx, 0x47d, (sc >> 8) & 0xff);
  451. cx18_av_write(cx, 0x47e, (sc >> 16) & 0xff);
  452. if (std & V4L2_STD_625_50) {
  453. state->slicer_line_delay = 1;
  454. state->slicer_line_offset = (6 + state->slicer_line_delay - 2);
  455. } else {
  456. state->slicer_line_delay = 0;
  457. state->slicer_line_offset = (10 + state->slicer_line_delay - 2);
  458. }
  459. cx18_av_write(cx, 0x47f, state->slicer_line_delay);
  460. }
  461. static void input_change(struct cx18 *cx)
  462. {
  463. struct cx18_av_state *state = &cx->av_state;
  464. v4l2_std_id std = state->std;
  465. u8 v;
  466. /* Follow step 8c and 8d of section 3.16 in the cx18_av datasheet */
  467. cx18_av_write(cx, 0x49f, (std & V4L2_STD_NTSC) ? 0x14 : 0x11);
  468. cx18_av_and_or(cx, 0x401, ~0x60, 0);
  469. cx18_av_and_or(cx, 0x401, ~0x60, 0x60);
  470. if (std & V4L2_STD_525_60) {
  471. if (std == V4L2_STD_NTSC_M_JP) {
  472. /* Japan uses EIAJ audio standard */
  473. cx18_av_write_expect(cx, 0x808, 0xf7, 0xf7, 0xff);
  474. cx18_av_write_expect(cx, 0x80b, 0x02, 0x02, 0x3f);
  475. } else if (std == V4L2_STD_NTSC_M_KR) {
  476. /* South Korea uses A2 audio standard */
  477. cx18_av_write_expect(cx, 0x808, 0xf8, 0xf8, 0xff);
  478. cx18_av_write_expect(cx, 0x80b, 0x03, 0x03, 0x3f);
  479. } else {
  480. /* Others use the BTSC audio standard */
  481. cx18_av_write_expect(cx, 0x808, 0xf6, 0xf6, 0xff);
  482. cx18_av_write_expect(cx, 0x80b, 0x01, 0x01, 0x3f);
  483. }
  484. } else if (std & V4L2_STD_PAL) {
  485. /* Follow tuner change procedure for PAL */
  486. cx18_av_write_expect(cx, 0x808, 0xff, 0xff, 0xff);
  487. cx18_av_write_expect(cx, 0x80b, 0x03, 0x03, 0x3f);
  488. } else if (std & V4L2_STD_SECAM) {
  489. /* Select autodetect for SECAM */
  490. cx18_av_write_expect(cx, 0x808, 0xff, 0xff, 0xff);
  491. cx18_av_write_expect(cx, 0x80b, 0x03, 0x03, 0x3f);
  492. }
  493. v = cx18_av_read(cx, 0x803);
  494. if (v & 0x10) {
  495. /* restart audio decoder microcontroller */
  496. v &= ~0x10;
  497. cx18_av_write_expect(cx, 0x803, v, v, 0x1f);
  498. v |= 0x10;
  499. cx18_av_write_expect(cx, 0x803, v, v, 0x1f);
  500. }
  501. }
  502. static int cx18_av_s_frequency(struct v4l2_subdev *sd,
  503. const struct v4l2_frequency *freq)
  504. {
  505. struct cx18 *cx = v4l2_get_subdevdata(sd);
  506. input_change(cx);
  507. return 0;
  508. }
  509. static int set_input(struct cx18 *cx, enum cx18_av_video_input vid_input,
  510. enum cx18_av_audio_input aud_input)
  511. {
  512. struct cx18_av_state *state = &cx->av_state;
  513. struct v4l2_subdev *sd = &state->sd;
  514. enum analog_signal_type {
  515. NONE, CVBS, Y, C, SIF, Pb, Pr
  516. } ch[3] = {NONE, NONE, NONE};
  517. u8 afe_mux_cfg;
  518. u8 adc2_cfg;
  519. u8 input_mode;
  520. u32 afe_cfg;
  521. int i;
  522. CX18_DEBUG_INFO_DEV(sd, "decoder set video input %d, audio input %d\n",
  523. vid_input, aud_input);
  524. if (vid_input >= CX18_AV_COMPOSITE1 &&
  525. vid_input <= CX18_AV_COMPOSITE8) {
  526. afe_mux_cfg = 0xf0 + (vid_input - CX18_AV_COMPOSITE1);
  527. ch[0] = CVBS;
  528. input_mode = 0x0;
  529. } else if (vid_input >= CX18_AV_COMPONENT_LUMA1) {
  530. int luma = vid_input & 0xf000;
  531. int r_chroma = vid_input & 0xf0000;
  532. int b_chroma = vid_input & 0xf00000;
  533. if ((vid_input & ~0xfff000) ||
  534. luma < CX18_AV_COMPONENT_LUMA1 ||
  535. luma > CX18_AV_COMPONENT_LUMA8 ||
  536. r_chroma < CX18_AV_COMPONENT_R_CHROMA4 ||
  537. r_chroma > CX18_AV_COMPONENT_R_CHROMA6 ||
  538. b_chroma < CX18_AV_COMPONENT_B_CHROMA7 ||
  539. b_chroma > CX18_AV_COMPONENT_B_CHROMA8) {
  540. CX18_ERR_DEV(sd, "0x%06x is not a valid video input!\n",
  541. vid_input);
  542. return -EINVAL;
  543. }
  544. afe_mux_cfg = (luma - CX18_AV_COMPONENT_LUMA1) >> 12;
  545. ch[0] = Y;
  546. afe_mux_cfg |= (r_chroma - CX18_AV_COMPONENT_R_CHROMA4) >> 12;
  547. ch[1] = Pr;
  548. afe_mux_cfg |= (b_chroma - CX18_AV_COMPONENT_B_CHROMA7) >> 14;
  549. ch[2] = Pb;
  550. input_mode = 0x6;
  551. } else {
  552. int luma = vid_input & 0xf0;
  553. int chroma = vid_input & 0xf00;
  554. if ((vid_input & ~0xff0) ||
  555. luma < CX18_AV_SVIDEO_LUMA1 ||
  556. luma > CX18_AV_SVIDEO_LUMA8 ||
  557. chroma < CX18_AV_SVIDEO_CHROMA4 ||
  558. chroma > CX18_AV_SVIDEO_CHROMA8) {
  559. CX18_ERR_DEV(sd, "0x%06x is not a valid video input!\n",
  560. vid_input);
  561. return -EINVAL;
  562. }
  563. afe_mux_cfg = 0xf0 + ((luma - CX18_AV_SVIDEO_LUMA1) >> 4);
  564. ch[0] = Y;
  565. if (chroma >= CX18_AV_SVIDEO_CHROMA7) {
  566. afe_mux_cfg &= 0x3f;
  567. afe_mux_cfg |= (chroma - CX18_AV_SVIDEO_CHROMA7) >> 2;
  568. ch[2] = C;
  569. } else {
  570. afe_mux_cfg &= 0xcf;
  571. afe_mux_cfg |= (chroma - CX18_AV_SVIDEO_CHROMA4) >> 4;
  572. ch[1] = C;
  573. }
  574. input_mode = 0x2;
  575. }
  576. switch (aud_input) {
  577. case CX18_AV_AUDIO_SERIAL1:
  578. case CX18_AV_AUDIO_SERIAL2:
  579. /* do nothing, use serial audio input */
  580. break;
  581. case CX18_AV_AUDIO4:
  582. afe_mux_cfg &= ~0x30;
  583. ch[1] = SIF;
  584. break;
  585. case CX18_AV_AUDIO5:
  586. afe_mux_cfg = (afe_mux_cfg & ~0x30) | 0x10;
  587. ch[1] = SIF;
  588. break;
  589. case CX18_AV_AUDIO6:
  590. afe_mux_cfg = (afe_mux_cfg & ~0x30) | 0x20;
  591. ch[1] = SIF;
  592. break;
  593. case CX18_AV_AUDIO7:
  594. afe_mux_cfg &= ~0xc0;
  595. ch[2] = SIF;
  596. break;
  597. case CX18_AV_AUDIO8:
  598. afe_mux_cfg = (afe_mux_cfg & ~0xc0) | 0x40;
  599. ch[2] = SIF;
  600. break;
  601. default:
  602. CX18_ERR_DEV(sd, "0x%04x is not a valid audio input!\n",
  603. aud_input);
  604. return -EINVAL;
  605. }
  606. /* Set up analog front end multiplexers */
  607. cx18_av_write_expect(cx, 0x103, afe_mux_cfg, afe_mux_cfg, 0xf7);
  608. /* Set INPUT_MODE to Composite, S-Video, or Component */
  609. cx18_av_and_or(cx, 0x401, ~0x6, input_mode);
  610. /* Set CH_SEL_ADC2 to 1 if input comes from CH3 */
  611. adc2_cfg = cx18_av_read(cx, 0x102);
  612. if (ch[2] == NONE)
  613. adc2_cfg &= ~0x2; /* No sig on CH3, set ADC2 to CH2 for input */
  614. else
  615. adc2_cfg |= 0x2; /* Signal on CH3, set ADC2 to CH3 for input */
  616. /* Set DUAL_MODE_ADC2 to 1 if input comes from both CH2 and CH3 */
  617. if (ch[1] != NONE && ch[2] != NONE)
  618. adc2_cfg |= 0x4; /* Set dual mode */
  619. else
  620. adc2_cfg &= ~0x4; /* Clear dual mode */
  621. cx18_av_write_expect(cx, 0x102, adc2_cfg, adc2_cfg, 0x17);
  622. /* Configure the analog front end */
  623. afe_cfg = cx18_av_read4(cx, CXADEC_AFE_CTRL);
  624. afe_cfg &= 0xff000000;
  625. afe_cfg |= 0x00005000; /* CHROMA_IN, AUD_IN: ADC2; LUMA_IN: ADC1 */
  626. if (ch[1] != NONE && ch[2] != NONE)
  627. afe_cfg |= 0x00000030; /* half_bw_ch[2-3] since in dual mode */
  628. for (i = 0; i < 3; i++) {
  629. switch (ch[i]) {
  630. default:
  631. case NONE:
  632. /* CLAMP_SEL = Fixed to midcode clamp level */
  633. afe_cfg |= (0x00000200 << i);
  634. break;
  635. case CVBS:
  636. case Y:
  637. if (i > 0)
  638. afe_cfg |= 0x00002000; /* LUMA_IN_SEL: ADC2 */
  639. break;
  640. case C:
  641. case Pb:
  642. case Pr:
  643. /* CLAMP_SEL = Fixed to midcode clamp level */
  644. afe_cfg |= (0x00000200 << i);
  645. if (i == 0 && ch[i] == C)
  646. afe_cfg &= ~0x00001000; /* CHROMA_IN_SEL ADC1 */
  647. break;
  648. case SIF:
  649. /*
  650. * VGA_GAIN_SEL = Audio Decoder
  651. * CLAMP_SEL = Fixed to midcode clamp level
  652. */
  653. afe_cfg |= (0x00000240 << i);
  654. if (i == 0)
  655. afe_cfg &= ~0x00004000; /* AUD_IN_SEL ADC1 */
  656. break;
  657. }
  658. }
  659. cx18_av_write4(cx, CXADEC_AFE_CTRL, afe_cfg);
  660. state->vid_input = vid_input;
  661. state->aud_input = aud_input;
  662. cx18_av_audio_set_path(cx);
  663. input_change(cx);
  664. return 0;
  665. }
  666. static int cx18_av_s_video_routing(struct v4l2_subdev *sd,
  667. u32 input, u32 output, u32 config)
  668. {
  669. struct cx18_av_state *state = to_cx18_av_state(sd);
  670. struct cx18 *cx = v4l2_get_subdevdata(sd);
  671. return set_input(cx, input, state->aud_input);
  672. }
  673. static int cx18_av_s_audio_routing(struct v4l2_subdev *sd,
  674. u32 input, u32 output, u32 config)
  675. {
  676. struct cx18_av_state *state = to_cx18_av_state(sd);
  677. struct cx18 *cx = v4l2_get_subdevdata(sd);
  678. return set_input(cx, state->vid_input, input);
  679. }
  680. static int cx18_av_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
  681. {
  682. struct cx18_av_state *state = to_cx18_av_state(sd);
  683. struct cx18 *cx = v4l2_get_subdevdata(sd);
  684. u8 vpres;
  685. u8 mode;
  686. int val = 0;
  687. if (state->radio)
  688. return 0;
  689. vpres = cx18_av_read(cx, 0x40e) & 0x20;
  690. vt->signal = vpres ? 0xffff : 0x0;
  691. vt->capability |=
  692. V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LANG1 |
  693. V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP;
  694. mode = cx18_av_read(cx, 0x804);
  695. /* get rxsubchans and audmode */
  696. if ((mode & 0xf) == 1)
  697. val |= V4L2_TUNER_SUB_STEREO;
  698. else
  699. val |= V4L2_TUNER_SUB_MONO;
  700. if (mode == 2 || mode == 4)
  701. val = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
  702. if (mode & 0x10)
  703. val |= V4L2_TUNER_SUB_SAP;
  704. vt->rxsubchans = val;
  705. vt->audmode = state->audmode;
  706. return 0;
  707. }
  708. static int cx18_av_s_tuner(struct v4l2_subdev *sd, const struct v4l2_tuner *vt)
  709. {
  710. struct cx18_av_state *state = to_cx18_av_state(sd);
  711. struct cx18 *cx = v4l2_get_subdevdata(sd);
  712. u8 v;
  713. if (state->radio)
  714. return 0;
  715. v = cx18_av_read(cx, 0x809);
  716. v &= ~0xf;
  717. switch (vt->audmode) {
  718. case V4L2_TUNER_MODE_MONO:
  719. /* mono -> mono
  720. stereo -> mono
  721. bilingual -> lang1 */
  722. break;
  723. case V4L2_TUNER_MODE_STEREO:
  724. case V4L2_TUNER_MODE_LANG1:
  725. /* mono -> mono
  726. stereo -> stereo
  727. bilingual -> lang1 */
  728. v |= 0x4;
  729. break;
  730. case V4L2_TUNER_MODE_LANG1_LANG2:
  731. /* mono -> mono
  732. stereo -> stereo
  733. bilingual -> lang1/lang2 */
  734. v |= 0x7;
  735. break;
  736. case V4L2_TUNER_MODE_LANG2:
  737. /* mono -> mono
  738. stereo -> stereo
  739. bilingual -> lang2 */
  740. v |= 0x1;
  741. break;
  742. default:
  743. return -EINVAL;
  744. }
  745. cx18_av_write_expect(cx, 0x809, v, v, 0xff);
  746. state->audmode = vt->audmode;
  747. return 0;
  748. }
  749. static int cx18_av_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
  750. {
  751. struct cx18_av_state *state = to_cx18_av_state(sd);
  752. struct cx18 *cx = v4l2_get_subdevdata(sd);
  753. u8 fmt = 0; /* zero is autodetect */
  754. u8 pal_m = 0;
  755. if (state->radio == 0 && state->std == norm)
  756. return 0;
  757. state->radio = 0;
  758. state->std = norm;
  759. /* First tests should be against specific std */
  760. if (state->std == V4L2_STD_NTSC_M_JP) {
  761. fmt = 0x2;
  762. } else if (state->std == V4L2_STD_NTSC_443) {
  763. fmt = 0x3;
  764. } else if (state->std == V4L2_STD_PAL_M) {
  765. pal_m = 1;
  766. fmt = 0x5;
  767. } else if (state->std == V4L2_STD_PAL_N) {
  768. fmt = 0x6;
  769. } else if (state->std == V4L2_STD_PAL_Nc) {
  770. fmt = 0x7;
  771. } else if (state->std == V4L2_STD_PAL_60) {
  772. fmt = 0x8;
  773. } else {
  774. /* Then, test against generic ones */
  775. if (state->std & V4L2_STD_NTSC)
  776. fmt = 0x1;
  777. else if (state->std & V4L2_STD_PAL)
  778. fmt = 0x4;
  779. else if (state->std & V4L2_STD_SECAM)
  780. fmt = 0xc;
  781. }
  782. CX18_DEBUG_INFO_DEV(sd, "changing video std to fmt %i\n", fmt);
  783. /* Follow step 9 of section 3.16 in the cx18_av datasheet.
  784. Without this PAL may display a vertical ghosting effect.
  785. This happens for example with the Yuan MPC622. */
  786. if (fmt >= 4 && fmt < 8) {
  787. /* Set format to NTSC-M */
  788. cx18_av_and_or(cx, 0x400, ~0xf, 1);
  789. /* Turn off LCOMB */
  790. cx18_av_and_or(cx, 0x47b, ~6, 0);
  791. }
  792. cx18_av_and_or(cx, 0x400, ~0x2f, fmt | 0x20);
  793. cx18_av_and_or(cx, 0x403, ~0x3, pal_m);
  794. cx18_av_std_setup(cx);
  795. input_change(cx);
  796. return 0;
  797. }
  798. static int cx18_av_s_radio(struct v4l2_subdev *sd)
  799. {
  800. struct cx18_av_state *state = to_cx18_av_state(sd);
  801. state->radio = 1;
  802. return 0;
  803. }
  804. static int cx18_av_s_ctrl(struct v4l2_ctrl *ctrl)
  805. {
  806. struct v4l2_subdev *sd = to_sd(ctrl);
  807. struct cx18 *cx = v4l2_get_subdevdata(sd);
  808. switch (ctrl->id) {
  809. case V4L2_CID_BRIGHTNESS:
  810. cx18_av_write(cx, 0x414, ctrl->val - 128);
  811. break;
  812. case V4L2_CID_CONTRAST:
  813. cx18_av_write(cx, 0x415, ctrl->val << 1);
  814. break;
  815. case V4L2_CID_SATURATION:
  816. cx18_av_write(cx, 0x420, ctrl->val << 1);
  817. cx18_av_write(cx, 0x421, ctrl->val << 1);
  818. break;
  819. case V4L2_CID_HUE:
  820. cx18_av_write(cx, 0x422, ctrl->val);
  821. break;
  822. default:
  823. return -EINVAL;
  824. }
  825. return 0;
  826. }
  827. static int cx18_av_set_fmt(struct v4l2_subdev *sd,
  828. struct v4l2_subdev_pad_config *cfg,
  829. struct v4l2_subdev_format *format)
  830. {
  831. struct v4l2_mbus_framefmt *fmt = &format->format;
  832. struct cx18_av_state *state = to_cx18_av_state(sd);
  833. struct cx18 *cx = v4l2_get_subdevdata(sd);
  834. int HSC, VSC, Vsrc, Hsrc, filter, Vlines;
  835. int is_50Hz = !(state->std & V4L2_STD_525_60);
  836. if (format->pad || fmt->code != MEDIA_BUS_FMT_FIXED)
  837. return -EINVAL;
  838. fmt->field = V4L2_FIELD_INTERLACED;
  839. fmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
  840. Vsrc = (cx18_av_read(cx, 0x476) & 0x3f) << 4;
  841. Vsrc |= (cx18_av_read(cx, 0x475) & 0xf0) >> 4;
  842. Hsrc = (cx18_av_read(cx, 0x472) & 0x3f) << 4;
  843. Hsrc |= (cx18_av_read(cx, 0x471) & 0xf0) >> 4;
  844. /*
  845. * This adjustment reflects the excess of vactive, set in
  846. * cx18_av_std_setup(), above standard values:
  847. *
  848. * 480 + 1 for 60 Hz systems
  849. * 576 + 3 for 50 Hz systems
  850. */
  851. Vlines = fmt->height + (is_50Hz ? 3 : 1);
  852. /*
  853. * Invalid height and width scaling requests are:
  854. * 1. width less than 1/16 of the source width
  855. * 2. width greater than the source width
  856. * 3. height less than 1/8 of the source height
  857. * 4. height greater than the source height
  858. */
  859. if ((fmt->width * 16 < Hsrc) || (Hsrc < fmt->width) ||
  860. (Vlines * 8 < Vsrc) || (Vsrc < Vlines)) {
  861. CX18_ERR_DEV(sd, "%dx%d is not a valid size!\n",
  862. fmt->width, fmt->height);
  863. return -ERANGE;
  864. }
  865. if (format->which == V4L2_SUBDEV_FORMAT_TRY)
  866. return 0;
  867. HSC = (Hsrc * (1 << 20)) / fmt->width - (1 << 20);
  868. VSC = (1 << 16) - (Vsrc * (1 << 9) / Vlines - (1 << 9));
  869. VSC &= 0x1fff;
  870. if (fmt->width >= 385)
  871. filter = 0;
  872. else if (fmt->width > 192)
  873. filter = 1;
  874. else if (fmt->width > 96)
  875. filter = 2;
  876. else
  877. filter = 3;
  878. CX18_DEBUG_INFO_DEV(sd,
  879. "decoder set size %dx%d -> scale %ux%u\n",
  880. fmt->width, fmt->height, HSC, VSC);
  881. /* HSCALE=HSC */
  882. cx18_av_write(cx, 0x418, HSC & 0xff);
  883. cx18_av_write(cx, 0x419, (HSC >> 8) & 0xff);
  884. cx18_av_write(cx, 0x41a, HSC >> 16);
  885. /* VSCALE=VSC */
  886. cx18_av_write(cx, 0x41c, VSC & 0xff);
  887. cx18_av_write(cx, 0x41d, VSC >> 8);
  888. /* VS_INTRLACE=1 VFILT=filter */
  889. cx18_av_write(cx, 0x41e, 0x8 | filter);
  890. return 0;
  891. }
  892. static int cx18_av_s_stream(struct v4l2_subdev *sd, int enable)
  893. {
  894. struct cx18 *cx = v4l2_get_subdevdata(sd);
  895. CX18_DEBUG_INFO_DEV(sd, "%s output\n", enable ? "enable" : "disable");
  896. if (enable) {
  897. cx18_av_write(cx, 0x115, 0x8c);
  898. cx18_av_write(cx, 0x116, 0x07);
  899. } else {
  900. cx18_av_write(cx, 0x115, 0x00);
  901. cx18_av_write(cx, 0x116, 0x00);
  902. }
  903. return 0;
  904. }
  905. static void log_video_status(struct cx18 *cx)
  906. {
  907. static const char *const fmt_strs[] = {
  908. "0x0",
  909. "NTSC-M", "NTSC-J", "NTSC-4.43",
  910. "PAL-BDGHI", "PAL-M", "PAL-N", "PAL-Nc", "PAL-60",
  911. "0x9", "0xA", "0xB",
  912. "SECAM",
  913. "0xD", "0xE", "0xF"
  914. };
  915. struct cx18_av_state *state = &cx->av_state;
  916. struct v4l2_subdev *sd = &state->sd;
  917. u8 vidfmt_sel = cx18_av_read(cx, 0x400) & 0xf;
  918. u8 gen_stat1 = cx18_av_read(cx, 0x40d);
  919. u8 gen_stat2 = cx18_av_read(cx, 0x40e);
  920. int vid_input = state->vid_input;
  921. CX18_INFO_DEV(sd, "Video signal: %spresent\n",
  922. (gen_stat2 & 0x20) ? "" : "not ");
  923. CX18_INFO_DEV(sd, "Detected format: %s\n",
  924. fmt_strs[gen_stat1 & 0xf]);
  925. CX18_INFO_DEV(sd, "Specified standard: %s\n",
  926. vidfmt_sel ? fmt_strs[vidfmt_sel]
  927. : "automatic detection");
  928. if (vid_input >= CX18_AV_COMPOSITE1 &&
  929. vid_input <= CX18_AV_COMPOSITE8) {
  930. CX18_INFO_DEV(sd, "Specified video input: Composite %d\n",
  931. vid_input - CX18_AV_COMPOSITE1 + 1);
  932. } else {
  933. CX18_INFO_DEV(sd, "Specified video input: S-Video (Luma In%d, Chroma In%d)\n",
  934. (vid_input & 0xf0) >> 4,
  935. (vid_input & 0xf00) >> 8);
  936. }
  937. CX18_INFO_DEV(sd, "Specified audioclock freq: %d Hz\n",
  938. state->audclk_freq);
  939. }
  940. static void log_audio_status(struct cx18 *cx)
  941. {
  942. struct cx18_av_state *state = &cx->av_state;
  943. struct v4l2_subdev *sd = &state->sd;
  944. u8 download_ctl = cx18_av_read(cx, 0x803);
  945. u8 mod_det_stat0 = cx18_av_read(cx, 0x804);
  946. u8 mod_det_stat1 = cx18_av_read(cx, 0x805);
  947. u8 audio_config = cx18_av_read(cx, 0x808);
  948. u8 pref_mode = cx18_av_read(cx, 0x809);
  949. u8 afc0 = cx18_av_read(cx, 0x80b);
  950. u8 mute_ctl = cx18_av_read(cx, 0x8d3);
  951. int aud_input = state->aud_input;
  952. char *p;
  953. switch (mod_det_stat0) {
  954. case 0x00: p = "mono"; break;
  955. case 0x01: p = "stereo"; break;
  956. case 0x02: p = "dual"; break;
  957. case 0x04: p = "tri"; break;
  958. case 0x10: p = "mono with SAP"; break;
  959. case 0x11: p = "stereo with SAP"; break;
  960. case 0x12: p = "dual with SAP"; break;
  961. case 0x14: p = "tri with SAP"; break;
  962. case 0xfe: p = "forced mode"; break;
  963. default: p = "not defined"; break;
  964. }
  965. CX18_INFO_DEV(sd, "Detected audio mode: %s\n", p);
  966. switch (mod_det_stat1) {
  967. case 0x00: p = "not defined"; break;
  968. case 0x01: p = "EIAJ"; break;
  969. case 0x02: p = "A2-M"; break;
  970. case 0x03: p = "A2-BG"; break;
  971. case 0x04: p = "A2-DK1"; break;
  972. case 0x05: p = "A2-DK2"; break;
  973. case 0x06: p = "A2-DK3"; break;
  974. case 0x07: p = "A1 (6.0 MHz FM Mono)"; break;
  975. case 0x08: p = "AM-L"; break;
  976. case 0x09: p = "NICAM-BG"; break;
  977. case 0x0a: p = "NICAM-DK"; break;
  978. case 0x0b: p = "NICAM-I"; break;
  979. case 0x0c: p = "NICAM-L"; break;
  980. case 0x0d: p = "BTSC/EIAJ/A2-M Mono (4.5 MHz FMMono)"; break;
  981. case 0x0e: p = "IF FM Radio"; break;
  982. case 0x0f: p = "BTSC"; break;
  983. case 0x10: p = "detected chrominance"; break;
  984. case 0xfd: p = "unknown audio standard"; break;
  985. case 0xfe: p = "forced audio standard"; break;
  986. case 0xff: p = "no detected audio standard"; break;
  987. default: p = "not defined"; break;
  988. }
  989. CX18_INFO_DEV(sd, "Detected audio standard: %s\n", p);
  990. CX18_INFO_DEV(sd, "Audio muted: %s\n",
  991. (mute_ctl & 0x2) ? "yes" : "no");
  992. CX18_INFO_DEV(sd, "Audio microcontroller: %s\n",
  993. (download_ctl & 0x10) ? "running" : "stopped");
  994. switch (audio_config >> 4) {
  995. case 0x00: p = "undefined"; break;
  996. case 0x01: p = "BTSC"; break;
  997. case 0x02: p = "EIAJ"; break;
  998. case 0x03: p = "A2-M"; break;
  999. case 0x04: p = "A2-BG"; break;
  1000. case 0x05: p = "A2-DK1"; break;
  1001. case 0x06: p = "A2-DK2"; break;
  1002. case 0x07: p = "A2-DK3"; break;
  1003. case 0x08: p = "A1 (6.0 MHz FM Mono)"; break;
  1004. case 0x09: p = "AM-L"; break;
  1005. case 0x0a: p = "NICAM-BG"; break;
  1006. case 0x0b: p = "NICAM-DK"; break;
  1007. case 0x0c: p = "NICAM-I"; break;
  1008. case 0x0d: p = "NICAM-L"; break;
  1009. case 0x0e: p = "FM radio"; break;
  1010. case 0x0f: p = "automatic detection"; break;
  1011. default: p = "undefined"; break;
  1012. }
  1013. CX18_INFO_DEV(sd, "Configured audio standard: %s\n", p);
  1014. if ((audio_config >> 4) < 0xF) {
  1015. switch (audio_config & 0xF) {
  1016. case 0x00: p = "MONO1 (LANGUAGE A/Mono L+R channel for BTSC, EIAJ, A2)"; break;
  1017. case 0x01: p = "MONO2 (LANGUAGE B)"; break;
  1018. case 0x02: p = "MONO3 (STEREO forced MONO)"; break;
  1019. case 0x03: p = "MONO4 (NICAM ANALOG-Language C/Analog Fallback)"; break;
  1020. case 0x04: p = "STEREO"; break;
  1021. case 0x05: p = "DUAL1 (AC)"; break;
  1022. case 0x06: p = "DUAL2 (BC)"; break;
  1023. case 0x07: p = "DUAL3 (AB)"; break;
  1024. default: p = "undefined";
  1025. }
  1026. CX18_INFO_DEV(sd, "Configured audio mode: %s\n", p);
  1027. } else {
  1028. switch (audio_config & 0xF) {
  1029. case 0x00: p = "BG"; break;
  1030. case 0x01: p = "DK1"; break;
  1031. case 0x02: p = "DK2"; break;
  1032. case 0x03: p = "DK3"; break;
  1033. case 0x04: p = "I"; break;
  1034. case 0x05: p = "L"; break;
  1035. case 0x06: p = "BTSC"; break;
  1036. case 0x07: p = "EIAJ"; break;
  1037. case 0x08: p = "A2-M"; break;
  1038. case 0x09: p = "FM Radio (4.5 MHz)"; break;
  1039. case 0x0a: p = "FM Radio (5.5 MHz)"; break;
  1040. case 0x0b: p = "S-Video"; break;
  1041. case 0x0f: p = "automatic standard and mode detection"; break;
  1042. default: p = "undefined"; break;
  1043. }
  1044. CX18_INFO_DEV(sd, "Configured audio system: %s\n", p);
  1045. }
  1046. if (aud_input)
  1047. CX18_INFO_DEV(sd, "Specified audio input: Tuner (In%d)\n",
  1048. aud_input);
  1049. else
  1050. CX18_INFO_DEV(sd, "Specified audio input: External\n");
  1051. switch (pref_mode & 0xf) {
  1052. case 0: p = "mono/language A"; break;
  1053. case 1: p = "language B"; break;
  1054. case 2: p = "language C"; break;
  1055. case 3: p = "analog fallback"; break;
  1056. case 4: p = "stereo"; break;
  1057. case 5: p = "language AC"; break;
  1058. case 6: p = "language BC"; break;
  1059. case 7: p = "language AB"; break;
  1060. default: p = "undefined"; break;
  1061. }
  1062. CX18_INFO_DEV(sd, "Preferred audio mode: %s\n", p);
  1063. if ((audio_config & 0xf) == 0xf) {
  1064. switch ((afc0 >> 3) & 0x1) {
  1065. case 0: p = "system DK"; break;
  1066. case 1: p = "system L"; break;
  1067. }
  1068. CX18_INFO_DEV(sd, "Selected 65 MHz format: %s\n", p);
  1069. switch (afc0 & 0x7) {
  1070. case 0: p = "Chroma"; break;
  1071. case 1: p = "BTSC"; break;
  1072. case 2: p = "EIAJ"; break;
  1073. case 3: p = "A2-M"; break;
  1074. case 4: p = "autodetect"; break;
  1075. default: p = "undefined"; break;
  1076. }
  1077. CX18_INFO_DEV(sd, "Selected 45 MHz format: %s\n", p);
  1078. }
  1079. }
  1080. static int cx18_av_log_status(struct v4l2_subdev *sd)
  1081. {
  1082. struct cx18 *cx = v4l2_get_subdevdata(sd);
  1083. log_video_status(cx);
  1084. log_audio_status(cx);
  1085. return 0;
  1086. }
  1087. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1088. static int cx18_av_g_register(struct v4l2_subdev *sd,
  1089. struct v4l2_dbg_register *reg)
  1090. {
  1091. struct cx18 *cx = v4l2_get_subdevdata(sd);
  1092. if ((reg->reg & 0x3) != 0)
  1093. return -EINVAL;
  1094. reg->size = 4;
  1095. reg->val = cx18_av_read4(cx, reg->reg & 0x00000ffc);
  1096. return 0;
  1097. }
  1098. static int cx18_av_s_register(struct v4l2_subdev *sd,
  1099. const struct v4l2_dbg_register *reg)
  1100. {
  1101. struct cx18 *cx = v4l2_get_subdevdata(sd);
  1102. if ((reg->reg & 0x3) != 0)
  1103. return -EINVAL;
  1104. cx18_av_write4(cx, reg->reg & 0x00000ffc, reg->val);
  1105. return 0;
  1106. }
  1107. #endif
  1108. static const struct v4l2_ctrl_ops cx18_av_ctrl_ops = {
  1109. .s_ctrl = cx18_av_s_ctrl,
  1110. };
  1111. static const struct v4l2_subdev_core_ops cx18_av_general_ops = {
  1112. .log_status = cx18_av_log_status,
  1113. .load_fw = cx18_av_load_fw,
  1114. .reset = cx18_av_reset,
  1115. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1116. .g_register = cx18_av_g_register,
  1117. .s_register = cx18_av_s_register,
  1118. #endif
  1119. };
  1120. static const struct v4l2_subdev_tuner_ops cx18_av_tuner_ops = {
  1121. .s_radio = cx18_av_s_radio,
  1122. .s_frequency = cx18_av_s_frequency,
  1123. .g_tuner = cx18_av_g_tuner,
  1124. .s_tuner = cx18_av_s_tuner,
  1125. };
  1126. static const struct v4l2_subdev_audio_ops cx18_av_audio_ops = {
  1127. .s_clock_freq = cx18_av_s_clock_freq,
  1128. .s_routing = cx18_av_s_audio_routing,
  1129. };
  1130. static const struct v4l2_subdev_video_ops cx18_av_video_ops = {
  1131. .s_std = cx18_av_s_std,
  1132. .s_routing = cx18_av_s_video_routing,
  1133. .s_stream = cx18_av_s_stream,
  1134. };
  1135. static const struct v4l2_subdev_vbi_ops cx18_av_vbi_ops = {
  1136. .decode_vbi_line = cx18_av_decode_vbi_line,
  1137. .g_sliced_fmt = cx18_av_g_sliced_fmt,
  1138. .s_sliced_fmt = cx18_av_s_sliced_fmt,
  1139. .s_raw_fmt = cx18_av_s_raw_fmt,
  1140. };
  1141. static const struct v4l2_subdev_pad_ops cx18_av_pad_ops = {
  1142. .set_fmt = cx18_av_set_fmt,
  1143. };
  1144. static const struct v4l2_subdev_ops cx18_av_ops = {
  1145. .core = &cx18_av_general_ops,
  1146. .tuner = &cx18_av_tuner_ops,
  1147. .audio = &cx18_av_audio_ops,
  1148. .video = &cx18_av_video_ops,
  1149. .vbi = &cx18_av_vbi_ops,
  1150. .pad = &cx18_av_pad_ops,
  1151. };
  1152. int cx18_av_probe(struct cx18 *cx)
  1153. {
  1154. struct cx18_av_state *state = &cx->av_state;
  1155. struct v4l2_subdev *sd;
  1156. int err;
  1157. state->rev = cx18_av_read4(cx, CXADEC_CHIP_CTRL) & 0xffff;
  1158. state->vid_input = CX18_AV_COMPOSITE7;
  1159. state->aud_input = CX18_AV_AUDIO8;
  1160. state->audclk_freq = 48000;
  1161. state->audmode = V4L2_TUNER_MODE_LANG1;
  1162. state->slicer_line_delay = 0;
  1163. state->slicer_line_offset = (10 + state->slicer_line_delay - 2);
  1164. sd = &state->sd;
  1165. v4l2_subdev_init(sd, &cx18_av_ops);
  1166. v4l2_set_subdevdata(sd, cx);
  1167. snprintf(sd->name, sizeof(sd->name),
  1168. "%s %03x", cx->v4l2_dev.name, (state->rev >> 4));
  1169. sd->grp_id = CX18_HW_418_AV;
  1170. v4l2_ctrl_handler_init(&state->hdl, 9);
  1171. v4l2_ctrl_new_std(&state->hdl, &cx18_av_ctrl_ops,
  1172. V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
  1173. v4l2_ctrl_new_std(&state->hdl, &cx18_av_ctrl_ops,
  1174. V4L2_CID_CONTRAST, 0, 127, 1, 64);
  1175. v4l2_ctrl_new_std(&state->hdl, &cx18_av_ctrl_ops,
  1176. V4L2_CID_SATURATION, 0, 127, 1, 64);
  1177. v4l2_ctrl_new_std(&state->hdl, &cx18_av_ctrl_ops,
  1178. V4L2_CID_HUE, -128, 127, 1, 0);
  1179. state->volume = v4l2_ctrl_new_std(&state->hdl,
  1180. &cx18_av_audio_ctrl_ops, V4L2_CID_AUDIO_VOLUME,
  1181. 0, 65535, 65535 / 100, 0);
  1182. v4l2_ctrl_new_std(&state->hdl,
  1183. &cx18_av_audio_ctrl_ops, V4L2_CID_AUDIO_MUTE,
  1184. 0, 1, 1, 0);
  1185. v4l2_ctrl_new_std(&state->hdl, &cx18_av_audio_ctrl_ops,
  1186. V4L2_CID_AUDIO_BALANCE,
  1187. 0, 65535, 65535 / 100, 32768);
  1188. v4l2_ctrl_new_std(&state->hdl, &cx18_av_audio_ctrl_ops,
  1189. V4L2_CID_AUDIO_BASS,
  1190. 0, 65535, 65535 / 100, 32768);
  1191. v4l2_ctrl_new_std(&state->hdl, &cx18_av_audio_ctrl_ops,
  1192. V4L2_CID_AUDIO_TREBLE,
  1193. 0, 65535, 65535 / 100, 32768);
  1194. sd->ctrl_handler = &state->hdl;
  1195. if (state->hdl.error) {
  1196. int err = state->hdl.error;
  1197. v4l2_ctrl_handler_free(&state->hdl);
  1198. return err;
  1199. }
  1200. err = v4l2_device_register_subdev(&cx->v4l2_dev, sd);
  1201. if (err)
  1202. v4l2_ctrl_handler_free(&state->hdl);
  1203. else
  1204. cx18_av_init(cx);
  1205. return err;
  1206. }