cx18-av-core.c 39 KB

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